加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

【Spring学习10】依赖配置:bean的父子继承

发布时间:2020-12-14 01:38:23 所属栏目:百科 来源:网络整理
导读:Spring Bean的父子继承主要是为了统一定义Spring Bean的公共属性、作业范围scope,并避免了冗余和修改的繁琐。 beans bean id = "notify" class = "twm.spring.start.NotifyServiceByCellPhoneImpl" / bean id = "parent" abstract = "true" class = "exampl

Spring Bean的父子继承主要是为了统一定义Spring Bean的公共属性、作业范围scope,并避免了冗余和修改的繁琐。

<beans>
<bean id="notify" class="twm.spring.start.NotifyServiceByCellPhoneImpl" />
    <bean id="parent" abstract="true" class="example.ComplexObject">
      <property name="notifyservice" ref="notify"/>
        <property name="adminEmails">
            <props>
                <prop key="administrator">administrator@example.com</prop>
                <prop key="support">support@example.com</prop>
            </props>
        </property>
    </bean>

    <bean id="child" parent="parent">
        <property name="adminEmails">
            <props merge="true">
                <prop key="sales">sales@example.com</prop>
                <prop key="support">support@example.co.uk</prop>
            </props>
        </property>
    </bean>
<beans>

可以看到child bean无需声明class,它用parent声明了父对象,这样就继承了父对象的class声明,以及notifyservice属性值,adminEmails的值也会从父对象继承并合并。

如果父对象声明了属性abstract="true",则不能实例化,因为它是抽象的bean。容器内部的preInstantiateSingletons()方法会忽略抽象bean。在作为子bean的纯模板时,非常有用。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读