我幾乎是Spring的初學者,所以不要以爲我沒有提到我可能做過的事情。無法獲得Spring依賴注入工作
我試圖讓依賴注入的工作,我有以下內容的spring.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" 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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.1.xsd">
<context:annotation-config/>
<bean id="connection" class="richo.project.ConnectionImpl"/>
</beans>
,然後在我的代碼有:
private IConnection conn;
@Resource(name="connection")
public void setConn(IConnection conn){
this.conn = conn;
}
當我嘗試在我的代碼中使用conn對象我得到一個nullpointerexception
請記住,我實際上並不知道如果彈簧運行,我使用IntelliJ,它放置了13個與彈簧相關的jar文件在我的lib目錄中,但我無法確定Spring是否嘗試注入任何東西
因此,類中的註釋只有在Spring實例化時纔有效,這意味着如果我僅僅使用新的,spring將會沒有閱讀這些註釋? –
Richo
2012-01-01 13:48:16
Guice的確如此。我不確定Spring是否屬於這種情況(關於_how_很多classloader魔術的問題已經到位)。 – 2012-01-01 13:50:59
@Richo:是的,Spring只注入它創建的bean。你實例化自己的豆不會被注入。 – 2012-01-01 13:54:29