springboot是如何自动装配的

springboot是如何自动装配的

springboot文档:https://docs.spring.io/spring-boot/docs/2.3.5.RELEASE/reference/htmlsingle/#boot-documentation

简述:springboot启动类详解

C:\Users\hwlb\AppData\Local\Temp

接下来都是围绕着这个启动类进行介绍


一.点进去run方法,可以看到

在这里插入图片描述

它可以去跑spring的一个实例,传参方式,任意一个类型的类和一个String数组,它会使用默认的配置,比如@SpringBootApplication,它会运行一个上下文对象

1.@SpringBootApplication的应用

点进去看下
在这里插入图片描述

①:@SpringBootConfiguration

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
    @AliasFor(
        annotation = Configuration.class
    )
    boolean proxyBeanMethods() default true;
}

它的主要配置是@Configuration,它是一个容器,相当于我们写spring中的xml的一些配置,如beans,包含了许多bean,是一个IOC容器

②:@EnableAutoConfiguration

它是用于开启自动装配的,点进去看下

@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 {};
}

@Import导入一个或者多个Configuration

​ AutoConfigurationImportSelector自动装配的选择类

​ 选择一些你想要导入的装配的类

    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        if (!this.isEnabled(annotationMetadata)) {
            return NO_IMPORTS;
        } else {
        //去获得自动装配的类
            AutoConfigurationImportSelector.AutoConfigurationEntry autoConfigurationEntry = this.getAutoConfigurationEntry(annotationMetadata);
            return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
        }
    }
    
    
    
    //getAutoConfigurationEntry方法
        protected AutoConfigurationImportSelector.AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
        if (!this.isEnabled(annotationMetadata)) {
            return EMPTY_ENTRY;
        } else {
            AnnotationAttributes attributes = this.getAttributes(annotationMetadata);
            //获得一些默认的配置
            List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
            configurations = this.removeDuplicates(configurations);
            Set<String> exclusions = this.getExclusions(annotationMetadata, attributes);
            this.checkExcludedClasses(configurations, exclusions);
            configurations.removeAll(exclusions);
            configurations = this.getConfigurationClassFilter().filter(configurations);
            this.fireAutoConfigurationImportEvents(configurations, exclusions);
            return new AutoConfigurationImportSelector.AutoConfigurationEntry(configurations, exclusions);
        }
    }
    
    
    
    //此种方法在 AutoConfigurationImportSelector 类中
        protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
        //断言,如果为空会出现断言后的异常,可以看到有META-INF/spring.factories
        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;
    }

定位这个类,可以看到AutoConfigurationImportSelector在下面的这个jar包中

在这里插入图片描述

一:找到对应的META-INF/spring.factories

1.找到对应的类信息,点进入
在这里插入图片描述
在这里插入图片描述

@ConditionalOnClass如上图,满足条件的时候进行加载

点进入tomcat看一下

在这里插入图片描述

有默认端口号和hostname

2.回到META-INF/spring.factories找到WebMvcAutoConfiguration
在这里插入图片描述

当我们使用@SpringBootApplication,它会自动帮我们装配springmvc

在这里插入图片描述

3.回到META-INF/spring.factories找到ServletWebServerFactoryAutoConfiguration
在这里插入图片描述

点进去,会看到一个jar,它这个类的含义是自动装配我们的web服务器,就是使用我们的内置tomcat

在这里插入图片描述

startup是用于启动tomcat的一个命令

4.META-INF/spring.factories还包含很多类

//springboot所推荐的一个模板配置类
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
//事务装配的自动类
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\
k.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
//事务装配的自动类
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值