以下内容引用自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/beanshttp://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/beanshttp://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

Spring中Bean的定义继承相关推荐

  1. 厉害了,Spring中bean的12种定义方法!

    前言 在庞大的java体系中,spring有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜.我们都知道spring是创建和管理bean的工厂,它提供了多种定义bean的方式,能够满足我们日常工 ...

  2. Spring 中 Bean 的生命周期

    本文作者: 伯乐在线 - yemengying 智商捉鸡?,实在没办法一下子理解Spring IoC和AOP的实现原理,看的闹心也不太懂,所以...决定拆成小的不能在小的一个个问题,一点点啃.今天先来 ...

  3. 如果你每次面试前都要去背一篇Spring中Bean的生命周期,请看完这篇文章

    前言 当你准备去复习Spring中Bean的生命周期的时候,这个时候你开始上网找资料,很大概率会看到下面这张图: 先不论这张图上是否全面,但是就说这张图吧,你是不是背了又忘,忘了又背? 究其原因在于, ...

  4. (转)Spring中Bean的命名问题(id和name区别)及ref和idref之间的区别

    Spring中Bean的命名 1.每个Bean可以有一个id属性,并可以根据该id在IoC容器中查找该Bean,该id属性值必须在IoC容器中唯一: 2.可以不指定id属性,只指定全限定类名,如: & ...

  5. 面试问题:Spring中Bean 的生命周期

    Spring Bean生命周期执行流程 在传统的 Java 应用中,Bean 的生命周期很简单,使用关键字 new 实例化 Bean,当不需要该 Bean 时,由 Java 自动进行垃圾回收. Spr ...

  6. 深究Spring中Bean的生命周期

    前言 这其实是一道面试题,是我在面试百度的时候被问到的,当时没有答出来(因为自己真的很菜),后来在网上寻找答案,看到也是一头雾水,直到看到了<Spring in action>这本书,书上 ...

  7. Spring中Bean管理操作基于XML配置文件方法实现

    Spring中Bean管理操作基于XML配置文件方法实现 基于XML配置文件方式实现 1.基于`xml`方式创建对象 2.基于`xml`方式注入属性 1.创建类,定义属性和对应的set方法 2.在Sp ...

  8. Spring中bean的单例与多例

    Spring中bean的单例与多例 前言 Spring中单例与多例 如何配置单例/多例 单例/多例Bean的使用事项 总结 前言 之前其实已经学习过对于单例模式的使用单例模式讲解,也用过一段时间的Sp ...

  9. spring中Bean的作用范围

    Spring中Bean的作用范围 Singleton:使用该属性定义Bean时,IOC容器仅创建一个Bean实例,IOC容器每次返回的是同一个Bean实例. singleton是默认的作用域,当定义B ...

最新文章

  1. linux 挂载u盘区别不到,linux系统下为什么不能挂载U盘
  2. 短信验证码“最佳实践”
  3. ae toolbarcontrol运行时没有_想办法让AE跑起来
  4. Silverlight 全屏显示
  5. AI朋克致敬MNIST:只用Python和开发板,制作永不重样的时钟
  6. Nodejs学习笔记(十二)--- 定时任务(node-schedule)
  7. Android之输入银行卡号判断属于哪个银行
  8. 微型计算机与接口技术总结,微机原理与接口技术课程总结
  9. Please either set ERLANG_HOME to point to your Erlang installation or place
  10. Access denied for user 'root'@'localhost'. Account is locked
  11. java 怎样判断拼图是否可还原_拼图游戏可解性判断,自动生成可解拼图
  12. 超炫的html5擦除效果,超炫html5效果代码(需浏览器支持)
  13. phpstudy安装php8.0和php8.1的方法(内含VC运行库)
  14. 华为网络配置(IPSec)
  15. 廖雪峰讲python高阶函数求导_廖雪峰python课程笔记
  16. 大数据--Hbase
  17. ant design vue:upload打开选择文件弹框前弹出确认框
  18. 高科技公司全线受损 中国电子企业盼政府救市
  19. 2021-11-26 ubuntu触摸板和小键盘设置
  20. k8s1.19.5单master平滑改造多master

热门文章

  1. c++switch实现猜拳_C语言实现人机猜拳游戏,非常适合C++小白练习的项目!
  2. 阻塞、非阻塞、同步与异步
  3. 华为架构师8年经验谈:从单体架构到微服务的服务化演进之路
  4. [LeetCode] Interleaving String
  5. 关于标准库中的ptr_fun/binary_function/bind1st/bind2nd
  6. 九度 1470 调整方阵
  7. iOS 二叉树相关算法实现
  8. Swift傻傻分不清楚系列(六)集合类型
  9. 数据库系统原理(第三章数据库设计 )
  10. [转]矩阵分解在推荐系统中的应用