Spring-bean-definition-inheritance

提供:Dev Guides
移動先:案内検索

Spring-Bean定義の継承

Bean定義には、コンストラクター引数、プロパティ値、および初期化メソッド、静的ファクトリーメソッド名などのコンテナー固有の情報など、多くの構成情報を含めることができます。

子Bean定義は、親定義から構成データを継承します。 子定義は、必要に応じて一部の値をオーバーライドしたり、他の値を追加したりできます。

Spring Bean定義の継承はJavaクラスの継承とは関係ありませんが、継承の概念は同じです。 親Bean定義をテンプレートとして定義でき、他の子Beanは必要な構成を親Beanから継承できます。

XMLベースの構成メタデータを使用する場合、この属性の値として親Beanを指定して、 parent 属性を使用して、子Bean定義を示します。

動作するEclipse IDEを用意し、次の手順を実行してSpringアプリケーションを作成します。

Steps Description
1 Create a project with a name SpringExample and create a package com.finddevguides under the *src *folder in the created project.
2 Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter.
3 Create Java classes HelloWorld, HelloIndia and MainApp under the com.finddevguides package.
4 Create Beans configuration file Beans.xml under the* src* folder.
5 The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.

以下は、2つのプロパティ_message1_および_message2_を持つ「helloWorld」Beanを定義した構成ファイル Beans.xml です。 次の「helloIndia」Beanは、 parent 属性を使用して「helloWorld」Beanの子として定義されています。 子Beanは_message2_プロパティをそのまま継承し、_message1_プロパティをオーバーライドして、もう1つのプロパティ_message3_を導入します。

<?xml version = "1.0" encoding = "UTF-8"?>

<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-3.0.xsd">

   <bean id = "helloWorld" class = "com.finddevguides.HelloWorld">
      <property name = "message1" value = "Hello World!"/>
      <property name = "message2" value = "Hello Second World!"/>
   </bean>

   <bean id ="helloIndia" class = "com.finddevguides.HelloIndia" parent = "helloWorld">
      <property name = "message1" value = "Hello India!"/>
      <property name = "message3" value = "Namaste India!"/>
   </bean>
</beans>
*HelloWorld.java* ファイルの内容は次のとおりです-
package com.finddevguides;

public class HelloWorld {
   private String message1;
   private String message2;

   public void setMessage1(String message){
      this.message1 = message;
   }
   public void setMessage2(String message){
      this.message2 = message;
   }
   public void getMessage1(){
      System.out.println("World Message1 : " + message1);
   }
   public void getMessage2(){
      System.out.println("World Message2 : " + message2);
   }
}
*HelloIndia.java* ファイルの内容は次のとおりです-
package com.finddevguides;

public class HelloIndia {
   private String message1;
   private String message2;
   private String message3;

   public void setMessage1(String message){
      this.message1 = message;
   }
   public void setMessage2(String message){
      this.message2 = message;
   }
   public void setMessage3(String message){
      this.message3 = message;
   }
   public void getMessage1(){
      System.out.println("India Message1 : " + message1);
   }
   public void getMessage2(){
      System.out.println("India Message2 : " + message2);
   }
   public void getMessage3(){
      System.out.println("India Message3 : " + message3);
   }
}

以下は MainApp.java ファイルの内容です-

package com.finddevguides;

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

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");

      HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
      objA.getMessage1();
      objA.getMessage2();

      HelloIndia objB = (HelloIndia) context.getBean("helloIndia");
      objB.getMessage1();
      objB.getMessage2();
      objB.getMessage3();
   }
}

ソースおよびBean構成ファイルの作成が完了したら、アプリケーションを実行しましょう。 すべてがあなたのアプリケーションでうまくいけば、それは次のメッセージを印刷します-

World Message1 : Hello World!
World Message2 : Hello Second World!
India Message1 : Hello India!
India Message2 : Hello Second World!
India Message3 : Namaste India!

ここで確認した場合、「helloIndia」Beanの作成中にmessage2を渡しませんでしたが、Bean Definition Inheritanceのために渡されました。

Bean定義テンプレート

Bean定義テンプレートを作成できます。このテンプレートは、他の子Bean定義であまり労力をかけずに使用できます。 Bean定義テンプレートを定義する際、 class 属性を指定せず、 abstract 属性を指定し、次のコードスニペットに示すように、 true の値で抽象属性を指定する必要があります-

<?xml version = "1.0" encoding = "UTF-8"?>

<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-3.0.xsd">

   <bean id = "beanTeamplate" abstract = "true">
      <property name = "message1" value = "Hello World!"/>
      <property name = "message2" value = "Hello Second World!"/>
      <property name = "message3" value = "Namaste India!"/>
   </bean>

   <bean id = "helloIndia" class = "com.finddevguides.HelloIndia" parent = "beanTeamplate">
      <property name = "message1" value = "Hello India!"/>
      <property name = "message3" value = "Namaste India!"/>
   </bean>

</beans>

親Beanは不完全であるため、単独ではインスタンス化できません。また、_abstract_として明示的にマークされています。 定義がこのように抽象的である場合、子定義の親定義として機能する純粋なテンプレートBean定義としてのみ使用できます。