Spring bean配置繼承

在 Spring,繼承是用為支持bean設置一個 bean 來分享共同的值,屬性或配置。
一個子 bean 或繼承的bean可以繼承其父 bean 的配置,屬性和一些屬性。另外,子 Bean 允許覆蓋繼承的值。
請參見下麵的完整的例子來告訴你如何配置 bean 繼承在 Spring 中工作。
package com.xuhuhu.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.xuhuhu.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.xuhuhu.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.xuhuhu.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.xuhuhu.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.xuhuhu.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有重複共同的值或配置是非常有用的。

上一篇: Spring PropertyPlaceholderConfigurer實例 下一篇: Spring依賴檢查