我有一个名为 com.example.service
的包,在我的 Spring 配置类中我有注释 @ComponentScan({"com.example.service"},{"com .example.controller"})
。
当我尝试 @Autowire
服务时,代码编译失败并出现 NoSuchBeanDefinitionException
。 MyService 接口(interface)使用 @Service
注释。
目前我使用了一个非常丑陋的解决方法,并在我的 ExampleConfig.java 中声明了每个服务 bean,例如
@Bean
public MyService myService() {
return new MyServiceImpl();
}
通常 @ComponentScan
似乎可以工作,如果我删除 Controller 包,则找不到 Controller 。我理解错了什么?如果我遗漏了任何相关信息,请告诉我。
最佳答案
The MyService interface is annotated with @Service
您必须注释接口(interface)的实现。不是界面本身。
关于java - @Service 的 Spring @ComponentScan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26730388/