Spring 专题
专题目录
您的位置:java > Spring专题 > Spring bean配置继承
Spring bean配置继承
作者:--    发布时间:2019-11-20
在 spring,继承是用为支持bean设置一个 bean 来分享共同的值,属性或配置。
一个子 bean 或继承的bean可以继承其父 bean 的配置,属性和一些属性。另外,子 bean 允许覆盖继承的值。
请参见下面的完整的例子来告诉你如何配置 bean 继承在 spring 中工作。
package com.h3.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.h3.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.h3.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.h3.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.h3.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.h3.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有重复共同的值或配置是非常有用的。

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册