springboot系列 @ComponentScan和@EnableAutoConfiguration的区别

研究springboot源码,在网上看相关博客的时候对@ComponentScan和@EnableAutoConfiguration两者之间的作用没有做过多的区分,导致我觉得他们两者都有扫描相关组建然后将符合要求的放入到ioc容器中。所以我就占牛角尖了,单独研究了一下他们的不同点。
@ComponentScan和@EnableAutoConfiguration都是包含在@SpringBootApplication中的,也是@SpringBootApplication中比较重要的注解。
@ComponentScan和@EnableAutoConfiguration的相同点
两者都可以将带有@Component,@Service等注解的对象加入到ioc容器中。

@ComponentScan和@EnableAutoConfiguration的不同点
1.两者虽然都能将带有注解的对象放入ioc容器中,但是它们扫描的范围是不一样的。@ComponentScan扫描的范围默认是它所在的包以及子包中所有带有注解的对象,@EnableAutoConfiguration扫描的范围默认是它所在类。
2.它们作用的对象不一样,@EnableAutoConfiguration除了扫描本类带有的注解外,还会 借助@Import的支持,收集和注册依赖包中相关的bean定义,将这些bean注入到ioc容器中,在springboot中注入的bean有两部分组成,一部分是自己在代码中写的标注有@Controller,@service,@Respority等注解的业务bean,这一部分bean就由@ComponentScan将它们加入到ioc容器中,还有一部分是springboot自带的相关bean,可以将这部分bean看成是工具bean,这部分bean就是由@EnableAutoConfiguration负责加入到容器中。
3.@EnableAutoConfiguration可以单独启动springboot项目,而@ComponentScan是不能的。

下面用代码来验证第一点和第三点


第一种情况,验证@EnableAutoConfiguration的扫描范围

 
  1. import org.springframework.boot.SpringApplication;

  2. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

  3. import org.springframework.web.bind.annotation.RequestMapping;

  4. import org.springframework.web.bind.annotation.RestController;

  5. import study.springboot.component.subPackage.Food;

  6.  
  7. /**

  8. * @Author wangbiao

  9. * @Date 2019-09-01 09:49

  10. * @Decripition TODO

  11. **/

  12. @RestController

  13. @EnableAutoConfiguration

  14. public class DogController {

  15.  
  16.  
  17. @RequestMapping(value = "showDog",name = "测试@Component,@EnableAutoConfiguration扫描范围")

  18. public String showDog(){

  19. return "中华田园犬";

  20. }

  21.  
  22.  
  23. public static void main(String[] args) {

  24. SpringApplication.run(DogController.class,args);

  25. }

  26.  
  27.  
  28. }

 
  1. import org.springframework.beans.factory.annotation.Autowired;

  2. import org.springframework.web.bind.annotation.RequestMapping;

  3. import org.springframework.web.bind.annotation.RestController;

  4. import study.springboot.component.subPackage.Food;

  5.  
  6. /**

  7. * @Author wangbiao

  8. * @Date 2019-09-01 10:00

  9. * @Decripition TODO

  10. **/

  11. @RestController

  12. public class CatController {

  13. @RequestMapping(value = "showCat",name = "测试@Component,@EnableAutoConfiguration扫描范围")

  14. public String showCat(){

  15. return "波斯猫";

  16. }

  17. }

然后用http://localhost:8080/showDog来访问,得到的结果是:

http://localhost:8080/showCat访问,得到的结果是:

得到的结果是@EnableAutoConfiguration不能将同一个包下带有注解的对象加入到ioc容器中

下面验证@EnableAutoConfiguration能否将子包中的注解对象加入到ioc容器中

 
  1. import org.springframework.stereotype.Component;

  2.  
  3. /**

  4. * @Author wangbiao

  5. * @Date 2019-09-19 10:47

  6. * @Decripition TODO

  7. **/

  8. @Component

  9. public class Food {

  10. private String foodname="狗粮,猫粮";

  11.  
  12. public String getFoodname() {

  13. return foodname;

  14. }

  15.  
  16. public void setFoodname(String foodname) {

  17. this.foodname = foodname;

  18. }

  19. }

 
  1. import org.springframework.beans.factory.annotation.Autowired;

  2. import org.springframework.boot.SpringApplication;

  3. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

  4. import org.springframework.web.bind.annotation.RequestMapping;

  5. import org.springframework.web.bind.annotation.RestController;

  6. import study.springboot.component.subPackage.Food;

  7.  
  8. /**

  9. * @Author wangbiao

  10. * @Date 2019-09-01 09:49

  11. * @Decripition TODO

  12. **/

  13. @RestController

  14. @EnableAutoConfiguration

  15. public class DogController {

  16.  
  17. @Autowired

  18. private Food food;

  19. @RequestMapping(value = "showDog",name = "测试@Component,@EnableAutoConfiguration扫描范围")

  20. public String showDog(){

  21. return "中华田园犬"+food.getFoodname();

  22. }

  23.  
  24.  
  25. public static void main(String[] args) {

  26. SpringApplication.run(DogController.class,args);

  27. }

  28.  
  29.  
  30. }

启动就报错了:

可以看到@EnableAutoConfiguration是不能将子包中的注解对象加入到ioc容器中。
 

第二种情况,验证@Component的扫描范围
在@EnableAutoConfiguration注解上面加上@Component

 
  1. import org.springframework.beans.factory.annotation.Autowired;

  2. import org.springframework.boot.SpringApplication;

  3. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

  4. import org.springframework.context.annotation.ComponentScan;

  5. import org.springframework.web.bind.annotation.RequestMapping;

  6. import org.springframework.web.bind.annotation.RestController;

  7. import study.springboot.component.subPackage.Food;

  8.  
  9. /**

  10. * @Author wangbiao

  11. * @Date 2019-09-01 09:49

  12. * @Decripition TODO

  13. **/

  14. @ComponentScan

  15. @RestController

  16. @EnableAutoConfiguration

  17. public class DogController {

  18.  
  19. @Autowired

  20. private Food food;

  21. @RequestMapping(value = "showDog",name = "测试@Component,@EnableAutoConfiguration扫描范围")

  22. public String showDog(){

  23. return "中华田园犬"+food.getFoodname();

  24. }

  25.  
  26.  
  27. public static void main(String[] args) {

  28. SpringApplication.run(DogController.class,args);

  29. }

  30.  
  31.  
  32. }

http://localhost:8080/showDog访问,结果是下面的页面,可以看出Food这个对象是被引进来了的

再用http://localhost:8080/showCat访问页面,得到的结果是下面的页面,可以看出CatController是被加入到容器中的。所以验证了第一点,@ComponentScan扫描的范围默认是它所在的包以及子包中所有带有注解的对象,@EnableAutoConfiguration扫描的范围默认是它所在类。
感谢这篇博客提供的参考:https://blog.csdn.net/qq_39404626/article/details/83995870

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值