SpringBoot自动装配原理

860 阅读4分钟

Spring Boot主要作用就是简化Spring应用的开发,开发者只需要通过少量代码就可以创建一个Spring应用,而达到这一目的最核心的思想就是约定优于配置

约定优于配置(Convention Over Configuration)是一种软件设计范式,目的在于减少配置的数量或者降低理解难度,从而提升开发效率。

先总结一下结论:

springboot通过spring.factories能把main方法所在类路径以外的bean自动加载,其目的就是为了帮助自动配置bean,减轻配置量

springboot autoconfig的一些实验

一个springboot工程,springbootautoconfig.test.config这个包和启动类的包不再同一个路径下,那么这个包下的注解等应该不会生效,bean也无法托管给spring管理

image.png

@Configuration//开启配置
@EnableConfigurationProperties(HelloProperties.class)//开启使用映射实体对象
@ConditionalOnClass(TestHello.class)//存在TestHello时初始化该配置类
@ConditionalOnProperty//存在对应配置信息时初始化该配置类
(
        prefix = "zxp.hello",//存在配置前缀zxp.hello
        value = "flag"
)
public class HelloAutoConfiguration {
    @Autowired
    private HelloProperties helloProperties;

    @Bean//创建HelloService实体bean
    @ConditionalOnMissingBean(TestHello.class)//缺失HelloService实体bean时,初始化HelloService并添加到SpringIoc
    public TestHello helloService()
    {
        System.out.println(">>>The TestHello Not Found,Execute Create New Bean.");
        TestHello testHello = new TestHello(helloProperties.getName(),helloProperties.getFlag());
        return testHello;
    }
}
@ConfigurationProperties(prefix = "zxp.hello")
@Data
public class HelloProperties {
    private String name;
    private String flag;
}
public class TestHello {
    String name;
    String flag;

    public TestHello(String name, String flag) {
        this.name = name;
        this.flag = flag;
    }

    public String print(){
        String msg = "name is "+name + "  " + "flag is "+flag;
        System.out.println(msg);
        return msg;
    }
}

经过测试,工程无法启动,并且在下面位置报错:

@RestController
public class TempController {
    @Autowired
    TestHello testHello;//无法注入

    @RequestMapping("/test/boot")
    public String boot(){
        return testHello.print();
    }
}

在resources下创建META-INF路径,并创建spring.factories文件

#配置自定义Starter的自动化配置
org.springframework.boot.autoconfigure.EnableAutoConfiguration=springbootautoconfig.test.config.HelloAutoConfiguration

再试启动又报错了

- Bean method 'helloService' not loaded because @ConditionalOnProperty (zxp.hello) did not find property 'flag'

原因是,如果没有配置zxp.hello.flag,怎会报错

@ConditionalOnProperty//存在对应配置信息时初始化该配置类
(
        prefix = "zxp.hello",//存在配置前缀hello  
        value = "flag"//开启
)

在application.properties中添加

zxp.hello.flag=2

成功了,访问controller

name is null flag is 1

把另外一项配置也加上

zxp.hello.name=zhang xp

访问controller

name is zhang xp flag is 1

SpringBoot autoconfig部分注解说明

@ConditionalOnXxx 可以根据条件来决定是否执行自动配置

@ConditionalOnBean:当SpringIoc容器内存在指定Bean的条件
@ConditionalOnSingleCandidate:当指定Bean在SpringIoc容器内只有一个,或者虽然有多个但是指定首选的Bean
@ConditionalOnMissingBean:当SpringIoc容器内不存在指定Bean的条件

@ConditionalOnClass:当SpringIoc容器内存在指定Class的条件
@ConditionalOnMissingClass:当SpringIoc容器内不存在指定Class的条件
@ConditionalOnExpression:基于SpEL表达式作为判断条件
@ConditionalOnJava:基于JVM版本作为判断条件
@ConditionalOnJndi:在JNDI存在时查找指定的位置
@ConditionalOnResource:类路径是否有指定的值
@ConditionalOnProperty:指定的属性是否有指定的值

@ConditionalOnNotWebApplication:当前项目不是Web项目的条件
@ConditionalOnWebApplication:当前项目是Web项目的条件

@AutoConfigureBefore
@AutoConfigureAfter
@AutoConfigureOrder
public @interface ConditionalOnProperty {
    String[] value() default {}; //数组,获取对应property名称的值,与name不可同时使用  
    String prefix() default "";//property名称的前缀,可有可无  
    String[] name() default {};//数组,property完整名称或部分名称(可与prefix组合使用,组成完整的property名称),与value不可同时使用  
    String havingValue() default "";//可与name组合使用,比较获取到的属性值与havingValue给定的值是否相同,相同才加载配置  
    boolean matchIfMissing() default false;//缺少该property时是否可以加载。如果为true,没有该property也会正常加载;反之报错  
    boolean relaxedNames() default true;//是否可以松散匹配,至今不知道怎么使用的  
} 

SpringBoot autoconfig原理

springboot启动类注解@SpringBootApplication引入@EnableAutoConfiguration又引入@Import(AutoConfigurationImportSelector.class)

AutoConfigurationImportSelector类中调用SpringFactoriesLoader.loadFactoryNames 方法扫描了所有JAR包的META-INF/spring.factories,如下代码:

protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
   List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
         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;
}
public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
    ……
    Enumeration<URL> urls = (classLoader != null ?
      classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
      ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
    result = new LinkedMultiValueMap<>();
    while (urls.hasMoreElements()) {

spring-boot-autoconfigure包内的spring.factories文件内容

……
work.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
……

包含了所有spring为其增加的自动配置的bean配置,这些bean在满足条件后会被加载到spring上下文中,从而实现了自动配置

eg:

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ RabbitTemplate.class, Channel.class })
@EnableConfigurationProperties(RabbitProperties.class)
@Import(RabbitAnnotationDrivenConfiguration.class)
public class RabbitAutoConfiguration {

会发现RabbitTemplate会报错,为什么这里不存在的类却不报错呢? 1、这个jar编译时这个类是有的,保证编译能过 2、看下ConditionalOnClass注解的注释

The classes that must be present. Since this annotation is parsed by loading class bytecode, > it is safe to specify classes here that may ultimately not be on the classpath, only if this annotation is directly on the affected component and not if this annotation is used as a composed, meta-annotation. In order to use this annotation as a meta-annotation, only use the name attribute. Returns: the classes that must be present

必须出现的类。由于此注释是通过加载类字节码来解析的,因此在此处指定最终可能不在类路径上的类是安全的,前提是此注释直接位于受影响的组件上,而不是将此注释用作组合的元注释。要将此注释用作元注释,请仅使用name属性。

starter

starter就是整理了依赖的maven配置,主要指maven相关依赖配置到单独的一个工程以避免引入过多的maven配置