SpringBoot自動配置實現平臺核心服務自動加載

什麼是自動配置

    看下官方原文咋說的,Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added. For example, if HSQLDB is on your classpath, and you have not manually configured any database connection beans, then Spring Boot auto-configures an in-memory database.

    意思就是SpringBoot可以根據我們所添加的包依賴來自動的配置,就比如要使用HSQLDB 的時候,我們添加了HSQLDB 的依賴,SpringBoot會自動配置對應的數據庫連接bean

如何實現自動配置

1. @SpringBootApplication -->@EnableAutoConfiguration-->AutoConfigurationImportSelector通過SpringFactoriesLoader.loadFactoryNames方法加載META-INF/spring.factories文件中的配置信息,這裏面列出了自動化配置相關的類。springboot會掃描這些配置,然後按照清單去嘗試是否需要配置。其實各配置類上有使用@Configuration註解,但是該註解只有在@ComponentScan註解掃描到的情況加纔會進行嘗試配置,但是由於這些類在springboot的jar保內,基本不會被掃描到,因此需要配置到spring.factories中進行掃描加載;

2. 配置類通過@EnableConfigurationProperties將對應的配置信息類實例化,其作用同META-INF/spring.factories文件,因爲配置信息類由springboot維護的,很可能掃描不到;配置類根據條件確定是否需要實例化對應的bean,掃描spring.factories,將其文件包裝成Properties對象,將初始化的bean添加到容器裏邊。

下面來舉個小栗子:

1、新建maven工程,引入必要依賴
    <groupId>fun.codenow</groupId>
    <artifactId>hello-spring-boot-starter</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <description>codenow.fun 平臺自定義 hello starter</description>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.6.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


    具體的依賴

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure-processor</artifactId>
            <scope>compile</scope>
            <optional>true</optional>
        </dependency>

    </dependencies>

2、自定義配置類數據模型:

 @ConfigurationProperties(prefix = "codenow.config.hello")
 public class HelloProperties {
    private String configname;
    private String testvalue;

    public String getConfigname() {
        return configname;
    }

    public void setConfigname(String configname) {
        this.configname = configname;
    }

    public String getTestvalue() {
        return testvalue;
    }

    public void setTestvalue(String testvalue) {
        this.testvalue = testvalue;
    }
 }

3、實現核心服務

public class HelloService {
    private HelloProperties helloProperties;
    public HelloService(){

    }
    public HelloService(HelloProperties helloProperties){
        this.helloProperties=helloProperties;
    }
    public void queryConfigure(){
        System.out.println("configname"+helloProperties.getConfigname()+",testvalue"+helloProperties.getTestvalue());
    }
}

4、自定義自動配置

@Configuration
@EnableConfigurationProperties({HelloProperties.class})
@ConditionalOnClass(HelloProperties.class)
public class HelloServiceAutoConfiguration {
    @Autowired
    private HelloProperties helloProperties;

    @Bean
    @ConditionalOnMissingBean(HelloProperties.class)
    public HelloService helloService(){
        HelloService helloService=new HelloService(helloProperties);
        return helloService;
    }
}

5、制定自動配置類的加載路徑 META-INF/spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  fun.codenow.hello.autoconfigure.HelloServiceAutoConfiguration

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章