SpringBoot自动装配,实现自定义配置

定义

基于约定大于配置的原则,实现Spring组件自动装配的目的

装配的依赖(方式)

模式注解、@Enable模块、条件装配、工厂加载机制

激活自动化装配、实现自动化装配、配置自动装配实现

底层装配技术

  • Spring 模式注解装配
  • Spring @Enable模块装配
  • Spring条件装配
  • Spring工厂加载机制
    • 实现类: SpringFactoriesLoader
    • 配置资源:META-INF/spring.factories

实现方式

  1. 激活自动装配
    比如 使用@EnableAutoConfiguration
  2. 实现自动装配
    通过定义 ***AutoConfiguration注解
  3. 配置自动装配
    META-INF/spring.factories文件中进行配置
    # Auto Configure
    // 这里是配置自动装配的注解, 下面都是这个自动装配需要装配的东西,通过这种配置的方式,只要使用EnableAutoConfiguration注解,就会将下面的这些配置全部自动装配进去
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=
    org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,
    org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,
    org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,
    org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,
    org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,
    org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,
    org.springframework.boot.autoconfigure.cloud.CloudServiceConnectorsAutoConfiguration,
    org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,
    org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,
    org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,
    org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,
    org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,
    org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,
    org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveDataAutoConfiguration,
    org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveRepositoriesAutoConfiguration,
    org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,
    org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration,
    

自定义自动装配

  1. 激活自动装配
//新建了一个启动类,使用这个注解
//这里我需要获取helloWorld的bean,所以我需要在将helloWorld的bean通过这个注解进行自动装配
@EnableAutoConfiguration
public class EnableAutoConfigurationBootStrap {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = new SpringApplicationBuilder(EnableAutoConfigurationBootStrap.class)
                .web(WebApplicationType.NONE)
                .run(args);

        String helloWorld = context.getBean("helloWorld", String.class);
        System.out.println("helloWorld:" + helloWorld);
    }
}
  1. 实现自动装配
    新建了一个HelloWorldAutoConfiguration
@Configuration  // 模式注解装配
@EnableHelloWorldConfiguration  // @Enable模块装配,这个装配是会生成一个helloWorldBean的
@TestCondition(name = "user.name", value = "Administrator") // 条件装配
public class HelloWorldAutoConfiguration {
}

这里的执行顺序是:

  • 先通过条件装配,如果条件装配不成立就不会进行下一步的操作
  • 条件装配成立后,进入EnableHelloWorldConfiguration ,使用@import导入了配置bean
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(HelloWorldConfiguration.class)
public @interface EnableHelloWorldConfiguration {
}

public class HelloWorldConfiguration {
    @Bean
    public String helloWorld() {
        return "hello World!";
    }
}
  • 这里其实到这一步已经可以将bean获取到了,不需要使用Configuration,只是为了说明也可以用这个方法生成bean
  1. 配置自动装配
    resources目录下新建META-INF文件夹,在下面新建spring.factories文件,在这里面进行配置,将EnableHelloWorldConfiguration 绑定给@EnableAutoConfiguration
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.imooc.study.configration.HelloWorldAutoConfiguration

通过上面的三个步骤就能实现简单的自动装配,SpringBoot的自动装配其实是基于SpringFramework的实现,在这个基础上添加了spring.factories

原文地址:https://www.cnblogs.com/bolbo/p/10550803.html