SpringBoot @ComponentScan 作用?
1 个回答
生活就是这样,别人看到的是结果,而自己撑的是整个过程。Hobbit霍比特人SpringBoot在写启动类的时候如果不使用@ComponentScan指明对象扫描范围,默认指扫描当前启动类所在的包里的对象,如果当前启动类没有包,则在启动时会报错:Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package错误。 因为启动类不能直接放在main/java文件夹下,必须要建一个包把它放进去或者使用@ComponentScan指明要扫描的包。代码示例如下: ```java @SpringBootApplication @ComponentScan(basePackageClasses=MytestApplication.class) public class MytestApplication { public static void main(String[] args){ SpringApplication.run(MytestApplication.class, args); } } ``` @ComponentScan(basePackageClasses=要扫描类.class所在位置的包)-意思是要扫描哪个类所在的包 通常结合spi机制使用。  ```xml org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.ph.share.hystrix.activity.Config ``` 别人引用这个jar 就是自动注入这个Config目录下的所有包。发布于 2022-03-07 05:40 •IP属地 辽宁举报评论收藏1
公众号进入网站体验更佳

