開發一個Spring Boot Starter!


在上一篇文章中,我們已經了解了一個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。


免責聲明!

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



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