W3Schools Learner's Blog

w3Schools programming knowledge summary website

div

12/12/2017

Inheriting Bean in Spring Constructor Injection

Inheriting Bean in Spring Constructor Injection, it's simple.
By using the parent attribute of bean, we can specify the inheritance relation between the beans. In such case, parent bean values will be inherited to the current bean.
Let's see the simple example to inherit the bean.
Employee.java
This class contains three properties, three constructor and show() method to display the values.
  1. package com.javatpoint;  
  2.   
  3. public class Employee {  
  4. private int id;  
  5. private String name;  
  6. private Address address;  
  7. public Employee() {}  
  8.   
  9. public Employee(int id, String name) {  
  10.     super();  
  11.     this.id = id;  
  12.     this.name = name;  
  13. }  
  14. public Employee(int id, String name, Address address) {  
  15.     super();  
  16.     this.id = id;  
  17.     this.name = name;  
  18.     this.address = address;  
  19. }  
  20.   
  21. void show(){  
  22.     System.out.println(id+" "+name);  
  23.     System.out.println(address);  
  24. }  
  25.   
  26. }  
Address.java
  1. package com.javatpoint;  
  2.   
  3. public class Address {  
  4. private String addressLine1,city,state,country;  
  5.   
  6. public Address(String addressLine1, String city, String state, String country) {  
  7.     super();  
  8.     this.addressLine1 = addressLine1;  
  9.     this.city = city;  
  10.     this.state = state;  
  11.     this.country = country;  
  12. }  
  13. public String toString(){  
  14.     return addressLine1+" "+city+" "+state+" "+country;  
  15. }  
  16.   
  17. }  

applicationContext.xml
  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="emp1" class="com.javatpoint.Employee">  
  10. <constructor-arg value="101"></constructor-arg>  
  11. <constructor-arg  value="Sachin"></constructor-arg>  
  12. </bean>  
  13.   
  14. <bean id="address1" class="com.javatpoint.Address">  
  15. <constructor-arg value="21,Lohianagar"></constructor-arg>  
  16. <constructor-arg value="Ghaziabad"></constructor-arg>  
  17. <constructor-arg value="UP"></constructor-arg>  
  18. <constructor-arg value="USA"></constructor-arg>  
  19. </bean>  
  20.   
  21. <bean id="emp2" class="com.javatpoint.Employee" parent="emp1">  
  22. <constructor-arg ref="address1"></constructor-arg>  
  23. </bean>  
  24.   
  25. </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.ClassPathResource;  
  6. import org.springframework.core.io.Resource;  
  7.   
  8. public class Test {  
  9. public static void main(String[] args) {  
  10.     Resource r=new ClassPathResource("applicationContext.xml");  
  11.     BeanFactory factory=new XmlBeanFactory(r);  
  12.       
  13.     Employee e1=(Employee)factory.getBean("emp2");  
  14.     e1.show();  
  15.       
  16. }  
  17. }  

No comments:

Post a Comment

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