Spring预计至少有1个bean符合这种依赖关系的自动线路候选 [英] Spring expected at least 1 bean which qualifies as autowire candidate for this dependency

查看:1141
本文介绍了Spring预计至少有1个bean符合这种依赖关系的自动线路候选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个Autowire的麻烦:

  @Controller 
public class ChiusuraController {

@Autowired
私人ChiusuraProvider chiusuraProvider;
}

与此bean:



公共类ChiusuraProvider扩展ThreadProvider {


public void run(){}
}

扩展

 code> public abstract class ThreadProvider extends Thread implements InitializingBean,Runnable,DisposableBean {
...
}

我收到此错误:

  org.springframework.beans.factory.BeanCreationException:创建错误bean名称为chiusuraController:注入自动连线依赖关系失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动连线字段:private com.cinebot.service.ChiusuraProvider com.cinebot.web.controller.ChiusuraController.chiusuraProvider;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到依赖关系的类型为[com.cinebot.service.ChiusuraProvider]的匹配bean:至少有一个bean符合此依赖关系的自动连线候选。依赖关系注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)} 

我看到我没有得到这个错误,如果我删除自动连线类的扩展ThreadProvider ,但我真的需要ThreadProvider抽象类。

解决方案

如果ThreadProvider层次结构中的任何地方都有接口,可以将接口名称作为服务提供者的类型,例如。如果你说这个结构:

  public class ThreadProvider实现CustomInterface {
...
}

然后在您的控制器中尝试这样:

  @Controller 
public class ChiusuraController {

@Autowired
private CustomInterface chiusuraProvider;
}

发生这种情况的原因是,在第一种情况下,当您DID NOT有 ChiusuraProvider extension ThreadProvider Spring可能是为您创建了一个基于CGLIB的代理(用于处理@Transaction)。假设ThreadProvider扩展了一些接口,当DID从 ThreadProvider 中扩展时,Spring在这种情况下会创建一个Java动态代理基于代理,它似乎是该接口的实现,而不是 ChisuraProvider 类型。



如果你绝对需要使用 ChisuraProvider 您可以尝试使用AspectJ作为替代或强制基于CGLIB的代理,在ThreadProvider的情况下也是这样的:

 < aop:aspectj-autoproxy proxy-target-class =true/> 

以下是Spring参考站点的更多参考资料: http://static.springsource .org / spring / docs / 3.1.x / spring-framework-reference / html / classic-aop-spring.html#classic-aop-pfb


I have a trouble with this 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 ThreadProvider of autowired class, but I really need ThreadProvider abstract class.

解决方案

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:

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 ChiusuraProvider extend ThreadProvider Spring probably was underlying creating a CGLIB based proxy for you(to handle the @Transaction).

When you DID extend from ThreadProvider assuming 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 ChisuraProvider type.

If you absolutely need to use ChisuraProvider you can try AspectJ as an alternative or force CGLIB based proxy in the case with ThreadProvider also this way:

<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预计至少有1个bean符合这种依赖关系的自动线路候选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