SpringBoot工程使用@ComponentScan导致启动类下及子包无法扫描问题

65 阅读1分钟

今天遇到一个大家经常容易忽视的问题,在Springboot工程中,启动类注解@SpringbootApplication本身包含了@EnableAutoConfiguration、@Configuration、@ComponentScan、@SpringbootApplication自带的扫描范围: 当前所在的包以及子包,当@SpringbootApplication 和 @ComponentScan同时使用时,我们定义的@ComponentScan会覆盖本身@SpringbootApplication的扫描范围 因此我们需要在@ComponentScan中添加当前工程需要扫描的包范围

image.png

image.png