JAVA后台开发_从入门到精通

9 bean配置继承

2017-07-11  本文已影响10人  笑Skr人啊

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

实体类
package com.gp6.extendsBean;

public class BaseBean {
    private int type;
    private String action;
    private String Country;
    public int getType() {
        return type;
    }
    public void setType(int type) {
        this.type = type;
    }
    public String getAction() {
        return action;
    }
    public void setAction(String action) {
        this.action = action;
    }
    public String getCountry() {
        return Country;
    }
    public void setCountry(String country) {
        Country = country;
    }
}

配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-2.5.xsd">

    <bean id="BaseBean" class="com.gp6.extendsBean.BaseBean">
        <property name="country" value="中国" />
    </bean>

    <bean id="ExtendsBean" parent="BaseBean">
        <property name="action" value="buy" />
        <property name="type" value="1" />
    </bean>
</beans>

以上就是“BaseBean” Bean中含有的 country 属性的值,而“ExtendsBean” Bean 继承其父('BaseBean')这个值。

测试文件
package com.gp6.extendsBean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ExtendsBeanTest {
     public static void main( String[] args ) {
            
         ApplicationContext context = new ClassPathXmlApplicationContext(
                    "com/gp6/extendsBean/ExtendsBean.xml");

         BaseBean baseBean = (BaseBean) context.getBean("ExtendsBean");
         System.out.println(baseBean);
    }
}

执行结果
Paste_Image.png

继承抽象

在上面的例子中,'BaseBean' 仍然能够实例化,例如,

BaseBean baseBean = (BaseBean) context.getBean("BaseBean ");

如果你要让这个 bean 作为一个基础模板,不允许别人来实例化它,可以在一个<bean>元素中添加一个“abstract”的属性。 例如

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-2.5.xsd">

    <bean id="BaseBean" class="com.gp6.extendsBean.BaseBean" abstract="true"> 
        <property name="country" value="中国" />
    </bean>

    <bean id="ExtendsBean" parent="BaseBean">
        <property name="action" value="buy" />
        <property name="type" value="1" />
    </bean>
</beans>

现在,“BaseBean' Bean是一个纯粹的模板,因为Bean只能继承它,如果试图实例化它,你会遇到以下错误消息。

BaseBean baseBean = (BaseBean) context.getBean("BaseBean");


错误:
Error creating bean with name 'BaseBean': Bean definition is abstract

纯继承模板

其实,父 bean 是不需要定义类的属性,很多时候,你可能只需要一个共同的属性共享。这里的是一个例子

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-2.5.xsd">

    <bean id="BaseBean"  abstract="true"> 
        <property name="country" value="中国" />
    </bean>

    <bean id="ExtendsBean" class="com.gp6.extendsBean.BaseBean" parent="BaseBean">
        <property name="action" value="buy" />
        <property name="type" value="1" />
    </bean>
</beans>

在这种情况下,“BaseBean' Bean 是一个纯粹的模板,只分享其 ”country“属性。

覆盖它

但是,仍然可以指定的子bean的新值覆盖继承的值。让我们来看看这个例子

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-2.5.xsd">

    <bean id="BaseBean"  abstract="true"> 
        <property name="country" value="中国" />
    </bean>

    <bean id="ExtendsBean" class="com.gp6.extendsBean.BaseBean" parent="BaseBean">
        <property name="country" value="米国" />
        <property name="action" value="buy" />
        <property name="type" value="1" />
    </bean>
</beans>

在“ExtendsBean” Bean只是覆盖父(“BaseBean”)country 属性,从 ‘中国’ 修改为 ‘米国’.

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-2.5.xsd">

    <bean id="BaseBean"  abstract="true"> 
        <property name="country" value="中国" />
    </bean>

    <bean id="ExtendsBean" class="com.gp6.extendsBean.BaseBean" parent="BaseBean">
        <property name="country" value="米国" />
        <property name="action" value="buy" />
        <property name="type" value="1" />
    </bean>
</beans>
Paste_Image.png

总结

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

上一篇 下一篇

猜你喜欢

热点阅读