Spring-autowiring-byname

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

春の自動配線「byName」

このモードは、プロパティ名で自動配線を指定します。 Springコンテナは、XML設定ファイルで_auto-wire_属性が_byName_に設定されているBeanを調べます。 次に、構成ファイル内の同じ名前で定義されたBeanとそのプロパティを一致させ、配線しようとします。 一致するものが見つかると、それらのBeanを注入します。 それ以外の場合、Beanは配線されません。

たとえば、設定ファイルでBean定義が_byName_をautowireに設定し、_spellChecker_プロパティが含まれている(つまり、_setSpellChecker _(…​)メソッドがある)場合、SpringCheckerという名前のBean定義を検索し、プロパティを設定します。 それでも、<property>タグを使用して残りのプロパティを配線できます。 次の例で概念を説明します。

動作する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 TextEditor, SpellChecker 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.

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

package com.finddevguides;

public class TextEditor {
   private SpellChecker spellChecker;
   private String name;

   public void setSpellChecker( SpellChecker spellChecker ){
      this.spellChecker = spellChecker;
   }
   public SpellChecker getSpellChecker() {
      return spellChecker;
   }
   public void setName(String name) {
      this.name = name;
   }
   public String getName() {
      return name;
   }
   public void spellCheck() {
      spellChecker.checkSpelling();
   }
}

以下は、別の依存クラスファイル SpellChecker.java の内容です。

package com.finddevguides;

public class SpellChecker {
   public SpellChecker() {
      System.out.println("Inside SpellChecker constructor." );
   }
   public void checkSpelling() {
      System.out.println("Inside checkSpelling." );
   }
}

以下は 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");
      TextEditor te = (TextEditor) context.getBean("textEditor");
      te.spellCheck();
   }
}

以下は、通常の状態の構成ファイル Beans.xml です。

<?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">

   <!-- Definition for textEditor bean -->
   <bean id = "textEditor" class = "com.finddevguides.TextEditor">
      <property name = "spellChecker" ref = "spellChecker"/>
      <property name = "name" value = "Generic Text Editor"/>
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id = "spellChecker" class = "com.finddevguides.SpellChecker"></bean>

</beans>

しかし、自動配線「byName」を使用する場合、XML構成ファイルは次のようになります-

<?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">

   <!-- Definition for textEditor bean -->
   <bean id = "textEditor" class = "com.finddevguides.TextEditor" autowire = "byName">
      <property name = "name" value = "Generic Text Editor"/>
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id = "spellChecker" class = "com.finddevguides.SpellChecker"></bean>

</beans>

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

Inside SpellChecker constructor.
Inside checkSpelling.

link:/cgi-bin/printpage.cgi [__印刷]