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

Bean定义继承

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

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

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

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

继承例子:

pom.xml:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4.  
  5. <groupId>com.jsoft.testspring</groupId>
  6. <artifactId>testbeandefinition</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9.  
  10. <name>testbeandefinition</name>
  11. <url>http://maven.apache.org</url>
  12.  
  13. <properties>
  14. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15. </properties>
  16.  
  17. <dependencies>
  18. <dependency>
  19. <groupId>junit</groupId>
  20. <artifactId>junit</artifactId>
  21. <version>3.8.1</version>
  22. <scope>test</scope>
  23. </dependency>
  24.  
  25. <!-- Spring Core -->
  26. <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
  27. <dependency>
  28. <groupId>org.springframework</groupId>
  29. <artifactId>spring-core</artifactId>
  30. <version>4.1.4.RELEASE</version>
  31. </dependency>
  32. <!-- Spring Context -->
  33. <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
  34. <dependency>
  35. <groupId>org.springframework</groupId>
  36. <artifactId>spring-context</artifactId>
  37. <version>4.1.4.RELEASE</version>
  38. </dependency>
  39.  
  40. </dependencies>
  41. </project>

HelloWorld.java:

  1. package com.jsoft.testspring.testbeandefinition;
  2.  
  3. public class HelloWorld {
  4. private String messageString1;
  5.  
  6. public void setMessage1(String message){
  7. this.messageString1 = message;
  8. }
  9.  
  10. public void getMessage1(){
  11. System.out.println(this.messageString1);
  12. }
  13.  
  14. private String messageString2;
  15.  
  16. public void getMessage2() {
  17. System.out.println(this.messageString2);
  18. }
  19.  
  20. public void setMessage2(String messageString2) {
  21. this.messageString2 = messageString2;
  22. }
  23. }

HelloIndia.java:

  1. package com.jsoft.testspring.testbeandefinition;
  2.  
  3. public class HelloIndia {
  4. private String messageString1;
  5.  
  6. public void setMessage1(String message){
  7. this.messageString1 = message;
  8. }
  9.  
  10. public void getMessage1(){
  11. System.out.println(this.messageString1);
  12. }
  13.  
  14. private String messageString2;
  15.  
  16. public void getMessage2() {
  17. System.out.println(this.messageString2);
  18. }
  19.  
  20. public void setMessage2(String messageString2) {
  21. this.messageString2 = messageString2;
  22. }
  23.  
  24. private String messageString3;
  25.  
  26. public void getMessage3() {
  27. System.out.println(this.messageString3);
  28. }
  29.  
  30. public void setMessage3(String messageString3) {
  31. this.messageString3 = messageString3;
  32. }
  33. }

beans.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.  
  7. <bean id="helloWorld" class="com.jsoft.testspring.testbeandefinition.HelloWorld">
  8. <property name="Message1" value="Hello World Message1!"></property>
  9. <property name="Message2" value="Hello World Message2!"></property>
  10. </bean>
  11.  
  12. <bean id="helloIndia" class="com.jsoft.testspring.testbeandefinition.HelloIndia" parent="helloWorld">
  13. <property name="Message1" value="Hello India Message1!"></property>
  14. <property name="Message3" value="Hello India Message3!"></property>
  15. </bean>
  16.  
  17. </beans>

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

App.java:

  1. package com.jsoft.testspring.testbeandefinition;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. /**
  7. * Hello world!
  8. *
  9. */
  10. public class App
  11. {
  12. public static void main( String[] args )
  13. {
  14. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
  15.  
  16. HelloWorld helloWorld = (HelloWorld)applicationContext.getBean("helloWorld");
  17. helloWorld.getMessage1();
  18. helloWorld.getMessage2();
  19.  
  20. HelloIndia helloIndia = (HelloIndia)applicationContext.getBean("helloIndia");
  21. helloIndia.getMessage1();
  22. helloIndia.getMessage2();
  23. helloIndia.getMessage3();
  24. }
  25. }

运行结果:

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

模板例子:

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.  
  7. <bean id="beanTeamplate" abstract="true">
  8. <property name="message1" value="Hello World!"/>
  9. <property name="message2" value="Hello Second World!"/>
  10. <property name="message3" value="Namaste India!"/>
  11. </bean>
  12.  
  13. <bean id="helloIndia2" class="com.jsoft.testspring.testbeandefinition.HelloIndia" parent="beanTeamplate">
  14. <property name="Message1" value="Hello India2 Message1!"></property>
  15. <property name="Message3" value="Hello India2 Message3!"></property>
  16. </bean>
  17.  
  18. </beans>

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

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

