Spring Constructor Injection Example

Spring Constructor Injection Example

In this example we have explained how to achieve DI using constructor injection. This example is same as previous setter injection example in term of project structure, jars required and Output but differ in Pojo’s and spring-config.xml.
Employee.java :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.poosan;
public class Employee {
    private String name;
    private int age;
    private Address address;
    public Employee(String name, int age, Address address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }
    public Address getAddress() {
        return address;
    }
    @Override
    public String toString() {
        return "Name :"+name+"\n"+"Age :"+age;
    }
}

Address.java :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.thecafetechno;
public class Address {
    private String city;
    private String state;
    public Address(String city,String state) {
        this.city=city;
        this.state=state;
    }
    @Override
    public String toString() {
        return "City: "+city+"\n"+"State: "+state;
    }
}

EmployeeAddress.java :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.poosan;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class EmployeeAddress {
    public static void main(String[] args) {
        ClassPathResource resource = new ClassPathResource("spring-  config.xml");
        BeanFactory factory = new XmlBeanFactory(resource);
        Employee employee=(Employee)factory.getBean("employeeBean");
        System.out.println("Employee: "+employee);
        System.out.println("Address: "+employee.getAddress());
    }
}

spring-config.xml :

We have defined two beans in the spring xml configuration file. The root tag of file must be <beans>. All other beans should be declared in <beans> tag. Each bean is declared by tag <bean> inside the root tag. Attribute id is used as unique identifier which is used to access the bean. Attribute class defines the full qualified name of the class whose bean is being created.
Tag <constructor-arg> is used inside <bean> to define arguments of constructor. Constructor args are identified either by ‘type’ or ‘index’ attribute of tag <constructor-arg>. We have used both in our example to show how to use these.
In case of <constructor-arg> with ‘index’ attribute, index values start from 0, index value must match with the position of constructor arg of Pojo e.g. index=”0” for city and index=”1” for state. Order in which <constructor-arg> are defined does not matter if attribute ‘index’ is used.
In case of <constructor-arg> with ‘type’ attribute, the value of attribute ‘value’ must be compatible with type declared by attribute ‘type’. And type should be same as the type of argument of constructor. Order in which <constructor-arg> are defined does not matter if types declared by ‘type’ attribute are different int the bean as in this example types string,int and Address are different data types. But if in a bean two or more <constructor-arg> have the same type then order does matter and values are assigned to the constructor arguments in the order in which these are defined.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="employeeBean" class="com.poosan.Employee">
          <constructor-arg type="java.lang.String" value="Davinder"/>
          <constructor-arg type="int" value="24"/>
          
          <constructor-arg type="Address" ref="addressBean"/>
        </bean>
        <bean id="addressBean" class="com.poosan.Address">
         <constructor-arg index="0" value="Hoshiarpur"/>
         <constructor-arg index="1" value="Punjab"/>
        </bean>
</beans>
To run this example download code , import it into eclipse and configure build path by adding jars to classpath.

SHARE

Unknown

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment