在 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. vector详讲(一)

    <vector>头文件里带有两个类型参数的类模板,第一个参数是需要指定的数据类型,第二个是分配器(allocator)类型 template<class T, class Alloc ...

  2. SpringMVC DELETE,PUT请求报错 添加支持Http的DELETE、PUT请求

    SpringMVC删除与修改操作需要用DELETE,PUT请求方式提交. 但要知道浏览器form表单只支持GET与POST请求,而DELETE.PUT等method并不支持. spring3.0添加了 ...

  3. mpvue微信小程序开发随笔

    mpvue上手很快,学习成本低,目前是开源的,适合技术实力不是很强的公司采用. spring boot 做后台,开发效率杠杠的.建议会java的开发尽量使用spring boot 开发,省事. 最近用 ...

  4. 菜鸟程序猿之IDEA快捷键

    Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift+E,最近更改的文件Shift+Click,可以关闭文件Ctrl+[ ...

  5. Web—07-JQuery

    jquery是一个函数库,一个js文件,页面用script标签引入这个js文件就可以使用. <script type="text/javascript" src=" ...

  6. Oracle startup的四个阶段

    shutdown->nomount->mount->open: 1.shutdown:数据库关闭 2.nomount: 1)$ORACLE_HOME/dbs下找初始化参数文件 a.s ...

  7. layDay日期格式不合法报错解决

    报错内容如下: Uncaught TypeError: Cannot read property 'appendChild' of undefined 相关报错内容的行代码如下 即使日期格式拼接正确也 ...

  8. solve the promble of VMware Workstation Ubuntu18.04 ethernet interface losting

    $ ifconfig -aens33: flags=4098<BROADCAST,MULTICAST>  mtu 1500    ether 00:**:**:**:**:**  txqu ...

  9. activemq的高级特性:通配符式分层订阅

    activemq的高级特性之通配符式分层订阅 队列的名称可以分层:aa.bb.cc.dd 可以匹配:aa.bb.cc.dd,aa.*.cc.dd,aa.> *:匹配当前层的内容 >:任何一 ...

  10. MySQL->复制表[20180509]

    MySQL复制表     通常复制表所采用CREATE TABLE .... SELECT 方式将资料复制,但无法将旧表中的索引,约束(除非空以外的)也复制.   完整复制MySQL数据表所需步骤: ...