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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM