Saturday, 17 March 2018

Constructor Injection in Spring

Constructor injection is one of the approach to inject the dependency in spring  and we can inject the dependencies for primitive and string types using constructor method as shown below.

It requires spring core jar files to execute the below application.

1. Student bean which has some field to store student details. It is Bean in our example.
2. Application Context xml file to configure the student bean as spring bean.
3. Main class to execute the application.

Bean:

package com.bk.spring.test;

public class StudentBean {

    private int id;
    private String name;
    private String emailId;
    private String course;
    private double fee;

    StudentBean() {
        System.out.println("default constructor");
    }

    StudentBean(String name) {
        this.name = name;
    }
  
    StudentBean(int id) {
        this.id = id;
    }

    StudentBean(int id, String name, String emailId, String course, double fee) {
        this.id = id;
        this.name = name;
        this.emailId = emailId;
        this.course = course;
        this.fee = fee;
    }

    public void getStudentDetails() {
        System.out.println("Id is " + this.id + " name is " + name
                + " email id is " + emailId + " course is " + course
                + " fee is " + fee);
    }

}


Xml file:

<?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 name="student" class="com.bk.spring.test.StudentBean">
        <constructor-arg value="001"/>
        <constructor-arg value="Krishna" />
        <constructor-arg value="krishna@abc.com" />
        <constructor-arg value="Spring" />
        <constructor-arg value="2018" />
    </bean>
</beans>

Execution Program:

package com.bk.spring.test;

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 SpringConDITest {

    public static void main(String a[]) {

        Resource res = new ClassPathResource("applicationContext.xml");
        BeanFactory factory = new XmlBeanFactory(res);
        StudentBean bean = (StudentBean) factory.getBean("student");
        bean.getStudentDetails();

    }

}

Output : Id is 1 name is Krishna email id is krishna@abc.com course is Spring fee is 2018.0

If we comment all constructor args tags in xml file, then default constructor will call

<?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 name="student" class="com.bk.spring.test.StudentBean">
        <!-- <constructor-arg value="001"/>
        <constructor-arg value="Krishna" />
        <constructor-arg value="krishna@abc.com" />
        <constructor-arg value="Spring" />
        <constructor-arg value="2018" /> -->
    </bean>
</beans>
Output : default constructor
Id is 0 name is null email id is null course is null fee is 0.0

Similar way we cam initialize string parameterized constructor as shown below

<?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 name="student" class="com.bk.spring.test.StudentBean">
        <!-- <constructor-arg value="001"/> -->
        <constructor-arg value="Krishna" />
        <!-- <constructor-arg value="krishna@abc.com" />
        <constructor-arg value="Spring" />
        <constructor-arg value="2018" /> -->
    </bean>
</beans>

Output : Id is 0 name is Krishna email id is null course is null fee is 0.0

We need to take care of order of elements to pass like if i change the xml file as below, then second value will print email id. Just what ever we give test, it will assign to variable. No error but order also need to follow for our safety.

<?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 name="student" class="com.bk.spring.test.StudentBean">
        <constructor-arg value="001"/>
        <constructor-arg value="krishna@abc.com" />
        <constructor-arg value="Krishna" />
        <constructor-arg value="Spring" />
        <constructor-arg value="2018" />
    </bean>
</beans>

Output: Id is 1 name is krishna@abc.com email id is Krishna course is Spring fee is 2018.0

Suppose if we have multiple single parameterized constructor and if we want to initialize specific constructor we need to specify the type and if we did not specify the type by default it will initialize the string parameterized constructor.

Below will call string param constructor;

<?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 name="student" class="com.bk.spring.test.StudentBean">
        <constructor-arg value="001"/>
        <!-- <constructor-arg value="Krishna" />
        <constructor-arg value="krishna@abc.com" />
        <constructor-arg value="Spring" />
        <constructor-arg value="2018" /> -->
    </bean>
</beans>

Output: Id is 0 name is 001 email id is null course is null fee is 0.0

Below will call int param constructor

<?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 name="student" class="com.bk.spring.test.StudentBean">
        <constructor-arg value="001" type="int"/>
        <!-- <constructor-arg value="Krishna" />
        <constructor-arg value="krishna@abc.com" />
        <constructor-arg value="Spring" />
        <constructor-arg value="2018" /> -->
    </bean>
</beans>

Output : Id is 1 name is null email id is null course is null fee is 0.0

No comments:

Post a Comment