As we discussed earlier a bean definition in configuration metadata can contain constructor arguments, property values etc. Spring framework provides the facility that a child bean definition can inherits configuration data from a parent definition. A child definition can override some values of parent definition or add some others, as required.
Use parent attribute in child definition and pass parent bean into it.
Syntax:
<bean id="parentBeanId" class="TestParentBean"> <property name="name1" value="value1"/> <property name="name2" value="value2"/> </bean> <bean id="childBeanId" class="ChildBeanId" parent="parentBeanId"> <property name="name1" value="value"/> <property name="name3" value="value3"/> </bean> |
Example Explanation:
We have created two beans “HelloWorld” and “HelloJava”. HelloWorld.java have two properties msg1, msg2 and HelloJava.java have three properties msg1, msg2, msg3. In applicationContext.xml HelloWorld bean defined with properties msg1 and msg2. The HelloJava bean use HelloWorld bean as parent and inherits the msg1 and msg2 properties. It use same value for msg1 property as we defined in HelloWorld and overrides the msg2 property. It also add one more property msg3.
Example:
HelloWorld.java
package com.w3schools.business; /** * This class will be used as a bean class. * @author w3schools */ public class HelloWorld { private String msg1; private String msg2; public String getMsg1() { return msg1; } public void setMsg1(String msg1) { this.msg1 = msg1; } public String getMsg2() { return msg2; } public void setMsg2(String msg2) { this.msg2 = msg2; } } |
HelloJava.java
package com.w3schools.business; /** * This class will be used as a bean class. * @author w3schools */ public class HelloJava { private String msg1; private String msg2; private String msg3; public String getMsg1() { return msg1; } public void setMsg1(String msg1) { this.msg1 = msg1; } public String getMsg2() { return msg2; } public void setMsg2(String msg2) { this.msg2 = msg2; } public String getMsg3() { return msg3; } public void setMsg3(String msg3) { this.msg3 = msg3; } } |
applicationContext.java
<?xml version="1.0" encoding="UTF-8"?> <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="helloWorld" class="com.w3schools.business.HelloWorld"> <property name="msg1" value="Java World"/> <property name="msg2" value="World."/> </bean> <bean id="helloJava" class="com.w3schools.business.HelloJava" parent="helloWorld"> <property name="msg2" value="Java"/> <property name="msg3" value="Java."/> </bean> </beans> |
Test.java
package com.w3schools.business; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { //Get ApplicationContext using spring configuration file. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //Get HelloWorld bean object from ApplicationContext instance. HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld"); //Process HelloWorld Object. System.out.println("HelloWorld bean properties: "); System.out.println("Hello " + helloWorld.getMsg1()); System.out.println("Hello " + helloWorld.getMsg2()); //Get HelloJava bean object from ApplicationContext instance. HelloJava helloJava = (HelloJava) context.getBean("helloJava"); //Process HelloJava Object. System.out.println("HelloJava bean properties: "); System.out.println("Hello " + helloJava.getMsg1()); System.out.println("Hello " + helloJava.getMsg2()); System.out.println("Hello " + helloJava.getMsg3()); } } |
Output:
HelloWorld bean properties: Hello Java World Hello World. HelloJava bean properties: Hello Java World Hello Java Hello Java. |