Master java skills

Setter Injection

In this approach, dependency injection happens via setter methods of the properties. It is done as mentioned below.

  • Subelement <property> of <bean> element is used for this purpose. <property> takes two attributes: name and value.
  • “name” takes the name of the property in the java class and “value” takes its value.
  • name and value both are given in the double quotes.
  • Irrespective of the data type, value of ‘value’ attribute is given in the double quotes.
<?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="emp" class="com.sks.Employee">
		<property name="id" value="10"></property>
		<property name="name" value="Mark"></property>
		<property name="age" value="25"></property>
		<property name="contract" value="true" />
	</bean>
</beans>

Note -> Make sure if you are using setter based injection, then the bean class must have the setter methods and a default (no-args) constructor.

Setter injection for custom object

If a property is a custom object, then we use ‘ref’ attribute in stead of ‘value’. In the ‘ref’ attribute, we give bean id of the dependent object. Refer the below example

Employee class has Address class dependency

package com.sks;

public class Employee {
		
	private int id;
	private String name;
	private int age;
	private boolean contract;
	//custom dependency
	private Address add;
		
	//getter/setter methods	
	//constructor
}

Address.java

package com.sks;

public class Address {
	
	private String addLine;
	private String city;
	private String country;
	
	//getter/setter methods	
	//constructor	
}

applicationContext.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="emp" class="com.sks.Employee">
		<property name="id" value="10"></property>
		<property name="name" value="Mark"></property>
		<property name="age" value="25"></property>
		<property name="contract" value="true" />
		<property name="add" ref="add1" />
	</bean>
	<bean id="add1" class="com.sks.Address">
		<property name="addLine" value="Street 1"></property>
		<property name="city" value="London"></property>
		<property name="country" value="UK"></property>
	</bean>

</beans>
	

Testing class that has main method

package com.sks;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class EmployeeMain {
	
	public static void main(String[] args) {
		
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		Employee e1 = (Employee) context.getBean("emp");
		
		System.out.println("e1 bean information is : " + e1);
		
		((ClassPathXmlApplicationContext) context).close();
		
	}

}
Output :

e1 bean information is : Employee [id=10, name=Mark, age=25, contract=true, add=Address [addLine=Street 1, city=London, country=UK]]

Setter injection for List

If there is a list in the mapping bean class, we can map it like below.

	<bean id="book" class="com.sks.Book">
		<property name="name" value="Lovely Revolution"></property>
		<property name="sellingPlatforms">
		 <list>
		 	<value>Amazon</value>
		 	<value>Flipkart</value>
		 	<value>BookAdda</value>
		 </list>
		</property>
	</bean>

Note -> If values in the list are custom objects, then, in stead of ‘<value>’ element, you have to use <ref bean = “bean-name”> element.

If list contains custom objects, then mapping will change according to below. Let’s assume list contains object of Platform class.

<?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="book" class="com.sks.Book">
		<property name="name" value="Learn Java in 21 days"></property>
		<property name="sellingPlatforms">
		 <list>
		 	<ref bean="platform1">
		 	<ref bean="platform2">
		 	<ref bean="platform3">
		 </list>
		</property>
	</bean>
	
	<bean id="platform1" class="com.sks.Platform">
		<property name="id" value="1"></property>
		<property name="name" value="Amazon"></property>
	</bean>
	
	<bean id="platform2" class="com.sks.Platform">
		<property name="id" value="2"></property>
		<property name="name" value="Flipkart"></property>
	</bean>
	
	<bean id="platform3" class="com.sks.Platform">
		<property name="id" value="3"></property>
		<property name="name" value="BookAdda"></property>
	</bean>

</beans>

Platform.java class

package com.sks;

import java.util.List;

public class Platform {

	private int id;
	private String name;
	
	//setter and getters
	//constructor
	//toString()

}

Below is the full example

package com.sks;

import java.util.List;

public class Book {

	private String name;
	private List<String> sellingPlatforms;
	
	//setter and getters
	//constructor
	//toString()

}
<?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="book" class="com.sks.Book">
		<property name="name" value="Lovely Revolution"></property>
		<property name="sellingPlatforms">
		 <list>
		 	<value>Amazon</value>
		 	<value>Flipkart</value>
		 	<value>BookAdda</value>
		 </list>
		</property>
	</bean>

</beans>

Main testing class

package com.sks;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BookMain {
	
	public static void main(String[] args) {
		
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Book book = (Book) context.getBean("book");
		
		System.out.println("Book info : " + book);
		
		((ClassPathXmlApplicationContext) context).close();
				
	}
}
Output :
Book info : Book [name=Lovely Revolution, sellingPlatforms=[Amazon, Flipkart, BookAdda]]

Setter injection for map

If one of the properties in the class is a Map, then refer the below example.

package com.sks;

import java.util.Map;

public class Book {

	private String name;
	private Map<String, Double> platformPriceMap;
	
	//getters and setters
	//toString()
		
}

applicationContext.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="book" class="com.sks.Book">
		<property name="name" value="Lovely Revolution"></property>
		<property name="platformPriceMap">
		 <map>
		 	<entry key="Amazon" value="123.00"/>
		 	<entry key="Flipkart" value="130.50"/>
		 	<entry key="BookAdda" value="129"/>
		 </map>
		</property>
	</bean>

</beans>

Note -> If map key and/or value are custom objects, then in stead of ‘key’ and ‘value’, you need to use ‘key-ref’ and ‘value-ref’.

Main testing class

package com.sks;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BookMain {
	
	public static void main(String[] args) {
		
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Book book = (Book) context.getBean("book");
		
		System.out.println("Book info : " + book);
		
		System.out.println("Price on Amazon = " + book.getPlatformPriceMap().get("Amazon"));
		
		((ClassPathXmlApplicationContext) context).close();
	}
}
Output :
Book info : Book [name=Lovely Revolution, platformPriceMap={Amazon=123.0, Flipkart=130.5, BookAdda=129.0}]
Price on Amazon = 123.0