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

在 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有重复共同的值或配置是非常有用的。
作者:逆舟
https://www.cnblogs.com/zy-jiayou/
本博客文章均为作者原创,转载请注明作者和原文链接。
原文地址:https://www.cnblogs.com/zy-jiayou/p/7700296.html