@Autowiredアノテーションを持つSpring自動配線Bean
最後の
XMLでのSpring自動配線
の例では、現在のSpringコンテナ内のBeanの一致プロパティをautowiredにします。ほとんどの場合、特定のBeanにのみautowiredプロパティが必要な場合があります。
Springでは、
@ Autowired
アノテーションを使用して、setterメソッド、コンストラクタ、またはフィールドのBeanを自動配線できます。さらに、特定のBeanでautowiredプロパティを使用できます。
1.ビーンズ
顧客Beanであり、Bean構成ファイルで宣言されています。後で、 ”
@ Autowired
“を使用して人のbeanを自動配線します。
package com.mkyong.common; public class Customer { //you want autowired this field. private Person person; private int type; private String action; //getter and setter method }
<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="CustomerBean" class="com.mkyong.common.Customer"> <property name="action" value="buy"/> <property name="type" value="1"/> </bean> <bean id="PersonBean" class="com.mkyong.common.Person"> <property name="name" value="mkyong"/> <property name="address" value="address 123"/> <property name="age" value="28"/> </bean> </beans>
2. AutowiredAnnotationBeanPostProcessorを登録します.
-
@ Autowired
を有効にするには、 ‘
AutowiredAnnotationBeanPostProcessor ** ‘を登録する必要があります。これは2通りの方法で行うことができます:
1. <context:annotation-config/>をインクルードします.
Bean設定ファイルにSpringコンテキストと<context:annotation-config/>を追加します。
<beans //... xmlns:context="http://www.springframework.org/schema/context" //... http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> //... <context:annotation-config/> //... </beans>
完全な例、
<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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config/> <bean id="CustomerBean" class="com.mkyong.common.Customer"> <property name="action" value="buy"/> <property name="type" value="1"/> </bean> <bean id="PersonBean" class="com.mkyong.common.Person"> <property name="name" value="mkyong"/> <property name="address" value="address ABC"/> <property name="age" value="29"/> </bean> </beans>
2. AutowiredAnnotationBeanPostProcessorをインクルードします.
Bean設定ファイルに ‘AutowiredAnnotationBeanPostProcessor’を直接組み込みます。
<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 class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <bean id="CustomerBean" class="com.mkyong.common.Customer"> <property name="action" value="buy"/> <property name="type" value="1"/> </bean> <bean id="PersonBean" class="com.mkyong.common.Person"> <property name="name" value="mkyong"/> <property name="address" value="address ABC"/> <property name="age" value="29"/> </bean> </beans>
3. @Autowiredの例
これで、
@ Autowired
を介してautowired Beanを作成することができ、setterメソッド、コンストラクタ、フィールドに適用することができます。
1. @Autowiredセッターメソッド
package com.mkyong.common; import org.springframework.beans.factory.annotation.Autowired; public class Customer { private Person person; private int type; private String action; //getter and setter methods @Autowired public void setPerson(Person person) { this.person = person; } }
2. @Autowiredコンストラクタ
package com.mkyong.common; import org.springframework.beans.factory.annotation.Autowired; public class Customer { private Person person; private int type; private String action; //getter and setter methods @Autowired public Customer(Person person) { this.person = person; } }
3. @Autowiredフィールド
package com.mkyong.common; import org.springframework.beans.factory.annotation.Autowired; public class Customer { @Autowired private Person person; private int type; private String action; //getter and setter methods }
上記の例では、Customerのpersonプロパティに ‘PersonBean’が自動的に呼び出されます。
それを実行します
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(new String[]{"SpringBeans.xml"}); Customer cust = (Customer)context.getBean("CustomerBean"); System.out.println(cust); } }
出力
Customer[action=buy, type=1, person=Person[address=address 123, age=28, name=mkyong]].... === 依存性チェック デフォルトで@Autowiredは、プロパティが正しく配線されていることを確認するために依存関係チェックを実行します。 Springが接続するBeanを見つけられない場合、例外をスローします。これを修正するには、@Autowiredの "** required ** "属性をfalseに設定して、このチェック機能を無効にすることができます。
package com.mkyong.common;
import org.springframework.beans.factory.annotation.Autowired;
public class Customer
{
@Autowired(required=false)
private Person person;
private int type;
private String action;
//getter and setter methods
}
上記の例では、Springが一致するBeanを見つけることができない場合、Personプロパティは設定されていません。 === @Qualifier @Qualifierアノテーションは、どのBeanをフィールドにautowireするかを制御するために使用します。たとえば、2つの類似する人の豆を持つbean構成ファイル。
<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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd”>
;
<context:annotation-config/>
<bean id="CustomerBean" class="com.mkyong.common.Customer"> <property name="action" value="buy"/> <property name="type" value="1"/> </bean>
<bean id="PersonBean1" class="com.mkyong.common.Person"> <property name="name" value="mkyong1"/> <property name="address" value="address 1"/> <property name="age" value="28"/> </bean>
<bean id="PersonBean2" class="com.mkyong.common.Person"> <property name="name" value="mkyong2"/> <property name="address" value="address 2"/> <property name="age" value="28"/> </bean>
</beans>
SpringはどのBeanを接続すべきかを知っていますか? これを修正するには、** @ Qualifier ** を使用して特定のBeanを自動配線することができます。たとえば、
package com.mkyong.common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class Customer
{
@Autowired
@Qualifier(“PersonBean1”)
private Person person;
private int type;
private String action;
//getter and setter methods
}
これは、Bean "PersonBean1"がCustomerのpersonプロパティにautowiredされていることを意味します。この完全な例を読む - link://spring/spring-autowiring-qualifier-example/[Spring Autowiring @Qualifierの例] === 結論 この** @ Autowired ** アノテーションは非常に柔軟で強力であり、Bean設定ファイルの "** autowire ** "属性よりもはるかに優れています。 === ソースコードをダウンロードする ダウンロードする - リンク://wp-content/uploads/2010/03/Spring-Autowired-Annotation-Example.zip[Spring-Autowired-Annotation-Example.zip](6 KB) link://tag/spring/[spring]link://タグ/wiring/[wiring]