以下内容引用自http://wiki.jikexueyuan.com/project/spring/bean-definition-inheritance.html

Bean定义继承

bean定义可以包含很多的配置信息,包括构造函数的参数,属性值,容器的具体信息例如初始化方法,静态工厂方法名,等等。

子bean的定义继承父定义的配置数据。子定义可以根据需要重写一些值,或者添加其他值。

Spring Bean定义的继承与Java类的继承无关,但是继承的概念是一样的。你可以定义一个父bean作为模板和其他子bean就可以从父bean中继承所需的配置。

当你使用基于XML的配置元数据时,通过使用父属性,指定父bean作为该属性的值来表明子bean的定义。

继承例子:

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.testspring</groupId>
<artifactId>testbeandefinition</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>testbeandefinition</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!-- Spring Core -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> <!-- Spring Context -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
</dependencies>
</project>

HelloWorld.java:

package com.jsoft.testspring.testbeandefinition;

public class HelloWorld {
private String messageString1; public void setMessage1(String message){
this.messageString1 = message;
} public void getMessage1(){
System.out.println(this.messageString1);
} private String messageString2; public void getMessage2() {
System.out.println(this.messageString2);
} public void setMessage2(String messageString2) {
this.messageString2 = messageString2;
}
}

HelloIndia.java:

package com.jsoft.testspring.testbeandefinition;

public class HelloIndia {
private String messageString1; public void setMessage1(String message){
this.messageString1 = message;
} public void getMessage1(){
System.out.println(this.messageString1);
} private String messageString2; public void getMessage2() {
System.out.println(this.messageString2);
} public void setMessage2(String messageString2) {
this.messageString2 = messageString2;
} private String messageString3; public void getMessage3() {
System.out.println(this.messageString3);
} public void setMessage3(String messageString3) {
this.messageString3 = messageString3;
}
}

beans.xml:

<?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.xsd"> <bean id="helloWorld" class="com.jsoft.testspring.testbeandefinition.HelloWorld">
<property name="Message1" value="Hello World Message1!"></property>
<property name="Message2" value="Hello World Message2!"></property>
</bean> <bean id="helloIndia" class="com.jsoft.testspring.testbeandefinition.HelloIndia" parent="helloWorld">
<property name="Message1" value="Hello India Message1!"></property>
<property name="Message3" value="Hello India Message3!"></property>
</bean> </beans>

在该配置文件中我们定义有两个属性message1和message2的“helloWorld”bean。然后,使用parent属性把“helloIndia”bean定义为“helloWorld”bean的孩子。这个子bean继承message2的属性,重写message1的属性,并且引入一个属性message3。

App.java:

package com.jsoft.testspring.testbeandefinition;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); HelloWorld helloWorld = (HelloWorld)applicationContext.getBean("helloWorld");
helloWorld.getMessage1();
helloWorld.getMessage2(); HelloIndia helloIndia = (HelloIndia)applicationContext.getBean("helloIndia");
helloIndia.getMessage1();
helloIndia.getMessage2();
helloIndia.getMessage3();
}
}

运行结果:

可以观察到,我们创建“helloIndia”bean的同时并没有传递message2,但是由于Bean定义的继承,所以它传递了message2。

模板例子:

代码逻辑不变,只需要修改beans.xml,不用指定class属性,指定带true值的abstract属性即可。

<?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.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="helloIndia2" class="com.jsoft.testspring.testbeandefinition.HelloIndia" parent="beanTeamplate">
<property name="Message1" value="Hello India2 Message1!"></property>
<property name="Message3" value="Hello India2 Message3!"></property>
</bean> </beans>

父bean自身不能被实例化,因为它是不完整的,而且它也被明确地标记为抽象(abstract)的。当一个定义是抽象的,它仅仅作为一个纯粹的模板bean定义来使用的,充当子定义的父定义使用。

测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test7/testbeandefinition

最新文章

  1. heading python decorator
  2. Nginx-server_name匹配规则
  3. Embedded之memory type
  4. Ajax乱码问题
  5. 【NetOffice Excel】Excel合并单元格【原】
  6. Qt入门(7)——窗口几何结构
  7. c++,纯虚函数与抽象类
  8. Android应用开发学习笔记之绘图
  9. Cocos2d-X中间应用
  10. iOS开发——打开手机相册,获取图片
  11. 【转】jqGrid学习之安装
  12. MySQL锁(MyISAM和InnoDB)
  13. 图表工具--- ECharts.js学习(一) 简单入门
  14. Linux内核线程
  15. vue - 新建一个项目
  16. JVM运行时内存组成分为一些线程私
  17. css的基本定位机制
  18. splash
  19. Linux: Linux C 获取当前系统时间的时间戳(精确到秒、毫秒、微秒) gettimeofday
  20. MyBatis动态sql之${}和#{}区别

热门文章

  1. makeObjectsPerformSelector用法
  2. 用函数式编程思维解析anagrams函数
  3. LIN总线协议
  4. Educational Codeforces Round 32:E. Maximum Subsequence(Meet-in-the-middle)
  5. JavaScript正则表达式-定义
  6. docker.io/centos安装phpstudy
  7. find_element——By 元素定位
  8. Convolution Fundamental I
  9. Leetcode 327.区间和的个数
  10. Leetcode 322.零钱兑换