+-
spring – 使用@Autowired注入实现ApplicationListener的bean不起作用?
我有一个服务bean(使用@Service注释),它实现了扩展ApplicationEvent抽象类的T类型事件对象的ApplicationListener接口.在 Spring docs here中有一个非常简单明了的例子

但是,当我尝试使用@Autowired将此bean注入其他的时,我得到的是:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No
matching bean of type […] found for dependency: expected at least 1
bean which qualifies as autowire candidate for this dependency.
Dependency annotation
{@org.springframework.beans.factory.annotation.Autowired(required=true)}

如果我尝试使用类似@Resource的东西,那么我会得到一个类强制转换异常(尝试注入一种类型的资源但获得代理).

最佳答案

If i try to use something like @Resource then i get a class cast
exception (attempting to inject a resource of one type but getting a
Proxy).

这听起来像是在尝试按类引用它,而它是作为基于接口的JDK代理连接的.

如果你有这个课程:

@Service
public class FooServiceImpl implements FooService{}

把它当成:

@Autowired
private FooService fooService;

不是:

@Autowired
private FooServiceImpl fooService;

参考:

> AOP Proxying Mechanisms

点击查看更多相关文章

转载注明原文:spring – 使用@Autowired注入实现ApplicationListener的bean不起作用? - 乐贴网