Spring中Bean的定义继承的更多相关文章

  1. spring中bean的配置详解--定义parent

    在工作中碰到了好多的配置文件,具体来说是spring 中bean配置的parent的配置,搞的我一头雾水,仔细看一下spring中有关bean的配置,剖析一下,具体什么含义! 一.Spring IoC ...

  2. Spring 中bean的作用、定义

    Spring 中bean的作用.定义: 创建一个bean定义,其实质是用该bean定义对应的类来创建真正实例的"配方(recipe)".把bean定义看成一个配方很有意义,它与cl ...

  3. Spring中bean的注入方式

    首先,要学习Spring中的Bean的注入方式,就要先了解什么是依赖注入.依赖注入是指:让调用类对某一接口的实现类的实现类的依赖关系由第三方注入,以此来消除调用类对某一接口实现类的依赖. Spring ...

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

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

  5. 一次性讲清楚spring中bean的生命周期之三:bean是如何实例化的

    在前面的两篇博文<一次性讲清楚spring中bean的生命周期之一:getSingleton方法>和<一次性讲清楚spring中bean的生命周期之二:FactoryBean的前世今 ...

  6. Spring中Bean的作用域、生命周期

                                   Bean的作用域.生命周期 Bean的作用域 Spring 3中为Bean定义了5中作用域,分别为singleton(单例).protot ...

  7. Spring中Bean的实例化

                                    Spring中Bean的实例化 在介绍Bean的三种实例化的方式之前,我们首先需要介绍一下什么是Bean,以及Bean的配置方式. 如果 ...

  8. spring中bean配置和bean注入

    1 bean与spring容器的关系 Bean配置信息定义了Bean的实现及依赖关系,Spring容器根据各种形式的Bean配置信息在容器内部建立Bean定义注册表,然后根据注册表加载.实例化Bean ...

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

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

随机推荐

  1. Luogu P2664 树上游戏 dfs+树上统计

    题目: P2664 树上游戏 分析: 本来是练习点分治的时候看到了这道题.无意中发现题解中有一种方法可以O(N)解决这道题,就去膜拜了一下. 这个方法是,假如对于某一种颜色,将所有这种颜色的点全部删去 ...

  2. <MySQL>入门一 查询 DQL

    1. 数据库表 1.1 员工表 Create Table CREATE TABLE `employees` ( `employee_id` ) NOT NULL AUTO_INCREMENT, `fi ...

  3. 前端用户体验优化: JS & CSS 各类效果代码段

    前言 不定时更新 在线预览 https://zzyper.github.io/opti... 在线预览的源码 https://github.com/zzyper/opt... 部分内容仅兼容webki ...

  4. (转)iOS 属性字符串

    富文本的基本数据类型是NSAttributedString.**属性化字符串**(attributed string)是把属性设置到某些字符上的字符串.属性可以是任何键值对,但是为了实现富文本,则通常 ...

  5. linux下ls出现文件的后缀有@,* ,/之类的解释

    ls -Fafptool*  img_maker*    lzcmp@     lzfgrep@   lzma*         lzmore*         node-pre-gyp@bower@ ...

  6. win7定时关机

    菜单>附件>系统工具>任务计划程序>创建基本任务 alt+r>cmd>shutdown/? 查看相关参数 /l 注销 /s 关机 /r 重启 /g 重启,重启后,重 ...

  7. 关于加号传递到后端会变为空格的c#例子

    参考博客:http://blog.csdn.net/nsdnresponsibility/article/details/50965262 以前在一次传递参数的情况中遇到,特此记录一下. 之前传递的参 ...

  8. Python内置函数6

    Python内置函数6 1.license() 输出当前python 的license信息 A. HISTORY OF THE SOFTWARE ========================== ...

  9. jmeter结果分析详解

    Jmeter测试报表相关参数说明 采用Jmeter测试工具对web系统作的负载测试,得出的响应报表,数据比较难懂,现作一具体说明.以下是在一次具体负载测试中得出的具体数值,测试线程设置情况为:线程数: ...

  10. SQL注入与xss

    1. 什么是SQL注入 所谓SQL注入,就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令.通过递交参数构造巧妙的SQL语句,从而成功获取 ...