Spring- Setter Injection

Spring - Setter Injection

1. Setter Injection with Primitive Types

package core;

public class Student {

	private int sno;
	private String name;
	private String address;

	public int getSno() {
 return sno;
	}

	public void setSno(int sno) {
 this.sno = sno;
	}

	public String getName() {
 return name;
	}

	public void setName(String name) {
 this.name = name;
	}

	public String getAddress() {
 return address;
	}

	public void setAddress(String address) {
 this.address = address;
	}

	@Override
	public String toString() {
 return "Student [sno=" + sno + ", name=" + name + ", address=" + address + "]";
	}

}

//SpDI.xml

<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="st" class="core.Student">
 <property name="sno" value="100"></property>
 <property name="name" value="Satya" />
 <property name="address" value="HYDERABAD"></property>
	</bean>

</beans>
package core;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
	public static void main(String[] args) {

 // Instantiating a container
 ApplicationContext context = new ClassPathXmlApplicationContext("SpDI.xml");

 Student student = (Student) context.getBean("st");
 System.out.println(student);
	}
}

----------------------------------------------
Student [sno=100, name=Satya, address=HYDERABAD]


2.Setter Injection with Object Types

If our class is depending on other class object, then dependency is in the form of object

  • If one spring bean is depending on another spring bean class for performing some logic, this process of dependency is called Object dependency.

  • If object dependency is there then in spring framework, the spring IOC container is responsible for creating that required object and injecting into the dependent class

  • For xml, we have 2 ways to inform to the spring container about this object dependency

    • Using <ref/> element
    • Using Inner beans


1.Using Tag

we can write any number of spring configuration xmls for the spring application. Our collaborator bean may be in same xml or other xml so spring has given these 3 options(local/parent/bean).

<ref local/parent/bean= "id of collaborator bean">

a. <ref local="id value">

If we use the local attribute in the <ref > element, then the spring IOC container will verify for the collaborator bean with in same container (same xml).

<beans>
 
  <bean id="id1">
    <property name="sb" class="Student">
      <ref local="id2" />
    </property>
  </bean>
 
  <bean id="id2" class="Address"> 

</beans>

b.<ref parent="id value" >
If we use the parenet attribute in the <ref > element, then the spring IOC container will verify for the collaborator bean with in other container (other xml)

//SpConfig1.xml
<beans>
  	<bean id="id1">
     <property name="sb" class="Student">
       <ref parent="id2" />
     </property>
 	 </bean>
</beans> 

//SpConfig2.xml
<beans>
  <bean id="id2" class="Address">
</beans>

c.<ref bean="id value" >
If we give attribute as bean, then first it will check at local xml file, then parent if its not available at local

public class Student {

	private int sno;
	private String name;
	private Address address;

	public int getSno() {
 return sno;
	}

	public void setSno(int sno) {
 this.sno = sno;
	}

	public String getName() {
 return name;
	}

	public void setName(String name) {
 this.name = name;
	}

	public Address getAddress() {
 return address;
	}

	public void setAddress(Address address) {
 this.address = address;
	}

	@Override
	public String toString() {
 return "Student [sno=" + sno + ", name=" + name + "]";
	}

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

	public int getHno() {
 return hno;
	}

	public void setHno(int hno) {
 this.hno = hno;
	}

	public String getCity() {
 return city;
	}

	public void setCity(String city) {
 this.city = city;
	}

	@Override
	public String toString() {
 return "Address [hno=" + hno + ", city=" + city + "]";
	}
}
<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="st" class="core.Student">
 <property name="sno" value="100"></property>
 <property name="name" value="Satya" />
 <property name="address">
 	<ref bean="addr" />
 </property>
	</bean>

	<bean id="addr" class="core.Address">
 <property name="hno" value="200"></property>
 <property name="city" value="HYDERABAD"></property>
	</bean>

</beans>
package core;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
	public static void main(String[] args) {

 // Instantiating a container
 ApplicationContext context = new ClassPathXmlApplicationContext("SpDI.xml");

 Student student = (Student) context.getBean("st");
 System.out.println(student);

 System.out.println(student.getAddress().toString());
	}
}
Student [sno=100, name=Satya]
Address [hno=200, city=HYDERABAD]
2.Using Inner Bean
<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="st" class="core.Student">
 <property name="sno" value="100"></property>
 <property name="name" value="Satya" />
 
 <!-- INNER BEAN -->
 <property name="address">
 	<bean id="addr" class="core.Address">
  <property name="hno" value="200"></property>
  <property name="city" value="HYDERABAD"></property>
 	</bean>
 </property>
 <!-- INNER BEAN -->
 
	</bean>

