W3Schools Learner's Blog

W3Schools Programming knowledge summary website

div

12/10/2017

Spring Ref Bean(Constructor Injection with Dependent Object)

If there is HAS-A relationship between the classes, we create the instance of dependent object (contained object) first then pass it as an argument of the main class constructor. Here, our scenario is Employee HAS-A Address. The Address class object will be termed as the dependent object. Let's see the Address class first:
Address.java
This class contains three properties, one constructor and toString() method to return the values of these object.
  1. package com.javatpoint;  
  2.   
  3. public class Address {  
  4. private String city;  
  5. private String state;  
  6. private String country;  
  7.   
  8. public Address(String city, String state, String country) {  
  9.     super();  
  10.     this.city = city;  
  11.     this.state = state;  
  12.     this.country = country;  
  13. }  
  14.   
  15. public String toString(){  
  16.     return city+" "+state+" "+country;  
  17. }  
  18. }  
Employee.java
It contains three properties id, name and address(dependent object) ,two constructors and show() method to show the records of the current object including the depedent object.
  1. package com.javatpoint;  
  2.   
  3. public class Employee {  
  4. private int id;  
  5. private String name;  
  6. private Address address;//Aggregation  
  7.   
  8. public Employee() {System.out.println("def cons");}  
  9.   
  10. public Employee(int id, String name, Address address) {  
  11.     super();  
  12.     this.id = id;  
  13.     this.name = name;  
  14.     this.address = address;  
  15. }  
  16.   
  17. void show(){  
  18.     System.out.println(id+" "+name);  
  19.     System.out.println(address.toString());  
  20. }  
  21.   
  22. }  

applicationContext.xml
The ref attribute is used to define the reference of another object, such way we are passing the dependent object as an constructor argument.
  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="address" class="com.javatpoint.Address">  
  10. <constructor-arg value="ghaziabad"></constructor-arg>  
  11. <constructor-arg value="UP"></constructor-arg>  
  12. <constructor-arg value="India"></constructor-arg>  
  13. </bean>  
  14.   
  15.   
  16. <bean id="emp" class="com.javatpoint.Employee">  
  17. <constructor-arg value="12" type="int"></constructor-arg>  
  18. <constructor-arg value="Sonoo"></constructor-arg>  
  19. <constructor-arg>  
  20. <ref bean="address"/>  
  21. </constructor-arg>  
  22. </bean>  
  23.   
  24. </beans>  
Test.java
This class gets the bean from the applicationContext.xml file and calls the show 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.core.io.*;  
  6.   
  7. public class Test {  
  8.     public static void main(String[] args) {  
  9.           
  10.         Resource r=new ClassPathResource("applicationContext.xml");  
  11.         BeanFactory factory=new XmlBeanFactory(r);  
  12.           
  13.         Employee s=(Employee)factory.getBean("emp");  
  14.         s.show();  
  15.           
  16.     }  
  17. }  

No comments:

Post a Comment

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