[英]Spring 3: Autowire a bean declared in xml application context to a @Component bean
我在聲明為@Component
類中自動裝配bean屬性時遇到問題。 我已經嘗試了許多不同的方法,但是不幸的是,在春季配置期間,我為此特定的bean收到了NoSuchBeanDefinitionException
。
下面是一些示例代碼,用於對我當前的內容進行建模。
package com.foo.bar;
@Component
public class MyDeployer implements ApplicationContextAware
{
@Autowired
private ClusterRegistry clusterRegistry; //The bean I am trying to wire
...
}
在我的應用程序上下文中,我具有以下內容:
<beans ...... />
<context:component-scan base-package="com.foo.bar" />
<context:annotation-config />
<bean id="clusterRegistry" name="clusterRegistry" class="com.my.implementation.ClusterRegistryFileImpl" />
</beans>
我的ClusterRegistryFileImpl類定義如下:
package com.my.implementation;
public final class ClusterRegistryFileImpl implements ClusterRegistry
{
...
}
請注意,ClusterRegistryFileImpl實現了ClusterRegistry接口,並且此接口/實現類沒有依賴關系。 這些文件也沒有使用組件掃描(也沒有用@Component
注釋,但是如果我在appContext中聲明bean,我認為它們並不需要。
我的整合測試看起來像
@ContextConfiguration(locations= {
"/com/app/context/path/appContext.xml",
"/com/app/context/path/aDifferentAppContext.xml"
})
public class MyDeployerTest extends AbstractTestNGSpringContextTests
{
@Autowired
private MyDeployer deployer; //class that uses the bean I need
@Test
protected void testDeployerStartup()
{
deployer.startup();
...
}
}
下面是錯誤:
Caused by:
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [com.my.implementation.ClusterRegistry] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency.
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
任何建議或方向將不勝感激。
您的類ClusterRegistryFileImpl類應具有注釋(如@Service,如果它是服務類,則為@Repository或@Component)。
而且我不確定最終訪問者是否適用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.