Spring- Constructor Injection

Spring - Constructor Injection

In this type of injection Spring Container uses constructor of the bean class for assigning the dependencies. In SpringConfig.xml, we need to inform to the spring IOC container about constructor injection by using <constructor -arg >

In spring bean class , if both constructor and setter injection applied for same property then constructor injection will be overridden by setter injection, because constructor injection will happen at the object creation time, and setter after objection. so finally, setter injected data will be there.

1.Constructor Injection –Primitive Types

public class Student {
	private int sno;
	private String name;
	public Student(int sno, String name) { 
 this.sno = sno;
 this.name = name;
	}
}
<!-- File : SpringConfig.xml -->
	<bean id="ob" class="Student">
 <constructor-arg name="sno" value="103"></constructor-arg>
 <constructor-arg name="name" value="Satya"></constructor-arg>
	</bean>

2.Constructor Injection –Object Types

public class Student {
	private int sno;
	private String name;
	private Address address;

	public Student(int sno, String name, Address address) {
 this.sno = sno;
 this.name = name;
 this.address = address;
	}
}

public class Address {
	private int hno;
	private String city;

	public Address(int hno, String city) {
 this.hno = hno;
 this.city = city;
	}
}
<beans>
	<bean id="st" class="obj.Student">
 <constructor-arg name="sno" value="101"></constructor-arg>
 <constructor-arg name="name" value="Satya Kaveti"></constructor-arg>
 <constructor-arg name="address">
 	<ref bean="adr" />
 </constructor-arg>
	</bean>
	<bean id="adr" class="obj.Address">
 <constructor-arg name="hno" value="305"></constructor-arg>
 <constructor-arg name="city" value="HYDERABAD"></constructor-arg>
	</bean>
</beans>

3. Constructor Injection –Collection Types

public class Country {
	private String countryName;
	private List<State> states;
	public Country(String countryName, List<State> states) {
 super();
 this.countryName = countryName;
 this.states = states;
	}	 
}
public class State {
	private String stName;
	private String stCapital;
	public State(String stName, String stCapital) {
 super();
 this.stName = stName;
 this.stCapital = stCapital;
	}
}
<bean id="ob" class="collectionsref.Country">
 <constructor-arg name="countryName" value="INDIA"></constructor-arg>
 <constructor-arg name="states">
 	<list>
   <ref bean="list1"/>
   <ref bean="list2"/>
 	</list>
 </constructor-arg>  
</bean>
	
<bean id="list1" class="collectionsref.State">  
 <constructor-arg name="stName" value="ANDHRA"></constructor-arg>
 <constructor-arg name="stCapital" value="HYDERABAD"></constructor-arg>   
</bean>


Example

public class Country {
	private String countryName;
	private List<State> states;

	public Country(String countryName, List<State> states) {
 super();
 this.countryName = countryName;
 this.states = states;
	}

	public void getCountry() {
 System.out.println("Country Name : " + this.countryName);
 List<State> states = this.states;
 Iterator<State> itr = states.iterator();
 while (itr.hasNext()) {
 	State s = (State) itr.next();
 	s.getState();
 }
	}
}
package core;

public class State {
	private String stName;
	private String stCapital;
 
	public State(String stName, String stCapital) {
 this.stName = stName;
 this.stCapital = stCapital;
	}
	public void getState() {
 System.out.println(this.stName + ", " + this.stCapital);
	}
}
<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="ob" class="core.Country">
 <constructor-arg value="INDIA"></constructor-arg>
 <constructor-arg>
 	<list>
  <ref bean="list1" />
  <ref bean="list2" />
 	</list>
 </constructor-arg>
	</bean>

	<bean id="list1" class="core.State">
 <constructor-arg value="ANDHRA"></constructor-arg>
 <constructor-arg value="HYDERABAD"></constructor-arg>
	</bean>

	<bean id="list2" class="core.State">
 <constructor-arg value="TAMILNADU"></constructor-arg>
 <constructor-arg value="CHENNAI"></constructor-arg>
	</bean>

</beans>
public class App2 {
	public static void main(String[] args) {

 Resource resource = new ClassPathResource("Sp1.xml");
 BeanFactory factory = new XmlBeanFactory(resource);

 Object ob = factory.getBean("ob");
 Country c = (Country) ob;
 c.getCountry();
	}
}
Country Name : INDIA
ANDHRA, HYDERABAD
TAMILNADU, CHENNAI
Setter Injection Constructor Injection
1.Partial injection possible: if we have 3 dependencies like int, string, long, then its not necessary to inject all values if we use setter injection. If you are not inject it will takes default values for those primitives 1.Partial injection NOT possible: for calling constructor we must pass all the arguments, otherwise we will get Error.
2. Setter Injection will overrides the constructor injection value, if we write setter and constructor injection for the same property . 2. Constructor injection cannot overrides the setter injected values
3. If we have more dependencies, for example 15 to 20 are there in our bean class then, in this case setter injection is not recommended. Because we need to write almost 20 setters right, bean length will increase. 3. In this case, Constructor injection is highly recommended as we can inject all the dependencies with in 3 to 4 lines .
4. Setter injection makes bean class object as mutable i.e We can change 4. Constructor injection makes bean class object as immutable.i.e We cannot change