2012-05-08 59 views
2

註釋@autowired的這個特定類的成員私有字段在服務JAR中,當我試圖在我的代碼中獲取該類的實例時,@autowired成員始終返回空值。我在我的項目的context.xml中有下面的組件掃描語句,它試圖查找JAR中的基本包,但仍然看起來有空值被注入到註釋的成員中。 JAR內部還有一個context.xml,它具有與下面相同的組件掃描條目。 JAR源代碼現在無法更改,有什麼我不見了?Spring @autowired返回空值

<!-- 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),所以它不是由spring管理的,所以值不會被注入。

+0

春天的管理bean修復它。謝謝!我應該早些發佈這個問題 – Noosphere

0

您是否嘗試用@Resource替換@Autowired?我認爲按類型@Autowired線,而@Resource通過bean名稱注入。