[英]Autowire Service bean into an XML defined bean in Spring
在我的Spring應用程序中,我在我的xml中定義了一個bean:
<beans:bean id="tokenStore" class="com.myproject.core.security.RemoteTokenStore" />
我也創建了這個類,並使用@Service注釋:
com.myproject.core.service.CacheService
在我的RemoteTokenStore.java文件中,如果我嘗試Autowire CacheService,如下所示:
@Autowired
private CacheService cacheService;
它始終為空。 我該怎么解決這個問題?
將其添加到上下文xml文件中:
<context:component-scan base-package="com.myproject.core.security" />
指定后,將處理注釋,Spring將識別您的組件。
如果您還沒有,則還需要在應用程序XML中包含類路徑和上下文命名空間的spring-context
依賴項。
閱讀本文: http : //www.mkyong.com/spring/spring-auto-scanning-components/可能有所幫助
你沒有提供足夠的信息。 哪個春季版? 哪個應用服務器?
一般來說,你應該有
<context:component-scan base-package="com.packagebase" />
在你的spring xml conf文件中使用帶注釋的bean。
但是,它並不總是有效。
我遇到了彈簧2.5.6 + Jboss AS7的問題。 Jboss繼續報告未定義的注釋bean。 (NoSuchBeanDefinition或類似的東西)。 我切換到spring 3.0.0.Relase來解決它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.