1

Spring で @Autowired アノテーションをチェックするための小さなコードを作成しました。これが私のコードです。

public class Address 
{
    private String street;
    private String City;
    private String State;
    private String Country;

    /* getter setter here */       
}

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

public class Customer 
{
    private String name;
    private Address address;

    // other getter setter here 

    @Autowired
    public void setAddress(Address address)
    {
        this.address = address;
    }
}

および springexample.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
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <bean id="address1" class="org.springexamples.Address">
            <property name="street" value="vihar" />
            <property name="city" value="dehradun" />
            <property name="state" value="Uttarakhand" />
            <property name="country" value="India" />
    </bean>
    <bean id="addres2" class="org.springexamples.Address">
            <property name="street" value="triveni vihar" />
            <property name="city" value="dehradun" />
            <property name="state" value="Uttarakhand" />
            <property name="country" value="India" />
    </bean>
    <bean id="customer" class="org.springexamples.Customer">
        <property name="name" value="deepak" />
    </bean>
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
</beans>

そしてメインクラス

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

public class AutowiredQualifierTest 
{
    public static void main(String[] args)
    {
        ApplicationContext context= new ClassPathXmlApplicationContext("springexample.xml");
        Customer cust = (Customer)context.getBean("customer");
        System.out.println(cust.getName() + "  " + cust.getAddress().getStreet());
    }
}

理想的には、同じタイプの 2 つの Bean が存在するため例外を表示する必要がありますが、id="address1" の Bean を選択しているため、この Bean の動作が得られます。

4

3 に答える 3

2

タイプ [org.springexamples.Address] の一意の Bean は定義されていません: 単一の一致する Bean が予想されますが、2 が見つかりました: [address1, address2]..例外が発生しています..そして、それは明らかに正しいと言っています..したがって、@Qualifier を使用する必要があります(your_required_beanid)

例: @Qualifier("Address1") の場合、id は address1 Bean と見なされます

于 2013-01-31T07:27:29.770 に答える
1

例外がスローされます。私はあなたが何か間違ったことをしているに違いない。私はあなたのコードを100パーセント確実にするためにテストしました、そしてそれは例外を投げます。

ドキュメントを見てみましょう:

3.4.5.1自動配線の制限と欠点

コンテナ内の複数のBean定義は、自動配線されるsetterメソッドまたはコンストラクター引数によって指定されたタイプと一致する場合があります。配列、コレクション、またはマップの場合、これは必ずしも問題ではありません。ただし、単一の値を期待する依存関係の場合、このあいまいさは任意に解決されません。一意のBean定義が利用できない場合、例外がスローされます

また、この投稿をご覧ください。

于 2013-01-31T08:38:19.527 に答える
1

これにより例外が発生します。springがアドレスフィールドを自動化しようとすると、idアドレスを持つBeanは検出されません...むしろ、適切なIDを持つユーザーQualiferが検出されないため、自動警告中に2id[address1からアドレスのporperオブジェクトをピンチします。 、 アドレス2]。

于 2013-01-31T08:44:03.743 に答える