×
☰ See All Chapters

constructor-arg tag in Spring

In constructor injection to inject primitive values we should use value attribute and to inject object values we should use ref attribute of <constructor-arg>. While injecting object values ref attribute should be assigned with id value of other beans.

Along with value and ref attributes <constructor-arg> element has name, index and type attributes. Below tables explains all the attributes of <constructor-arg> and child tags of <constructor-arg>.

Attributes of <constructor-arg> tag

Attribute

Occurrence

Description

name

Optional

This attribute takes the name of a property in the javabean class. This should be used if index attribute is not used.

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.

index

Optional

This should be used if name attribute is not used.

 

type

Optional

 

 

Child tags of <constructor-arg> element

<constructor-arg> 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 <constructor-arg> . Using ‘value’ attribute in <constructor-arg> is more recommended over the <value> child element.

 

<ref>

 

This is same as using ‘ref’  attribute in <constructor-arg> . Using ‘ref’ attribute in <constructor-arg> 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">

        <constructor-arg name="add">

                <null></null>

        </constructor-arg>

        <constructor-arg name="name" value="Manu Manjunatha"></constructor-arg>

</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 ClassRoom(List<String> students) {

                this.students = students;

        }

}

        <bean class="com.java4coding.ClassRoom">

                <constructor-arg name="students">

                        <list>

                                <value>Manu Manjunath</value>

                                <value>Likitha Manjunatha</value>

                                <value>Manoj Manjunatha</value>

                        </list>

                </constructor-arg>

        </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 ClassRoom(Set<String> students) {

                this.students = students;

        }

}

 

        <bean class="com.java4coding.ClassRoom">

                <constructor-arg name="students">

                        <set>

                                <value>Manu Manjunath</value>

                                <value>Likitha Manjunatha</value>

                                <value>Manoj Manjunatha</value>

                        </set>

                </constructor-arg>

        </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 ClassRoom(Map<String, String> students) {

                this.students = students;

        }

}

 

        <bean class="com.java4coding.ClassRoom">

                <constructor-arg name="students">

                        <map>

                                <entry key="name" value="Manu Manjunatha"></entry>

                                <entry key="id" value="123"></entry>

                                <entry key="marks" value="100"></entry>

                        </map>

                </constructor-arg>

        </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 ClassRoom(Properties students) {

                this.students = students;

        }

}

 

        <bean class="com.java4coding.ClassRoom">

                <constructor-arg name="students">

                        <props>

                                <prop key="name">Manu Manjunatha</prop>

                                <prop key="id">123</prop>

                                <prop key="marks">100</prop>

                        </props>

                </constructor-arg>

        </bean>

<bean>

This is used create and pass inner bean to the variable of bean.

<bean id="studentbean" class="com.java4coding.EmployeeBean">

        <constructor-arg name="add" ref="adrress">

                <bean id="adrress" class="com.java4coding.EmplyeeAdress">

                        <constructor-arg name="city" value="Bangalore"></constructor-arg>

                </bean>

        </constructor-arg>

        <constructor-arg name="name" value="Manu Manjunatha"></constructor-arg>

</bean>

 

 

 

 

 


All Chapters
Author