☰ See All Chapters |
property tag in Spring
In setter injection to inject primitive values we should use value attribute and to inject object values we should use ref attribute of <property>. While injecting object values ref attribute should be assigned with id value of other beans.
Along with value and ref attributes <property> element has name attribute. Below tables explains all the attributes of <property> and child tags of <property>.
Attributes of <property> tag
Attribute | Occurrence | Description |
name | required | This attribute takes the name of a property in the javabean class. |
value | Optional | Used to assign value to the variable of primitive type |
ref | Optional | Used to assign Object type value to variable. This ref attribute should refer the id value of other bean in the spring configuration file. |
Child tags of <property> element
<property> element can have one and only one child tags. Child tags of <constructor-arg> element are given in tables below.
Element | Description |
<value>content</value> | This is same as using ‘value’ attribute in < property > . Using ‘value’ attribute in < property > is more recommended over the <value> child element. |
<ref>
| This is same as using ‘ref’ attribute in < property >. Using ‘ref’ attribute in < property > is more recommended over the <value> child element. But unlike <value> element we have to pass the Object (id of other bean ) to the bean attribute of <ref> element. |
<null> | This is used to pass null value to the variable of bean. Variable should not be of primitive type. <bean id="studentbean" class="com.java4coding.EmployeeBean"> <property name="add" ref="adrress"> <null></null> </property> <property name="name" value="Manu Manjunatha"></property> </bean> |
<list> | This is used to pass List object to the variable of bean. Variable should be of type List.
public class ClassRoom { public List<String> students ; public void setStudents(List<String> students) { this.students = students; } }
<bean class="com.java4coding.ClassRoom"> <property name="students"> <list> <value>Manu Manjunath</value> <value>Likitha Manjunatha</value> <value>Manoj Manjunatha</value> </list> </property> </bean> |
<set> | This is used to pass Set object to the variable of bean. Variable should be of type Set. public class ClassRoom { public Set<String> students ; public void setStudents(Set<String> students) { this.students = students; } } <bean class="com.java4coding.ClassRoom"> <property name="students"> <set> <value>Manu Manjunath</value> <value>Likitha Manjunatha</value> <value>Manoj Manjunatha</value> </set> </property> </bean> |
<map> | This is used to pass Map object to the variable of bean. Variable should be of type Map.
public class ClassRoom { public Map<String, String> students ; public void setStudents(Map<String, String> students) { this.students = students; } } <bean class="com.java4coding.ClassRoom"> <property name="students"> <map> <entry key="name" value="Manu Manjunatha"></entry> <entry key="id" value="123"></entry> <entry key="marks" value="100"></entry> </map> </property> </bean> |
<prop> | This is used to pass Properties object to the variable of bean. Variable should be of type Properties.
public class ClassRoom { public Properties students ; public void setStudents(Properties students) { this.students = students; } } <bean class="com.java4coding.ClassRoom"> <property name="students"> <props> <prop key="name">Manu Manjunatha</prop> <prop key="id">123</prop> <prop key="marks">100</prop> </props> </property> </bean> |
<bean> | This is used create and pass inner bean to the variable of bean. <bean id="studentbean" class="com.java4coding.EmployeeBean"> <property name="add" ref="adrress"> <bean id="adrress" class="com.java4coding.EmplyeeAdress"> <property name="city" value="Bangalore"></property> </bean> </property> <property name="name" value="Manu Manjunatha"></property> </bean> |
All Chapters