2012-05-08 8 views
2

@autowiredというアノテーションが付けられたこの特定のクラスのメンバープライベートフィールドは、サービスJAR内にあり、コード内でクラスのインスタンスを取得しようとすると、@ autowiredメンバーは常にヌル。 JAR内のベースパッケージを検索しようとしているプロジェクトのcontext.xml内に以下のコンポーネントスキャンステートメントがありますが、nullが注釈付きメンバーに注入されているようです。 JARは内部的にcontext.xmlも持っていますが、これは以下のコンポーネントスキャンエントリと同じです。 JARソースコードは今変更できません。欠けているものはありますか?スプリング@autowiredがnullを返す

<!-- local WEB-INF/spring-context/spring-application-context.xml --> 
<context:annotation-config /> 
<context:component-scan base-package="blah.blah" /> 

//this code within my project 
//wjc.getMethod() dereferencing comes back null 
public class A{ 
    WithinJARInterface wjc = new WithinJARInterfaceImpl() 
    List someObject = wjc.getMethod() 
} 

//this code interface within JAR 
public interface WithinJARInterface{ 
    public List getMethod() throws Exception(); 
} 

//this code within JAR 
public class WithinJARInterfaceImpl implements WithinJARInterface{ 

    //below member always comes back NULL 
    @Autowired 
    private JARService jService; 

    public List getMethod(){.....} 

} 

public interface JARService{ 
    public List method1(); 
} 

@Service("JARService") 
    public class JARServiceImpl implments JARService { 
    public List method1(){ } 
    } 
+2

'@ Autowired'でアノテーションされた' null'フィールドを持つオブジェクトはどうやって取得しますか?コードを表示します。 –

+0

私は質問を更新しました – Noosphere

+0

それはたぶん質問の誤字ですが、注釈は '@ Autowired'(大文字のA)です。 - これが質問のタイプミスでない場合は、問題の根源になる可能性があります。 – Ralph

答えて

7

自分でWithinJARInterfaceImplを構築しています(newを呼び出して)ので、スプリングで管理されないため、値は注入されません。

+0

スプリングマネージドビーンがそれを修正しました。どうも!早く質問を投稿するべきだった – Noosphere

0

@Autowiredを@Resourceに置き換えてみましたか? @ResourceはBean名で注入するのに対し、@Autowiredワイヤはタイプ別に考えています。

関連する問題