在 Spring,继承是用为支持bean设置一个 bean 来分享共同的值,属性或配置。
一个子 bean 或继承的bean可以继承其父 bean 的配置,属性和一些属性。另外,子 Bean 允许覆盖继承的值。
请参见下面的完整的例子来告诉你如何配置 bean 继承在 Spring 中工作。
package com.yiibai.common;

public class Customer {

    private int type;
private String action;
private String Country; //... }
Bean配置文件
<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-2.5.xsd"> <bean id="BaseCustomerMalaysia" class="com.yiibai.common.Customer">
<property name="country" value="Malaysia" />
</bean> <bean id="CustomerBean" parent="BaseCustomerMalaysia">
<property name="action" value="buy" />
<property name="type" value="1" />
</bean> </beans>
以上就是“BaseCustomerMalaysia” Bean中含有的 country 属性的值,而“CustomerBean” Bean 继承其父('BaseCustomerMalaysia')这个值。

执行它

package com.yiibai.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class App
{
public static void main( String[] args )
{
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml"); Customer cust = (Customer)context.getBean("CustomerBean");
System.out.println(cust); }
}

输出结果

Customer [type=1, action=buy, Country=Malaysia]
CustomerBean Bean 只从它的父(“BaseCustomerMalaysia”)继承 country 属性。
继承抽象
在上面的例子中,'BaseCustomerMalaysia' 仍然能够实例化,例如,
Customer cust = (Customer)context.getBean("BaseCustomerMalaysia");
如果你要让这个 bean 作为一个基础模板,不允许别人来实例化它,可以在一个<bean>元素中添加一个“abstract”的属性。 例如
<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-2.5.xsd"> <bean id="BaseCustomerMalaysia" class="com.yiibai.common.Customer" abstract="true">
<property name="country" value="Malaysia" />
</bean> <bean id="CustomerBean" parent="BaseCustomerMalaysia">
<property name="action" value="buy" />
<property name="type" value="1" />
</bean> </beans>
现在,“BaseCustomerMalaysia' Bean是一个纯粹的模板,因为Bean只能继承它,如果试图实例化它,你会遇到以下错误消息。
Customer cust = (Customer)context.getBean("BaseCustomerMalaysia");

org.springframework.beans.factory.BeanIsAbstractException:
Error creating bean with name 'BaseCustomerMalaysia':
Bean definition is abstract

纯继承模板

 
其实,父 bean 是不需要定义类的属性,很多时候,你可能只需要一个共同的属性共享。这里的是一个例子
 
<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-2.5.xsd"> <bean id="BaseCustomerMalaysia" abstract="true">
<property name="country" value="Malaysia" />
</bean> <bean id="CustomerBean" parent="BaseCustomerMalaysia"
class="com.yiibai.common.Customer"> <property name="action" value="buy" />
<property name="type" value="1" />
</bean> </beans>
在这种情况下,“BaseCustomerMalaysia' Bean 是一个纯粹的模板,只分享其 ”country“属性。
覆盖它
但是,仍然可以指定的子bean的新值覆盖继承的值。让我们来看看这个例子
<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-2.5.xsd"> <bean id="BaseCustomerMalaysia" class="com.yiibai.common.Customer" abstract="true">
<property name="country" value="Malaysia" />
</bean> <bean id="CustomerBean" parent="BaseCustomerMalaysia">
<property name="country" value="Japan" />
<property name="action" value="buy" />
<property name="type" value="1" />
</bean> </beans>
在“CustomerBean” Bean只是覆盖父(“BaseCustomerMalaysia”)country 属性,从 ‘Malaysia’ 修改为 ‘Japan’.
Customer [Country=Japan, action=buy, type=1]

总结

Spring bean配置继承是为了避免多个Bean有重复共同的值或配置是非常有用的。

Spring学习(九)-----Spring bean配置继承的更多相关文章

  1. Spring学习笔记之bean配置

    1.命名bean 每个bean都有一个或者多个的的标识符.这些标识符必须在加载他们的容器里边唯一.一个bean经常有且只有一个标识符,但是如果需要超过一个的名字,可以考虑额外的别名. 基于xml的配置 ...

  2. spring学习九 spring aop详解

