Spring Core- Defining Bean Properties by Shortcut

Posted on Updated on

Spring supports a shortcut for specifying the value of a simple type property.

In case of creating beans using setter method, we use below Spring configuration.

For creating beans using constructor, we use below Spring configuration.

Since Spring 2.0 another convenient shortcut to define properties was added. It consists of using the p schema to define bean properties as attributes of the element. This can shorten the lines of XML configuration.

The below example demonstrate creation of beans using p schema.

1) School.java

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

2) Spring Configuration.
Bean creation using p schema.

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.