W3Schools Learner's Blog

w3Schools programming knowledge summary website

div

12/12/2017

Setter Injection with Object properties in spring

Setter Injection with Object properties in spring example
Like Constructor Injection, we can inject the dependency of another bean using setters. In such case, we use property element. Here, our scenario is Employee has Address. The Address class object will be termed as the dependent object. Let's see the Address class first:
Address.java
This class contains four properties, setters and getters and toString() method.
  1. package com.tpyyes;  
  2.   
  3. public class Address {  
  4. private String addressLine1,city,state,country;  
  5.   
  6. //getters and setters ..... 
  7.   
  8. public String toString(){  
  9.     return addressLine1+" "+city+" "+state+" "+country;  
  10. }  
Employee.javaIt contains three properties id, name and address(dependent object) , setters and getters with displayInfo() method.
  1. package com.tpyyes;  
  2.   
  3. public class Employee {  
  4. private int id;  
  5. private String name;  
  6. private Address address;  
  7.   
  8. //setters and getters  ......
  9.   
  10. void displayInfo(){  
  11.     System.out.println(id+" "+name);  
  12.     System.out.println(address);  
  13. }  
  14. }  

applicationContext.xmlThe ref attribute of property elements is used to define the reference of another bean.
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  8.   
  9. <bean id="address1" class="com.javatpoint.Address">  
  10. <property name="addressLine1" value="51,Lohianagar"></property>  
  11. <property name="city" value="Ghaziabad"></property>  
  12. <property name="state" value="UP"></property>  
  13. <property name="country" value="India"></property>  
  14. </bean>  
  15.   
  16. <bean id="emp" class="com.javatpoint.Employee">  
  17. <property name="id" value="1"></property>  
  18. <property name="name" value="Sachin Yadav"></property>  
  19. <property name="address" ref="address1"></property>  
  20. </bean>  
  21.   
  22. </beans>  
Test.javaThis class gets the bean from the applicationContext.xml file and calls the displayInfo() method.
  1. package com.javatpoint;  
  2.   
  3. import org.springframework.beans.factory.BeanFactory;  
  4. import org.springframework.beans.factory.xml.XmlBeanFactory;  
  5. import org.springframework.context.ApplicationContext;  
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  7. import org.springframework.core.io.ClassPathResource;  
  8. import org.springframework.core.io.Resource;  
  9.   
  10. public class Test {  
  11. public static void main(String[] args) {  
  12.     Resource r=new ClassPathResource("applicationContext.xml");  
  13.     BeanFactory factory=new XmlBeanFactory(r);  
  14.       
  15.     Employee e=(Employee)factory.getBean("emp");  
  16.     e.displayInfo();  
  17.       
  18. }  
  19. }  

No comments:

Post a Comment

Note: only a member of this blog may post a comment.