在 Spring,继承是用为支持bean设置一个 bean 来分享共同的值,属性或配置。
一个子 bean 或继承的bean可以继承其父 bean 的配置,属性和一些属性。另外,子 Bean 允许覆盖继承的值。
请参见下面的完整的例子来告诉你如何配置 bean 继承在 Spring 中工作。
  1. package com.yiibai.common;
  2.  
  3. public class Customer {
  4.  
  5. private int type;
  6. private String action;
  7. private String Country;
  8.  
  9. //...
  10.  
  11. }
Bean配置文件
  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
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5.  
  6. <bean id="BaseCustomerMalaysia" class="com.yiibai.common.Customer">
  7. <property name="country" value="Malaysia" />
  8. </bean>
  9.  
  10. <bean id="CustomerBean" parent="BaseCustomerMalaysia">
  11. <property name="action" value="buy" />
  12. <property name="type" value="1" />
  13. </bean>
  14.  
  15. </beans>
以上就是“BaseCustomerMalaysia” Bean中含有的 country 属性的值,而“CustomerBean” Bean 继承其父('BaseCustomerMalaysia')这个值。

执行它

  1. package com.yiibai.common;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. public class App
  7. {
  8. public static void main( String[] args )
  9. {
  10. ApplicationContext context =
  11. new ClassPathXmlApplicationContext("applicationContext.xml");
  12.  
  13. Customer cust = (Customer)context.getBean("CustomerBean");
  14. System.out.println(cust);
  15.  
  16. }
  17. }

输出结果

  1. Customer [type=1, action=buy, Country=Malaysia]
CustomerBean Bean 只从它的父(“BaseCustomerMalaysia”)继承 country 属性。
继承抽象
在上面的例子中,'BaseCustomerMalaysia' 仍然能够实例化,例如,
  1. Customer cust = (Customer)context.getBean("BaseCustomerMalaysia");
如果你要让这个 bean 作为一个基础模板,不允许别人来实例化它,可以在一个<bean>元素中添加一个“abstract”的属性。 例如
  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
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5.  
  6. <bean id="BaseCustomerMalaysia" class="com.yiibai.common.Customer" abstract="true">
  7. <property name="country" value="Malaysia" />
  8. </bean>
  9.  
  10. <bean id="CustomerBean" parent="BaseCustomerMalaysia">
  11. <property name="action" value="buy" />
  12. <property name="type" value="1" />
  13. </bean>
  14.  
  15. </beans>
现在,“BaseCustomerMalaysia' Bean是一个纯粹的模板,因为Bean只能继承它,如果试图实例化它,你会遇到以下错误消息。
  1. Customer cust = (Customer)context.getBean("BaseCustomerMalaysia");
  1. org.springframework.beans.factory.BeanIsAbstractException:
  2. Error creating bean with name 'BaseCustomerMalaysia':
  3. Bean definition is abstract
纯继承模板
其实,父 bean 是不需要定义类的属性,很多时候,你可能只需要一个共同的属性共享。这里的是一个例子
  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
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5.  
  6. <bean id="BaseCustomerMalaysia" abstract="true">
  7. <property name="country" value="Malaysia" />
  8. </bean>
  9.  
  10. <bean id="CustomerBean" parent="BaseCustomerMalaysia"
  11. class="com.yiibai.common.Customer">
  12.  
  13. <property name="action" value="buy" />
  14. <property name="type" value="1" />
  15. </bean>
  16.  
  17. </beans>
在这种情况下,“BaseCustomerMalaysia' Bean 是一个纯粹的模板,只分享其 ”country“属性。
覆盖它
但是,仍然可以指定的子bean的新值覆盖继承的值。让我们来看看这个例子
  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
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5.  
  6. <bean id="BaseCustomerMalaysia" class="com.yiibai.common.Customer" abstract="true">
  7. <property name="country" value="Malaysia" />
  8. </bean>
  9.  
  10. <bean id="CustomerBean" parent="BaseCustomerMalaysia">
  11. <property name="country" value="Japan" />
  12. <property name="action" value="buy" />
  13. <property name="type" value="1" />
  14. </bean>
  15.  
  16. </beans>
在“CustomerBean” Bean只是覆盖父(“BaseCustomerMalaysia”)country 属性,从 ‘Malaysia’ 修改为 ‘Japan’.
  1. Customer [Country=Japan, action=buy, type=1]

总结

