[转载]Spring Bean Definition Inheritance

本文介绍 Spring 框架中 Bean 的继承机制及如何使用 Bean 定义模板简化配置。通过示例展示了如何创建可继承属性的子 Bean,并介绍了抽象 Bean 的用法。

Following is the configuration file Beans.xml where we defined "helloWorld" bean which has two properties message1 and message2. Next "helloIndia" bean has been defined as a child of "helloWorld" bean by using parent attribute. The child bean inherits message2 property as is, and overrides message1 property and introduces one more property message3. 

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-
 4 3.0.xsd">
 5 <bean id="helloWorld" class="com.tutorialspoint.HelloWorld"> <property name="message1" value="Hello World!"/>
 6 <property name="message2" value="Hello Second World!"/>
 7 </bean>
 8 <bean id="helloIndia" class="com.tutorialspoint.HelloIndia" parent="helloWorld">
 9 <property name="message1" value="Hello India!"/> <property name="message3" value="Namaste India!"/>
10    </bean>
11 </beans>

Here is the content of HelloWorld.java file: 

package com.tutorialspoint;
public class HelloWorld { private String message1; private String message2;
public void setMessage1(String message){ this.message1 = message;
}
public void setMessage2(String message){ this.message2 = message;
}
public void getMessage1(){
System.out.println("World Message1 : " + message1);
}
public void getMessage2(){
System.out.println("World Message2 : " + message2);
} }

Here is the content of HelloIndia.java file: 

package com.tutorialspoint;
public class HelloIndia { 
private String message1; 
private String message2; 
private String message3;
public void setMessage1(String message){ this.message1 = message;
}
public void setMessage2(String message){ this.message2 = message;
}
public void setMessage3(String message){ this.message3 = message;
}
public void getMessage1(){
System.out.println("India Message1 : " + message1);}
public void getMessage2(){
System.out.println("India Message2 : " + message2);}
public void getMessage3(){
System.out.println("India Message3 : " + message3);}
}

Following is the content of the MainApp.java file: 

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
      HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
      objA.getMessage1();
      objA.getMessage2();
      HelloIndia objB = (HelloIndia) context.getBean("helloIndia");
      objB.getMessage1();
      objB.getMessage2();
      objB.getMessage3();
} }


Once you are done with creating source and bean configuration files, let us run the application. If everything is fine with your application, this will print the following message:

World Message1 : Hello World!
World Message2 : Hello Second World!
India Message1 : Hello India!
India Message2 : Hello Second World!
India Message3 : Namaste India!

If you observed here, we did not pass message2 while creating "helloIndia" bean, but it got passed because of Bean Definition Inheritance.

 

Bean Definition Template 

You can create a Bean definition template which can be used by other child bean definitions without putting much effort. While defining a Bean Definition Template, you should not specifyclassattribute and should specifyabstractattribute with a value oftrueas shown below: 

<?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="beanTeamplate" abstract="true">
<property name="message1" value="Hello World!"/> <property name="message2" value="Hello Second World!"/> <property name="message3" value="Namaste India!"/>
</bean>
<bean id="helloIndia" class="com.tutorialspoint.HelloIndia" parent="beanTeamplate">
<property name="message1" value="Hello India!"/> <property name="message3" value="Namaste India!"/>
   </bean>
</beans>

The parent bean cannot be instantiated on its own because it is incomplete, and it is also explicitly marked as abstract. When a definition is abstract like this, it is usable only as a pure template bean definition that serves as a parent definition for child definitions. 

转载于:https://www.cnblogs.com/reynold-lei/p/3545300.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值