Spring 预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11332289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring expected at least 1 bean which qualifies as autowire candidate for this dependency
提问by Tobia
I have a trouble with this Autowire:
我有这个 Autowire 的问题:
@Controller
public class ChiusuraController {
@Autowired
private ChiusuraProvider chiusuraProvider;
}
with this bean:
用这个豆:
@Service @Transactional
public class ChiusuraProvider extends ThreadProvider {
public void run() {}
}
that extends
延伸
public abstract class ThreadProvider extends Thread implements InitializingBean, Runnable, DisposableBean {
...
}
I get this error:
我收到此错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'chiusuraController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.cinebot.service.ChiusuraProvider com.cinebot.web.controller.ChiusuraController.chiusuraProvider; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.cinebot.service.ChiusuraProvider] 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)}
I saw that I did not get this error if I remove extends ThreadProviderof autowired class, but I really need ThreadProvider abstract class.
我看到如果我删除 自动装配类的extends ThreadProvider,我没有收到这个错误 ,但我真的需要 ThreadProvider 抽象类。
采纳答案by Biju Kunjummen
If there is an interface anywhere in the ThreadProvider hierarchy try putting the name of the Interface as the type of your service provider, eg. if you have say this structure:
如果 ThreadProvider 层次结构中的任何地方都有接口,请尝试将接口的名称作为服务提供者的类型,例如。如果你说这个结构:
public class ThreadProvider implements CustomInterface{
...
}
Then in your controller try this:
然后在你的控制器中试试这个:
@Controller
public class ChiusuraController {
@Autowired
private CustomInterface chiusuraProvider;
}
The reason why this is happening is, in your first case when you DID NOT have ChiusuraProviderextend ThreadProviderSpring probably was underlying creating a CGLIB based proxy for you(to handle the @Transaction).
发生这种情况的原因是,在您没有ChiusuraProvider扩展ThreadProviderSpring 的第一种情况下,可能是在为您创建基于 CGLIB 的代理(处理 @Transaction)。
When you DID extend from ThreadProviderassuming that ThreadProvider extends some interface, Spring in that case creates a Java Dynamic Proxy based Proxy, which would appear to be an implementation of that interface instead of being of ChisuraProvidertype.
当您从ThreadProvider假设 ThreadProvider 扩展某个接口进行扩展时,Spring 在这种情况下会创建一个基于 Java 动态代理的代理,它看起来是该接口的实现而不是ChisuraProvider类型。
If you absolutely need to use ChisuraProvideryou can try AspectJ as an alternative or force CGLIB based proxy in the case with ThreadProvider also this way:
如果您绝对需要使用,ChisuraProvider您可以尝试使用AspectJ 作为替代方案,或者在 ThreadProvider 的情况下也可以通过这种方式强制使用基于 CGLIB 的代理:
<aop:aspectj-autoproxy proxy-target-class="true"/>
Here is some more reference on this from the Spring Reference site: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/classic-aop-spring.html#classic-aop-pfb
这里有一些来自 Spring 参考站点的更多参考:http: //static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/classic-aop-spring.html#classic-aop -pfb
回答by Xaerxess
You should put this line in your application context:
您应该将此行放在您的应用程序上下文中:
<context:component-scan base-package="com.cinebot.service" />
Read more about Automatically detecting classes and registering bean definitionsin documentation.

