開發一個Spring Boot Starter!

NinWoo發表於2019-08-05

在上一篇文章中,我們已經瞭解了一個starter實現自動配置的基本流程,在這一小結我們將復現上一過程,實現一個自定義的starter。

先來分析starter的需求:

  • 在專案中新增自定義的starter依賴,自動在Spring中載入starter中的Bean;
  • 從application.properties中載入指定配置

建立專案

  1. 先建立一個名為starter的專案。
<?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>top.ninwoo</groupId>
    <artifactId>demo-starter</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>
    </dependencies>
</project>
  1. 在resources中建立一個META-INF的目錄,並在目錄中建立一個spring.factories。在這個配置中,我們只設定一個EnableAutoConfiguration項,並且對應只設定一個DemoAutoConfig配置類。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=top.ninwoo.config.DemoAutoConfig  
  1. 建立DemoAutoConfig配置類

    package top.ninwoo.config;
    
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @EnableConfigurationProperties(DemoStarterProperties.class)
    public class DemoAutoConfig {
    
        @Bean
        DemoBean demoBean() {
            return new DemoBean();
        }
    }

    這個配置類,我們主要使用了@Configuration和@EnableConfigurationProperties兩個註解。@EnableConfigurationProperties啟用一個ConfigurationProperties。

  2. 建立ConfigurationProperties對應的DemoStarterProperties

    package top.ninwoo.config;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    @ConfigurationProperties(prefix = "top.ninwoo.demo")
    public class DemoStarterProperties {
    
        private String name = "default";
        private int age = 0;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }

    建立一個ConfigurationProperties類。這個類主要用來從application.properties中讀取配置項,並自動設定到相對應的欄位上。

  3. 建立一個測試用Bean,並使用ConfigurationProperties類中的資訊。

    起初這裡有個疑惑,不知道如何使用這個ConfigurationProperties類。不過在spring中最常見的就是Bean,我們可以大膽的猜測通過@ConfigurationProperties註釋的類,將自動在Spring容器中自動建立一個Bean。而我們在使用的時候,就通過普通的bean注入方式便可以使用ConfigurationProperties類中的資訊。所以,我們這樣建立一個測試Bean

    package top.ninwoo;
    
    import javax.annotation.Resource;
    
    public class DemoBean {
        @Resource
        DemoStarterProperties properties;
    
        public String getName() {
            return properties.getName();
        }
    
        public String getAge() {
            return getAge();
        }
    }
    

    同時在DemoAutoConfig中使用@Bean註解建立一個Bean。

到這裡,我們的starter就建立完成了。通過mvn打包,或者建立同一個父專案的不同子Module的方式,我們可以進行測試這個starter是否生效。

建立測試類

測試類使用一個spring boot web專案來完成,主要建立了一個RestController,並通過RestController獲取Spring上下文中註冊的bean names和starter中的測試Bean。

pom.xml

<?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>top.ninwoo</groupId>
    <artifactId>springboot-demo</artifactId>
    <version>1.0.0</version>

    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.1.6.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>top.ninwoo</groupId>
            <artifactId>demo-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

在pom檔案中,我們新增了剛剛實現的starter。

RestController:

@RestController
public class IndexController implements ApplicationContextAware {

    ApplicationContext ctx = null;

    @Resource
    DemoBean demoBean;

    @RequestMapping("/getList")
    public String[] getBeanNames() {
        return ctx.getBeanDefinitionNames();
    }

    @RequestMapping("/getDemoBean")
    public String demoBean() {
        return demoBean.getName();
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ctx = applicationContext;
    }
}

SpringBoot啟動類 MainApp:

package top.ninwoo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainApp {

    public static void main(String[] args) {
        SpringApplication.run(MainApp.class, args);
    }
}

我們可以看到,與正常的一個web專案相比,我們只是新增了一個依賴,而並沒有修改啟動類。

測試

訪問127.0.0.1:8080/getList介面,我們可以看到最後的幾個bean Names是:

...,"top.ninwoo.config.DemoAutoConfig","demoBean","top.ninwoo.demo-top.ninwoo.config.DemoStarterProperties"]

這證明,通過注入我們starter依賴,已經在Spring的上下文建立了starter配置類中的Bean。

在沒有設定application.properties時,直接訪問http://127.0.0.1:8080/getDemoBean,可以獲取到測試用的Bean例項中預設的引數配置default.

新增application.properties:

top.ninwoo.demo.name=joliu

重啟專案,再次訪問該介面,發現測試用的Bean例項對應的屬性已經安裝配置類中的引數進行設定,返回了joliu。

小結

到這裡,我們可以說已經瞭解了開發一個SpringBoot Starter最基本的流程,我們可以嘗試在我們日常的專案中開發這樣的starter。

相關文章