[英]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.