3.Setter Injection with Collection Types

In Spring bean class, we can use any of the following 4 types of collections as dependency, along with Primitives Types and Objects Types

  • List

  • Set

  • Map

  • Properties

Spring supports only these 4 collections. if we use other than these Collections, programmer should have to take care about Dependency injection because Spring IoC doesn’t know other collections.

1.<list > List allows Duplicate Values

---------------------------------------------------
We use <value> in the case of primitive types
---------------------------------------------------
<property name="states">
	<list>
 <value>ANDHRA</value>
 <value>ANDHRA</value>
 <value>TELANGANA</value>
 <value>TAMILNADU</value>
	</list>
</property>
---------------------------------------------
We use <ref> in the case of Object types
----------------------------------------------
<bean id="ob" class="collectionsref.Country">
 <property name="countryName" value="INDIA"></property>
 <property name="states">
 	<list>
   <ref bean="list1"/>
   <ref bean="list2"/>
 	</list>
 </property>  
</bean>
	
	
<bean id="list1" class="collectionsref.State">  
 <property name="stName" value="ANDHRA"></property>
 <property name="stCapital" value="HYDERABAD"></property>    
</bean>

2.<set> : Set Doesn’t allow Duplicate Values

------------------------------------------------
We use <value> in the case of primitive types
------------------------------------------------
<property name="states">
	<set>
 <value>ANDHRA</value>//Not allows Duplicates
 <value>ANDHRA</value>
 <value>TELANGANA</value>
 <value>TAMILNADU</value>
	</set>
</property>

------------------------------------------------
We use <ref> in the case of Object types
------------------------------------------------
	<bean id="ob" class="collectionsref.Country">
 <property name="countryName" value="INDIA"></property>
 <property name="states">
 	<set>
   <ref bean="set1"/>
   <ref bean="set2"/>
 	</set>
 </property>  
	</bean>
	
	
	<bean id="set1" class="collectionsref.State">  
 <property name="stName" value="ANDHRA"></property>
 <property name="stCapital" value="HYDERABAD"></property>   
	</bean>

3.<map> Map will accept data in <KEY, VALUE> pair, here <KEY> must be UNIQUE

We use <entry key=" " value=" ">in the case of primitive types
<map>
	<entry key="ANDHRA" value="VIJAYAWADA"></entry>
	<entry key="TELANGANA" value="HYDERABAD"></entry>
	<entry key="TAMILNADU" value="CHENNAI"></entry>
</map> 
 
We use <entry key-ref=" " value-ref=" ">in the case of Object types
<map>
	<entry key-ref ="statesObj1" value-ref="capObj1"></entry>
	<entry key-ref ="statesObj2" value-ref ="capObj2"></entry>	 
</map> 
public class Country {
	private String countryName;
	private List<State> states;

	public String getCountryName() {
 return countryName;
	}

	public void setCountryName(String countryName) {
 this.countryName = countryName;
	}

	public List<State> getStates() {
 return states;
	}

	public void setStates(List<State> states) {
 this.states = states;
	}
}
package core;

public class State {
	private String stName;
	private String stCapital;

	public String getStName() {
 return stName;
	}

	public void setStName(String stName) {
 this.stName = stName;
	}

	public String getStCapital() {
 return stCapital;
	}

	public void setStCapital(String stCapital) {
 this.stCapital = stCapital;
	}
}

//Sp1.xml

<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="ctr" class="core.Country">
 <property name="countryName" value="INDIA"></property>
 <property name="states">
  <list>
  	<ref bean="st1"/>
  	<ref bean="st2"/>
  </list>
 </property>

	</bean>


	<bean id="st1" class="core.State">	
 <property name="stName" value="ANDRA" />
 <property name="stCapital" value="VIJAYAWADA" />
	</bean>

	<bean id="st2" class="core.State">	
 <property name="stName" value="KARNATAKA" />
 <property name="stCapital" value="BANGLORE" />
	</bean>

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

 // Instantiating a container
 ApplicationContext context = new ClassPathXmlApplicationContext("Sp1.xml");

 Country country = (Country) context.getBean("ctr");
 System.out.println(country.getCountryName());

 for (State s : country.getStates()) {
 	System.out.println(s.getStName() + ": " + s.getStCapital());
 }
	}
}
INDIA
ANDRA: VIJAYAWADA
KARNATAKA: BANGLORE