1.@ComponentScan注解的作用
告诉spring从哪里装载bean到容器。
2.Springboot项目中如何使用@ComponentScan注解
- 如果你的其他包都在使用了@SpringBootApplication注解的main app所在的包及其下级包,则你什么都不用做,SpringBoot会自动帮你把其他包都扫描了
- 如果你有一些bean所在的包,不在main app的包及其下级包,那么你需要手动加上@ComponentScan注解并指定那个bean所在的包
3.@Component和@ComponentScan的区别
- 在某个类上使用@Component注解,表明当需要创建类时,这个被注解的类是一个候选类。就像是举手。
- @ComponentScan 用于扫描指定包下的类。就像看都有哪些举手了。