Spring bean配置继承是为了避免多个Bean有重复共同的值或配置是非常有用的。
 
http://www.yiibai.com/spring/spring-bean-configuration-inheritance.html

Spring bean配置继承的更多相关文章

  1. Spring学习(九)-----Spring bean配置继承

    在 Spring,继承是用为支持bean设置一个 bean 来分享共同的值,属性或配置. 一个子 bean 或继承的bean可以继承其父 bean 的配置,属性和一些属性.另外,子 Bean 允许覆盖 ...

  2. spring Bean配置的三种形式

    Spring Bean配置有以下三种形式: 传统的xml配置 Spring 2.5 以后新增注解配置 Spring3.0以后新增JavaConfig 1. 传统的xml配置 <?xml vers ...

  3. [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  4. Spring Bean配置

    Spring 是什么 •Spring 为简化企业级应用开发而生. 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能. •Spring 是一个 IOC(DI) 和 ...

  5. Spring Bean 定义继承

    本例子源于:W3CSchool,在此作记录 bean 定义可以包含很多的配置信息,包括构造函数的参数,属性值,容器的具体信息例如初始化方法,静态工厂方法名,等等. 子 bean 的定义继承父定义的配置 ...

  6. spring—Bean配置

    Spring是一个开源的框架,其目标是简化java的开发.为了降低Java开发的复杂性,Spring有如下的特性: >> 基于POJO的轻量级和最小侵入性编程 >> 通过依赖注 ...

  7. spring学习五:Spring Bean 定义继承

    Bean 定义继承 bean 定义可以包含很多的配置信息,包括构造函数的参数,属性值,容器的具体信息例如初始化方法,静态工厂方法名,等等. 子 bean 的定义继承父定义的配置数据.子定义可以根据需要 ...

  8. [原创]java WEB学习笔记101:Spring学习---Spring Bean配置:IOC容器中bean的声明周期,Bean 后置处理器

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  9. [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

随机推荐

  1. Java Eclipse 配置

    1.清除多余记录 最近用eclipse打包jar的时候,需要指定一个main函数.需要先运行一下main函数,eclipse的Runnable JAR File Specification 下的Lau ...

  2. 1、CentOS 6 安装GitLab

    1.安装和配置必需的依赖项 在CentOS上将系统防火墙打开HTTP和SSH访问. sudo yum install -y curl policycoreutils-python openssh-se ...

  3. 转:google测试分享-SET和TE

    原文:  http://blog.sina.com.cn/s/blog_6cf812be0102vbnb.html 前端时间看了google测试之道,收获了一些,在此总结下并打算写一个系列blog,顺 ...

  4. Debian系网络配置 /etc/network/interfaces

    说Debian系的网卡配置跟Redhat系很不一样,Redhat是放在/etc/sysconfig/network-scripts目录下面的一大堆文件里面,要修改?你一个一个文件来过吧.Debian系 ...

  5. Java web项目中新建maven项目出现的问题

    1.首先新建maven项目,新建Maven时出现了版本问题,报错 第一个错误:jdk版本与project facets不匹配(大概是这样,忘记截图了),那么解决办法是: 在项目右击--->Pro ...

  6. POJ 2349 Arctic Network(最小生成树+求第k大边)

    题目链接:http://poj.org/problem?id=2349 题目大意:有n个前哨,和s个卫星通讯装置,任何两个装了卫星通讯装置的前哨都可以通过卫星进行通信,而不管他们的位置. 否则,只有两 ...

  7. 今日头条、Face++开发岗面经

    今日头条.Face++开发岗面经 [头条] 两个栈实现一个队列.怎么优化 数组每一个元素找出数组右边第一个大于自己的数 实现LRU TCP四次握手 滑动窗口.窗口大小 线程与进程区别 什么是线程安全 ...

  8. scrapy保存csv文件有空行的解决方案

    比如现在我有一个名为test的爬虫,运行爬虫后将结果保存到test.csv文件 默认情况下,我执行scrapy crawl test -o test.csv ,得到的结果可能就是下面这种情况,每两行中 ...

  9. springMVC返回modelmap跟new hashMap的区别

    今天遇到了个坑. 在springboot中 平时写接口,newHashMap,@ResponseBody 返回json对象,没什么问题 @RequestMapping("url") ...

  10. onvif 开发中的一些重要函数介绍

    ➤soap结构中count(soap->count)成员 soap结构中count(soap->count)成员记录的是http协议中Content-Length的数值. ➤keep_al ...