SpringBoot自动装配原理

流程图
在这里插入图片描述
前置知识点掌握
首先我们应该知道所有的springboot-starter配置文件中都提供了一个autoconfigure的依赖,而这个依赖就是来帮助我们将对应工具中的配置加载注册到ioc容器中例如:下面是mybatis的配置

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-autoconfigure</artifactId>
</dependency>

这是一个对应的文件夹,位置就在对应的jar中,在这个jar中的META-INF文件夹下的spring.factories文件中定义了自动配置有关的东西,如下。

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration

这两行代码第一行代表spring提供的注解,这个注解的作用是开启自动装配;第二行代表的是对应的自定义开启自动配置的类,这个类中我们定义如下:

package com.gsh.pagex.autoconfigure;

import com.gsh.pagex.aop.PageXAop;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * @author gsh
 * 2024/11/4 19:05
 */
@Configuration//这个注解作用是将当前类的东西添加到ioc容器
@Import(PageXAop.class)//这个注解作用是指明要被添加到ioc容器中的类
@ConditionalOnProperty(value = "pagex.enabled", havingValue = "true",matchIfMissing = true)//这个注解的作用是设置满足某种条件时启动自动装配
public class PageXAutoConfiguration {
}

接下来我们了解如何读取到对应的starter中设置的spring.factories注解开启配置
流程详解

  1. springboot启动类中的@SpringBootApplication注解,这个注解中包含三大注解
    ● @EnableAutoConfiguration:在starter中我们使用了对应依赖,但是这个依赖是怎样被读取的,就是根据这个注解
    ● @ComponentScan:扫描标注了 @Component、@Service、@Repository 和 @Controller 的类,并将它们注册为 Spring 管理的 Bean。
    ● @Configuration:标记当前类为配置类,可以用来定义 Bean。
    其中最重要的是第一个注解@EnableAutoConfiguration,这个注解内部是一个注解类
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.springframework.boot.autoconfigure;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
    String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";

    Class<?>[] exclude() default {};

    String[] excludeName() default {};
}

在上面的注解类中我们可以找到AutoConfigurationImportSelector这个自动装配导入选择器,在这个里面我们可以找到import org.springframework.core.io.support.SpringFactoriesLoader这个类里面我们可以看到以下两行代码,定义了我们查找配置文件的地址,以及对应文件名

    public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
    private static final Log logger = LogFactory.getLog(SpringFactoriesLoader.class);

然后我们继续回到AutoConfigurationImportSelector类,可以看到以下代码,说明springboot内部通过这里来去上述的地址查找配置

    protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
        Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
        return configurations;
    }

然后我们任意找到一个starter配置文件的META-INF文件夹下的spring.factories文件

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.gsh.wapperx.autoconfigure.WapperxAutoConfiguration

可以看到对应的key-value结构,注解以及对应的文件我们找到这个配置文件可以看到以下配置

package com.gsh.pagex.autoconfigure;

import com.gsh.pagex.aop.PageXAop;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * @author gsh
 * 2024/11/4 19:05
 */
@Configuration//这个注解作用是将当前类的东西添加到ioc容器
@Import(PageXAop.class)//这个注解作用是指明要被添加到ioc容器中的类
@ConditionalOnProperty(value = "pagex.enabled", havingValue = "true",matchIfMissing = true)//这个注解的作用是设置满足某种条件时启动自动装配
public class PageXAutoConfiguration {
}

总结
到这里我们就可以知道当一个springboot启动类加载的时候,会根据@EnableAutoConfiguration注解找到项目依赖库中对应的需要开启自动装配的配置文件实现自动装配,这其中使用了spring的SPI机制,全名是service provide interface 服务提供接口,spring的这一机制可以然我们不局限于单独的接口进行服务实现,可以借助类或者注解。这就是springboot自动装配原理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值