![](/img/trans.png)
[英]Why I can't inject this bean annoted with @Service into a bean declared into the spring-security.xml configuration file?
[英]Why I can't inject a Spring Environment object into my bean?
我在使用Spring框架的Java應用程序中遇到以下問題。
所以我有以下情況,進入root-context.xml配置文件我有這個bean配置:
<!-- Definition for datiPianiInterventiDaoImpl bean -->
<bean id="datiPianiInterventiDaoImpl" class="it.myCompany.myclient.batch.dao.DatiPianiInterventiDaoImpl">
<property name="dataSource" ref="dataSource" />
</bean>
好吧它工作正常,這個bean正確創建並正常工作。
問題是現在在這個bean中我還要注入一個org.springframework.core.env.Environment Spring類的內容。
所以我試着這樣做:
public class DatiPianiInterventiDaoImpl implements DatiPianiInterventiDao {
@Autowired
private Environment env;
...................................................
...................................................
...................................................
}
但它似乎無法工作,因為當我執行我的應用程序時, 環境env的值為null 。
@Autowired已激活,因為我在項目的其他類中使用此注釋。
那可能是什么問題呢? 我想也許它可能取決於我將我的bean id =“datiPianiInteventiDaoImpl”定義到我的root-context.xml中 (這里我也定義了注入此bean的依賴項)。
那么也許我不能將XML依賴注入與@Autowired的使用混合在一起?
怎么了? 我錯過了什么? 如何正確地將Environment實例注入此類?
環境的可能原因為null:
將XML依賴注入與@Autowired的使用混合沒有問題。 只要你的bean被spring bean工廠掃描,這是一個有效的語法。 自動裝配環境到Dao classe有問題,看看戴夫在這里寫的是什么,你可以在這個鏈接中找到解決方案(另一個答案)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.