Constructor Injection

In constructor injection spring container uses constructor of the bean class for assigning the dependencies. The <constructor-arg> subelement of <bean> is used for constructor injection in spring configuration file. In spring bean class, if both constructor and setter injection applied for same property then constructor injection will be overridden by setter injection.

Let's see an example for better clarity:

Required Files:

  • StudentBean.java
  • spConfig.xml
  • Test.java

StudentBean.java

package java8s;
public class StudentBean {
    private int regdno;
    private String name;

    public StudentBean(int regdno, String name) {
        this.regdno = regdno;
        this.name = name;
    }

    public void show() {
        System.out.println(regdno + "-->" + name);
    }



}

spConfig.xml:

<< ? xml version = "1.0"
encoding = "UTF-8" ? >
    <
    beans
xmlns = "http://www.springframework.org/schema/beans"
xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns: p = "http://www.springframework.org/schema/p"
xsi: schemaLocation = "http://www.springframework.org/schema/beans
http: //www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <
    bean id = "id1"
class = "java8s.StudentBean" >
    <
    constructor - arg value = "101" > < /constructor-arg> <
    constructor - arg value = "Arunima Bairiganjan" > < /constructor-arg> <
    /bean> <
    /beans>

Test.java:

package java8s;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Test {
    public static void main(String[] args) {
        Resource res = new ClassPathResource("spconfig.xml");
        BeanFactory bf = new XmlBeanFactory(res);
        Object ob = bf.getBean("id1");
        StudentBean sb = (StudentBean) ob;
        sb.show();
    }
}

Output :

Injecting String based values:

 

If you don't specify the type attribute in the constructor-arg element, by default string type constructor will be invoked.

....
<bean id="id4" class="java8s.Student">
<constructor-arg value="101"> </constructor-arg>
</bean>
....

If you change the bean element as given above, string parameter constructor will be invoked and the output will be 0 101.

Output:0 101

We can pass the string value which is as follows:

....
<bean id="id4" class="java8s.Student">
<constructor-arg value="Tilan"> </constructor-arg>
</bean>
....

Output:
0 Tilan

We can pass integer value and string both as :

....
<bean id="id4" class="java8s.Student">
<constructor-arg value="101" type="int"> </constructor-arg>
<constructor-arg value=”Tilan"> </constructor-arg>
</bean>


....

Output:
101 Tilan

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology, BBSR
Call to: 0674-2361252

Comment