2009-09-01 5 views
84

Springのutil名前空間<util:list id="myList">を使用して名前付きリストを挿入するBeanがありますが、SpringはString型のBeanのコレクションを探しています。私の壊れたテストでは、次のとおりです。utilスキーマを使用してリストを自動配線すると、NoSuchBeanDefinitionExceptionが返されます。

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns="http://www.springframework.org/schema/beans" 
    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/util http://www.springframework.org/schema/util/spring-util-2.5.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 

    <util:list id="myList"> 
     <value>foo</value> 
     <value>bar</value> 
    </util:list> 

</beans> 

しかし、私は、私は、これはそれが動作するように期待された方法だろう考え出しとしてではなく、私を困惑

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [java.lang.String] found for dependency [collection of java.lang.String]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=myList)} 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:726) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:571) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412) 

を得る:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration 
public class ListInjectionTest { 

    @Autowired @Qualifier("myList") private List<String> stringList; 

    @Test public void testNotNull() { 
     TestCase.assertNotNull("stringList not null", stringList); 
    } 
} 

私のコンテキストがあります。

答えて

162

これにより3.11.2. @Autowiredで指定@ Autowiredの行動のかなり曖昧な部分にされています

へ 注釈を追加することにより、 ApplicationContextから特定のタイプのすべての 豆を提供することも可能です同じことが型付きコレクションに適用され は、その型の配列を期待フィールドやメソッド...

...

つまり、@Autowired @Qualifier("myList") List<String>と言うと、実際には、「java.lang.StringのタイプのすべてのBeanのリストに、修飾子「myList」が付いています。

溶液は3.11.3. Fine-tuning annotation-based autowiring with qualifiersに記載されている:

名前で 、アノテーション駆動型の注入を表現する場合、 は主に@Autowired使用していない - Bean名に を参照する技術的に可能である場合でも、 を@Qualifier 値まで。代わりに、意味的に宣言された型 がマッチングプロセスとは無関係であると、その 一意の名前で特定のターゲット・コンポーネントを識別するために定義された あるJSR-250 @Resource注釈を好みます。この セマンティック差の特定結果として

型マッチングがそれらに適切に適用できない あるので、それ自体がコレクションまたは マップタイプとして定義 ある豆は @Autowiredを介して注入することができません。 特定のコレクション/マップBeanを ユニークな名前で参照して、そのような豆の場合は @Resourceを使用してください。

だからあなたのテストでこれを使用し、それが正常に動作します:

@Resource(name="myList") private List<String> stringList; 
+7

あなたは人生にとって安全であり、だからstackoverflow.comです! :) – Rihards

+5

私はできれば私はこの10票を与えるだろう。あなたはダ・マン、スカフマンです。 – duffymo

+4

ジョンスケートの投票があったら、私はこれを与えるだろう。 –

-1

のJBoss 6.1を使用している場合、私は@Resourceアノテーションは、私の展開は(質問とは無関係のように省略し、詳細を)失敗する原因となりました。

@Injectアノテーション(JSR-330)を使用するのは、すべてのアプリケーションサーバーの一般的な代替方法です。

+0

現在のスレッドとはまったく関係ありません – Gab

0

もう一つ起こっていることは、あなたがbeanのプロパティをautowiringしているということです。

<bean id="cleaningUpOldFilesTasklet" class="com.example.mypackage.batch.tasklets.CleanUpOldFilesTasklet"> 
    <property name="directoriesToClean"> 
     <list> 
      <value>asfs</value> 
      <value>fvdvd</value> 
      <value>sdfsfcc</value> 
      <value>eeerer</value> 
      <value>rerrer</value> 
     </list> 
    </property> 
</bean> 

とクラス:

public class CleanUpOldFilesTasklet extends TransferingFilesTasklet implements Tasklet{ 

private long pastMillisForExpiration; 
private final String dateFormat = "MM.dd"; 
Date currentDate = null; 

List<String> directoriesToClean; 

public void setDirectoriesToClean(List<String> directories){ 
    List<String> dirs = new ArrayList<>(); 
    for(String directory : directories){ 
     dirs.add(getSanitizedDir(directory)); 
    } 
    this.directoriesToClean = dirs; 
} 

このような場合、あなたは(XMLを使用した場合)の例をautowireが、ちょうどsetterメソッドを作成し、Bean定義にプロパティタグを使用する必要がいけませんクラス内の@Autowiredアノテーションを参照してください。

関連する問題