Spring攻略学习笔记(13)------继承Bean配置

本文介绍如何在Spring IoC容器中通过Bean继承来减少重复配置,提高代码复用性。介绍了父Bean与子Bean的概念,展示了如何利用抽象Bean进行配置模板的创建,并提供了具体的XML配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 一:知识点

      在Spring IoC容器中配置Bean时,可能拥有一个以上的共享某些公用配置的Bean,比如属性和<bean>元素中的属性。你常常需要为多个Bean重复这些配置。

      Spring允许你提取公用的Bean配置组成一个父Bean。从父Bean继承来的Bean称作子Bean。子Bean从父Bean继承Bean配置,包括Bean属性和<bean>元素中的属性,避免重复配置。子Bean在必要时也可以覆盖继承的配置。

      父Bean可以作为配置模板,也可以同时作为Bean的一个实例。如果你希望父Bean只作为模板而不能检索,必须将abstract设置为true,要求Spring不要实例化这个Bean。

      必须注意,并不是所有在父<bean>元素中定义的属性都将被继承。具体可参见Spring文档关于Bean继承的内容。

二、代码示例:

        Bean配置文件:

<bean id="sequenceGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.SequenceGenerator" >
   <property name="initial" value="100000" />
   <property name="suffix" value="A" />
   <property name="prefixGenerator" ref="datePrefixGenerator" />
</bean>
		
<bean id="sequenceGenerator1" class="com.jackie.codeproject.springrecipesnote.springioc.SequenceGenerator" >
   <property name="initial" value="100000" />
   <property name="suffix" value="A" />
   <property name="prefixGenerator" ref="datePrefixGenerator" />
</bean>
		
<bean id="datePrefixGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.DatePrefixGenerator">
   <property name="pattern" value="yyyyMMdd" />
</bean> 

       为了避免重复相同的属性,可以用这些属性集声明一个基序列生成器。然后,两个序列生成器可以从这个基序列生成器中继承,这样它们自动拥有哪些属性集。如果子Bean和父Bean的class属性相同,就不需要指定。

 

 

<bean id="baseSequenceGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.SequenceGenerator" >
    <property name="initial" value="100000" />
    <property name="suffix" value="A" />
    <property name="prefixGenerator" ref="datePrefixGenerator" />
</bean>
		
<bean id="sequenceGenerator" parent="baseSequenceGenerator" />
			
<bean id="sequenceGenerator1" parent="baseSequenceGenerator" />

       继承的属性可以由子Bean覆盖。例如,可以添加不同初始值的子序列生成器。

 

 

<bean id="baseSequenceGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.SequenceGenerator" >
   <property name="initial" value="100000" />
   <property name="suffix" value="A" />
   <property name="prefixGenerator" ref="datePrefixGenerator" />
</bean>
		
<bean id="sequenceGenerator2" parent="baseSequenceGenerator" >
   <property name="initial" value="200000"></property>
</bean>

       现在,基序列生成器Bean可以恢复为Bean实例使用。如果希望它仅作为模板,必须将abstract属性设置为true,则Spring不会实例化这个Bean,否则会抛异常。

<bean id="baseSequenceGenerator" abstract="true" class="com.jackie.codeproject.springrecipesnote.springioc.SequenceGenerator" >
   <property name="initial" value="100000" />
   <property name="suffix" value="A" />
   <property name="prefixGenerator" ref="datePrefixGenerator" />
</bean>

       你也可以忽略父Bean的类,让子Bean指定自己的类,特别是在父Bean与子Bean不在同一类层次结构但是共享同名属性的时候。在这种情况下,父Bean的abstract属性必须设置为true,因为父Bean不能实例化。例如,添加另一个也有initial属性的ReverseGenerator类。

 

 

public class ReverseGenerator {

    private int initial;

    public void setInitial(int initial) {
        this.initial = initial;
    }
}

       现在,SequenceGenerator和ReverseGenerator不会扩展相同的基类,即他们不在相同的类层次机构中,但是具有同名的属性:initial。为了提取公共的initial属性,需要一个没有定义class属性的父Bean--------baseGenerator。

 

 

<bean id="baseGenerator" abstract="true">
   <property name="initial" value="100000" />
</bean>
		
<bean id="baseSequenceGenerator" abstract="true" parent="baseGenerator" >
   <property name="suffix" value="A" />
   <property name="prefixGenerator" ref="datePrefixGenerator" />
</bean>
		
<bean id="reverseGenerator" parent="baseGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.ReverseGenerator" />
		
<bean id="sequenceGenerator" parent="baseSequenceGenerator" />
			
<bean id="sequenceGenerator1" parent="baseSequenceGenerator" />
		
<bean id="sequenceGenerator2" parent="baseSequenceGenerator" />



 

 

转载于:https://www.cnblogs.com/jiangu66/archive/2013/04/23/3037524.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值