3

我有一个服务 bean(用 @Service 注释),它为扩展 ApplicationEvent 抽象类的 T 类型的事件对象实现 ApplicationListener 接口。Spring docs here中有一个非常简单明了的示例

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

org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型 [...] 的匹配 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解 {@org.springframework.beans.factory.annotation.Autowired(required=true)}

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

4

1 回答 1

6

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

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

如果你有这个课程:

@Service
public class FooServiceImpl implements FooService{}

将其连接为:

@Autowired
private FooService fooService;

不是:

@Autowired
private FooServiceImpl fooService;

参考:

于 2011-07-11T14:25:45.847 回答