SpringBoot-定義自己的auto-configuration

藍星花發表於2018-10-07

1.前言

Spring Boot內部定義了各種各樣的xxxAutoConfiguration配置類,預先定義好了各種所需的Bean。只有在特定的情況下這些配置類才會被起。 那我們如何定義一個自己的Configuration呢?順便了解一下原理?


2.那我們開始吧,哈哈哈

2.1 新建一個Maven工程

在這裡插入圖片描述

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hello</artifactId>
<version>1.0-SNAPSHOT</version>

2.2 修改pom檔案
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-hello</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>1.4.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>1.4.3.RELEASE</version>
            <optional>true</optional>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.3 建立HelloServiceProperties類,自動屬性配置類

在這裡插入圖片描述

package com.cxx.springboot.starter.hello;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
 * User: lanxinghua
 * Date: 2018/10/7 21:36
 * Desc: hello 自動配置屬性類
 */
@ConfigurationProperties(prefix = "hello")
public class HelloServiceProperties {
    private static final String MSG = "world";
    private String msg = MSG;

    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
}


2.4 建立HelloService,服務類
package com.cxx.springboot.starter.hello;

/**
 * User: lanxinghua
 * Date: 2018/10/7 21:38
 * Desc: 服務類
 */
public class HelloService {
    private String msg;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String sayHello(){
        return "hello" + msg;
    }
}


2.5 建立HelloServiceAutoConfiguration,自動配置類
package com.cxx.springboot.starter.hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * User: lanxinghua
 * Date: 2018/10/7 21:40
 * Desc: 自動配置類
 */
@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(HelloService.class) //判斷該類在類路徑下是否存在
@ConditionalOnProperty(prefix = "hello", value = "ebabled", matchIfMissing = true)
public class HelloServiceAutoConfiguration {
    @Autowired
    private HelloServiceProperties helloServiceProperties;

    @Bean
    @ConditionalOnMissingBean(HelloService.class)
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        helloService.setMsg(helloServiceProperties.getMsg());
        return helloService;
    }
}

2.6 註冊配置

1、在src/main/resources新建META-INF資料夾
2、在META-INF資料夾下新建spring.factories檔案
3、註冊配置自動配置類
在這裡插入圖片描述

spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.cxx.springboot.starter.hello.HelloServiceAutoConfiguration

2.7 將上面構建的starter安裝到本地
mvn clean install

3.使用自定義的Configuration

新建一個springboot專案,新增依賴
在這裡插入圖片描述

在這裡插入圖片描述

測試一下:

package com.example;
import com.cxx.springboot.starter.hello.HelloService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@SpringBootApplication
public class DesignsApplication {
    @Resource
    private HelloService helloService;
    @RequestMapping("/test")
    public String test(){
        return helloService.sayHello();
    }
    public static void main(String[] args) {
        SpringApplication.run(DesignsApplication.class, args);
    }
}

在這裡插入圖片描述

相關文章