Springboot核心注解(@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan)、关闭自动配置、自动配置原理等。

一、核心注解

@SpringBootApplication:组合注解,包括以下注解。在这里插入图片描述

  1. @SpringBootConfiguration:

配置注解,底层其实也是@Configuration注解,只不过在SpringBoot工程中更推荐使用@SpringBootConfiguration来替代Configuration。在这里插入图片描述

  1. @EnableAutoConfiguration:

启动自动配置,根据所依赖的Jar包自动配置相关配置项。(后续会继续解释)
在这里插入图片描述

  1. @ComponentScan:

扫描配置,SpringBoot默认会扫描启动类所在的同级包及其子包。

二、基本配置

(1)问:这里只讲一点,假如不需要SpringBoot自动配置,想关闭某一项自动配置,又该如何设置?
(2)比如:此时你需要做一个短信验证码发送的微服务,但因为项目架构,不得已自动配置了数据源,此时需要去进行取消。
(3)解决:使用主动类上@SpringBootApplication下的exclude参数进行关闭。
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)//取消数据源自动配置
@SpringBootApplication(exclude = RedisAutoConfiguration.class)//取消redis自动配置

三、自动配置原理

(1)SpringBoot在SpringApplication对象实例化时会加载META-INF/spring.factories文件,将该配置文件载入Spring容器中,进行自动配置。
(2) getSpringFactoriesInstances->loadFactoryNames->加载了META-INF/spring.factories。
(3)关于spring.factores:用来指导SpringBoot找到指定的配置文件。

总结:Springboot是通过SpringFactoriesLoader的loadFactoryNames方法读取spring.factories文件的。而SpringFactoryLoader是Spring框架的一个工具类,主要功能是从META-INF/spring.factories加载配置。
@SpringBootApplication注解使用@EnableAutoConfiguration进行自动配置:从classpath路径下搜寻所有的META-INF/spring.factories配置文件,然后将其中对应的配置项通过反射实例化为对应的标注了@Configration的IoC容器配置类,最后汇总并加载到Spring框架的IoC容器中。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
引用:@SpringBootConfiguration是一个注解,用于标识一个类是Spring Boot的配置类。它具有@Configuration的功能,并且在Spring Boot项目中更推荐使用@SpringBootConfiguration来替代@Configuration。 引用:@EnableAutoConfiguration是一个注解,用于启用自动配置。它会根据项目的依赖和配置自动加载适合的配置类。它会扫描classpath下的META-INF/spring.factories文件,根据其中的配置信息,自动配置Spring Boot应用程序的各种组件。 引用:@ComponentScan是一个注解,用于指定要扫描的包路径。它可以扫描指定包及其子包下的所有类,并将带有标识注解(如@Controller、@Service、@Repository等)的类注册为Spring Bean。它通常与@SpringBootConfiguration一起使用,用于启用组件扫描并将扫描到的类注册为Spring的组件。 综上所述,@SpringBootConfiguration用于标识一个类是Spring Boot的配置类,@EnableAutoConfiguration用于启用自动配置,@ComponentScan用于指定要扫描的包路径。它们的功能分别是配置Spring Boot应用程序、自动加载适合的配置类以及扫描并注册指定包下的组件。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [springboot(一)-@SpringBootConfiguration、@EnableAutoConfiguration源码分析](https://blog.csdn.net/weixin_43520586/article/details/121156638)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Springboot核心注解(@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan)、关闭自动...](https://blog.csdn.net/weixin_44045828/article/details/117983882)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT学习小镇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值