    本文来自于:https://www.cnblogs.com/jingzhishen/p/4980551.html AOP(Aspect-Oriented Programming,面向方面编程),可以说 ...

  3. Spring学习(十一)-----Spring使用@Required注解依赖检查

    Spring学习(九)-----Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置.在大多数情况下,你只需要确保特定属性已经设置但不是所有属性.. 对于这种 ...

  4. Spring bean配置继承

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

  5. Spring学习(六)-----Spring使用@Autowired注解自动装配

    Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...

  6. Spring学习记录(四)---bean之间的关系:继承、依赖

         继承 这里说的继承和java的继承是不一样的,不是父类子类.但思想很相似,是父bean和子bean 1.父bean是一个实例时.它本身是一个完整的bean 2.父bean是模板,抽象bean ...

  7. Spring学习笔记(2)——Bean的配置

    要使应用程序中的Spring容器成功启动,需要以下三个方面的条件都具备: 1.Spring框架的类包都已经放到应用程序的类路径下 2.应用程序为Spring提供完备的Bean配置信息 3.Bean的类 ...

  8. spring学习总结——装配Bean学习二(JavaConfig装配bean)

    通过Java代码装配bean 前言:上面梳理了通过注解来隐式的完成了组件的扫描和自动装配,下面来学习下如何通过显式的配置的装配bean: 使用场景:比如说,你想要将第三方库中的组件装配到你的应用中,在 ...

  9. Spring学习笔记(3)——Bean的注入方式

    依赖注入 依赖注入支持属性注入.构造函数注入.工厂注入. 属性注入: 属性注入即通过setXxx()方法注入Bean的属性值或依赖对象 属性注入要求Bean提供一个默认的构造函数(无参构造函数),并为 ...

随机推荐

  1. 7、RabbitMQ-主题模式

    1.模式图 发送到主题交换的消息不能具有任意的 routing_key - 它必须是由点分隔的单词列表. 单词可以是任何内容,但通常它们指定与消息相关的一些功能.一些有效的路由键示例:“ stock. ...

  2. Windows下Python3.6安装PIL

    PIL是Python平台事实上的图像处理标准库,需要用到图片的需要导入该模块 一 安装pip https://pip.pypa.io/en/stable/installing/#id8 python ...

  3. alibaba之Nacos

    本文为转载文章 原文链接:https://windmt.com/2018/11/09/intro-to-spring-cloud-alibaba-nacos/ 上个月最后一天的凌晨,Spring Cl ...

  4. rman备份报错,全zero错误处理一例(bbed)

    问题:某数据库在执行rman全备的时候,发现alert日志中有报错,报错提示, file 10,block 305076全部为zero,内容全零,处理过程如下 分析处理: 1. 这个问题可能是 系统或 ...

  5. 记一次 oracle 12.2 RAC : Transaction recovery: lock conflict caught and ignored

    节点一 alert日志: PDB(17):Transaction recovery: lock conflict caught and ignored PDB(17):Transaction reco ...

  6. 随机获取UDID

    (NSString *)uuidString { CFUUIDRef uuid_ref = CFUUIDCreate(NULL); CFStringRef uuid_string_ref= CFUUI ...

  7. RHS 和 LHS

    不成功的的RHS 引用会导致抛出 ReferenceError异常 不成的的LHS 引用会导致自动隐式地创建一个全局变量(非严格模式下)   function foo(a) { var b = a; ...

  8. 使用img标签能使用background-size:conver一样的效果

    使用css img { object-fit: cover; object-position:left center; } 就可以达到 和 background-size:cover; 一样的效果 共 ...

  9. 课时14.DTD文档声明上(掌握)

    1.什么是DTD文档声明? 由于HTML有很多格版本的规范,每个版本的规范之间又又一些差异,所以为了让浏览器能够正确的编译/解析/渲染我们的网页,我们需要在HTML文件的第一行告诉浏览器,我们当前这个 ...

  10. [Err] ERROR: wrong record type supplied in RETURN NEXT

    在写GP 输出不定长列数据表 函数时,报了一个错,百思不得其解.在公司大佬帮助下,知道是什么鬼了.. 先看看例子吧: ---- 函数定义 CREATE OR REPLACE FUNCTION &quo ...