As we discussed earlier a bean definition in configuration metadata can contain constructor arguments, property values etc. Spring framework provides the facility to define a bean definition template which can be used by child bean definitions. To define a template remove class attribute and use abstract attribute to true in bean definition.
Use parent attribute in child definition and pass template bean into it.
Syntax:
<bean id="templateId" abstract=”true”> <property name="name1" value="value1"/> <property name="name2" value="value2"/> </bean> <bean id="childBeanId" class="ChildBeanId" parent="templateId"> <property name="name1" value="value"/> <property name="name3" value="value3"/> </bean> |
Note: We can create template with or without using the class attribute. If we create template using class attribute then corresponding class can’t be instantiated.
Example Explanation:
We have created two beans “HelloWorld” and “HelloJava”. HelloWorld.java and HelloJava.java have one common property msg1. In applicationContext.xml we defined a template helloTemplate which define msg1 property. Then HelloJava and HelloWorld bean use this template as parent and inherits the msg1 property.
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; 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; } } |
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="helloTemplate" abstract="true"> <property name="msg1" value="Common World."/> </bean> <bean id="helloWorld" class="com.w3schools.business.HelloWorld" parent="helloTemplate"> <property name="msg2" value="World."/> </bean> <bean id="helloJava" class="com.w3schools.business.HelloJava" parent="helloTemplate"> <property name="msg2" 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()); } } |
Output:
HelloWorld bean properties: Hello Common World. Hello World. HelloJava bean properties: Hello Common World. Hello Java |