springboot多模块项目,有时候会出现,报错找不到自动注入的service
springboot Field xxxService in required a bean of type
每个service接口的实现类都加上@service注解,但还是找不到service。
最后调查发现,由于项目是多模块,启动类不在同一个包或子包下,导致无法自动注入
解决方法:
1、在启动类在加上扫描 @ComponentScan(“com.demo”),扫描上层包 com.demo
@SpringBootApplication
@EnableSwagger2
@MapperScan("com.demo.dao")
@ComponentScan("com.demo")
public class TaskApplication {
public static void main(String[] args) {
SpringApplication.run(TaskApplication.class, args);
}
}
2、调整TaskApplication启动类位置,调整到com.demo包小