Springでは、Beanの共通の値、プロパティ、または設定を共有するためにBeanの設定で継承がサポートされています。

子Beanまたは継承されたBeanは、親Beanの設定、プロパティ、およびいくつかの属性を継承できます。さらに、子Beanは継承された値をオーバーライドすることができます。

Beanの設定継承がSpringでどのように機能するかを示すために、以下の完全な例を参照してください。

package com.mkyong.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.mkyong.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プロパティの ‘Malaysia’値が含まれており、 ‘CustomerBean’ Beanはこの値をその親( ‘BaseCustomerMalaysia’)から継承しています。

それを実行します

package com.mkyong.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("SpringBeans.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”&gt

;

<bean id="BaseCustomerMalaysia" class="com.mkyong.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”&gt

;

<bean id="BaseCustomerMalaysia" abstract="true">
    <property name="country" value="Malaysia"/>
</bean>

<bean id="CustomerBean" parent="BaseCustomerMalaysia"
    class="com.mkyong.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”&gt

;

<bean id="BaseCustomerMalaysia" class="com.mkyong.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は、 'Malaysia'から 'Japan'への親( 'BaseCustomerMalaysia')の国のプロパティをオーバーライドします。

Customer[Country=Japan, action=buy, type=1]…​.

結論

Spring Beanの設定継承は、複数のBeanの共通の値や設定の繰り返しを避けるために非常に便利です。

ソースコードをダウンロードする

ダウンロードする – リンク://wp-content/uploads/2010/03/Spring-Bean-Inheritance-Example.zip[Spring-Bean-Inheritance-Example.zip](5 KB)