Java Spring@component扫描@Service
我有一个名为Java Spring@component扫描@Service,java,spring,spring-mvc,autowired,Java,Spring,Spring Mvc,Autowired,我有一个名为com.example.service的包,在我的Spring配置类中有注释@ComponentScan({“com.example.service”},{“com.example.controller”}) 当我尝试@Autowire服务时,代码编译失败,出现NoSuchBeanDefinitionException。MyService接口用@Service注释 目前,我使用了一种非常糟糕的解决方法,并在ExampleConfig.java中声明了每一个服务bean @Bean p
com.example.service
的包,在我的Spring配置类中有注释@ComponentScan({“com.example.service”},{“com.example.controller”})
当我尝试@Autowire
服务时,代码编译失败,出现NoSuchBeanDefinitionException
。MyService接口用@Service
注释
目前,我使用了一种非常糟糕的解决方法,并在ExampleConfig.java中声明了每一个服务bean
@Bean
public MyService myService() {
return new MyServiceImpl();
}
通常,@ComponentScan
似乎可以工作,如果我删除了控制器包,则找不到控制器。我理解错了什么?如果我遗漏了任何相关信息,请告诉我
MyService接口用@Service注释
必须对接口的实现进行注释。不是界面本身。尝试使用以下代码扫描组件扫描注释扫描多个软件包:
@ComponentScan({"com.example.service","com.example.controller"})
而不是
@ComponentScan({"com.example.service"},{"com.example.controller"})
使用字符串数组扫描多个基本包。很抱歉,我猜您没有正确阅读问题。这正是我正在做的。但是Jens提供了正确的答案,编译时不会发生异常。将错误类型分开,否则您将在错误的地方寻找解决方案。这一点您是绝对正确的。在这一点上,我的描述是不准确的。