Spring Core – Create bean(POJO) using constructor

Posted on Updated on

Constructor is common way to create a instance of class. In Spring framework we have facility to create a bean using constructors. The below example demonstrate how to create a bean using constructor.

To create bean(POJO) using setter method, click here.

1) School.java

We have a School bean which contain name and noOfFaculties as instance variables.

2) Spring Configuration

We have created a bean with name school having class attribute pointing to the java class.

Value for instance variables name and noOfFaculties have been injected through constructor-arg tag.

3) Load bean xml and run the program

4) Output.

Name : Code2Succeed
No of Faculties : 100

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.