Springboot学习3——自动配置是如何实现的(源码解析)

JAVA框架文章索引

1、 starter启动器

在pom文件里面我们可以发现很多的spring-boot-starter-xxx这样的配置,这是官方提供的简化的pom配置。
比如我们需要添加web的配置只需要导入:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
 </dependency>

如果需要导入thymeleaf只需要配置:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
 </dependency>

更多的配置可以参考:这篇文章

点开spring-boot-starter-parent再点开里面的spring-boot-dependencies可以看到很多默认配置好的依赖版本
在这里插入图片描述

2、自动配置核心注解

@SpringBootApplication注解:

在程序主入口可以明显看到有这样一个注解@SpringBootApplication,它表示这是Springboot的主配置类,springboot应该运行它的main方法来启动springboot应用。点进去可以看到这样的内容:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {

@SpringBootConfiguration注解:

在上面我们可以明显看到有这个注解,它表示这是一个Springboot的配置类。点开这个注解内容如下

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {

在上面可以发现 @Configuration这个注解,它的作用是声明当前类是一个配置类,相当于一个Spring的XML配置文件。点开它

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {

可以发现它有一个注解是 @Component,这个注解同样是spring的一个底层注解,它可以标识当前类为配置类相当于XML中配置的bean。
@Configuration本身也算是@Component,它们的应用场景不同,一般全局的一些配置才用@Configuration,而@Component一般用来配置业务bean。
配置业务的的时候相关的几个注解:@Service(用于标注服务层),@Repository(用于标注数据访问层),@Controller(用于标注控制层)

@EnableAutoConfiguration注解:

这个注解的功能就是开启自动配置,点进去内容如下

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {

(一)打开 @AutoConfigurationPackage这个注解

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})
public @interface AutoConfigurationPackage {

@import 通过快速导入的方式实现把实例加入spring的IOC容器中
我们打开 Registrar.class这个类并debug里面的一个方法,并执行其中一段语句,可以发现结果返回了SpringBootApplication主入口所在的包名。

在这里插入图片描述
通过以上探析,可以明白@EnableAutoConfiguration注解的主要作用是 将@SpringBootApplication主配置类所在的包和子包里面所有的组件扫描到Spring容器
这也解释了为什么规定代码要写到主配置类包及子包之下,否则无法被扫描到。

(二)打开 @Import({AutoConfigurationImportSelector.class}) 打开注解里面的这个类,并debug
在这里插入图片描述
可以发现通过 AutoConfigurationImportSelector.class里面的方法以全类名返回了很多组件,这些组件都会被加载到spring容器中。而这些被加载的组件都是配置好的 自动配置类

在springboot启动的时候这些导入的内容来自META-INF/spring.factories文件
在这里插入图片描述
随便打开一个类:
在这里插入图片描述
可以看到这些类事先是已经被配好了的。

3、总结

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值