Spring Boot的@Service和@Autowired和@ComponentScan注解
SpringBoot项目的Bean装配默认规则是根据Application类(SpringBoot项目入口类)所在的包位置从上往下扫描,即只扫描该类所在的包及其子包。
1.如果需要扫描其上层包中的类,则将其移动到其上层包中
2.使用@ComponentScan注解,在SpringBoot启动类上注解配置,需要扫描的包。

1SpringBootApplication 2@ComponentScan(basePackages = {"com.example"}) 3public class DemoApplication { 4 5 public static void main(String[] args) { 6 SpringApplication.run(DemoApplication.class, args); 7 } 8}