Spring-autowired-annotation

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

Spring @Autowiredアノテーション

*@ Autowired* アノテーションは、自動配線をどこでどのように実行するかについて、よりきめ細かな制御を提供します。 @Autowiredアノテーションを使用すると、@ Requiredアノテーション、コンストラクター、プロパティ、または任意の名前や複数の引数を持つメソッドのように、セッターメソッドでBeanを自動接続できます。

セッターメソッドの@Autowired

セッターメソッドで @ Autowired アノテーションを使用して、XML構成ファイルの<property>要素を削除できます。 Springは、セッターメソッドで使用される@Autowiredアノテーションを見つけると、メソッドで byType 自動配線を実行しようとします。

作業中のEclipse IDEを用意し、次の手順に従ってSpringアプリケーションを作成します。

Step 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;

import org.springframework.beans.factory.annotation.Autowired;

public class TextEditor {
   private SpellChecker spellChecker;

   @Autowired
   public void setSpellChecker( SpellChecker spellChecker ){
      this.spellChecker = spellChecker;
   }
   public SpellChecker getSpellChecker( ) {
      return spellChecker;
   }
   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"
   xmlns:context = "http://www.springframework.org/schema/context"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd">

   <context:annotation-config/>

   <!-- Definition for textEditor bean without constructor-arg  -->
   <bean id = "textEditor" class = "com.finddevguides.TextEditor">
   </bean>

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

</beans>

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

Inside SpellChecker constructor.
Inside checkSpelling.

プロパティの@Autowired

プロパティで @ Autowired アノテーションを使用して、セッターメソッドを取り除くことができます。 <property>を使用して自動配線されたプロパティの値を渡す場合、Springはそれらのプロパティに渡された値または参照を自動的に割り当てます。 したがって、プロパティで@Autowiredを使用すると、 TextEditor.java ファイルは次のようになります-

package com.finddevguides;

import org.springframework.beans.factory.annotation.Autowired;

public class TextEditor {
   @Autowired
   private SpellChecker spellChecker;

   public TextEditor() {
      System.out.println("Inside TextEditor constructor." );
   }
   public SpellChecker getSpellChecker( ){
      return spellChecker;
   }
   public void spellCheck(){
      spellChecker.checkSpelling();
   }
}

以下は、構成ファイル 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"
   xmlns:context = "http://www.springframework.org/schema/context"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd">

   <context:annotation-config/>

   <!-- Definition for textEditor bean -->
   <bean id = "textEditor" class = "com.finddevguides.TextEditor">
   </bean>

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

</beans>

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

Inside TextEditor constructor.
Inside SpellChecker constructor.
Inside checkSpelling.

コンストラクターの@Autowired

@Autowiredをコンストラクターにも適用できます。 コンストラクター@Autowiredアノテーションは、XMLファイルでBeanを構成するときに<constructor-arg>要素が使用されていない場合でも、Beanの作成時にコンストラクターが自動配線されることを示します。 次の例を確認してみましょう。

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

package com.finddevguides;

import org.springframework.beans.factory.annotation.Autowired;

public class TextEditor {
   private SpellChecker spellChecker;

   @Autowired
   public TextEditor(SpellChecker spellChecker){
      System.out.println("Inside TextEditor constructor." );
      this.spellChecker = spellChecker;
   }
   public void spellCheck(){
      spellChecker.checkSpelling();
   }
}

以下は、構成ファイル 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"
   xmlns:context = "http://www.springframework.org/schema/context"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd">

   <context:annotation-config/>

   <!-- Definition for textEditor bean without constructor-arg  -->
   <bean id = "textEditor" class = "com.finddevguides.TextEditor">
   </bean>

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

</beans>

上記の2つのソースおよびBean構成ファイルの変更が完了したら、アプリケーションを実行します。 アプリケーションに問題がない場合、次のメッセージが出力されます。

Inside SpellChecker constructor.
Inside TextEditor constructor.
Inside checkSpelling.

(必須= false)オプション付きの@Autowired

デフォルトでは、@ Autowiredアノテーションは、@ Requiredアノテーションと同様に依存関係が必要であることを示しますが、@ Autowiredで*(required = false)*オプションを使用すると、デフォルトの動作をオフにできます。

次の例は、年齢プロパティに値を渡さなくても機能しますが、名前プロパティを要求します。 このサンプルは、 Student.java ファイルのみが変更されていることを除いて@Requiredアノテーションのサンプルに似ているため、自分で試すことができます。

package com.finddevguides;

import org.springframework.beans.factory.annotation.Autowired;

public class Student {
   private Integer age;
   private String name;

   @Autowired(required=false)
   public void setAge(Integer age) {
      this.age = age;
   }
   public Integer getAge() {
      return age;
   }

   @Autowired
   public void setName(String name) {
      this.name = name;
   }
   public String getName() {
      return name;
   }
}