天天看點

年輕人的第一個自定義 Spring Boot Starter!

年輕人的第一個自定義 Spring Boot Starter!

陸陸續續,零零散散,棧長已經寫了幾十篇 Spring Boot 系列文章了,其中有介紹到 Spring Boot Starters 啟動器,使用的、介紹的都是第三方的 Starters ,那如何開發一個自己的 Spring Boot Starter 呢?

下面帶大家開發一個年輕人的第一個 Spring Boot Starter!

不知道 Starters 為何物的請進這個傳送門===>Spring Boot Starters 啟動器,看完有了學習基礎,我們再繼續下面的自定義 Starter 實戰!

一、自定義 Starter 必備元件

一個完整的 Spring Boot Starter 需要包含以下元件:

包含自動配置代碼的自動配置子產品;參考:Spring Boot自動配置原理、實戰。

Starter子產品提供對自動子產品的依賴關系,和相關依賴庫,以及任何需要用到的依賴。簡而言之,就是,添加一個 Starter 就應該提供使用該 Starter 所需的一切;

二、建立一個自定義Starter

怎麼建立 Spring Boot 項目就不說了,之前也分享過,參考:年輕人的第一個 Spring Boot 應用!。

這個自定義 Starter 就實作一個根據屬性的值是否配置Bean。

1、建立自動配置類

package cn.javastack.springboot.starter.config;
 
import cn.javastack.springboot.starter.service.TestService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
@ConditionalOnProperty(prefix = "javastack.starter", name = "enabled", havingValue = "true")
public class TestServiceAutoConfiguration {
 
    @Bean
    public TestService testService() {
        return new TestService();
    }
 
}      

這個自動配置類很簡單,就是根據是否有

javastack.starter.enabled=true

這個參數的值再配置一個Bean。

TestService

示例如下:

package cn.javastack.springboot.starter.service;
 
public class TestService {
 
    public String getServiceName() {
        return "Java技術棧";
    }
 
}      

這個類就有一個方法

getServiceName

,它就傳回一個字元串:

Java技術棧

2、允許自動配置

建立

META-INF/spring.factories

檔案,添加這個允許自動配置的類。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.javastack.springboot.starter.config.TestServiceAutoConfiguration      

三、測試這個自定義Starter

上面的自定義 Starter 項目建好後,可以來測試一下它是否生效了。

一般是把它打成 jar 包上傳到 Maven 倉庫,供其他同僚調用,這裡我們本報打完包之後再引用它。

1、添加依賴

建立一個 Spring Boot 測試項目,添加這個自定義 Starter 依賴,Maven 依賴如下:

<dependencies>
    <dependency>
        <groupId>cn.javastack</groupId>
        <artifactId>javastack-spring-boot-starter</artifactId>
        <version>1.0</version>
        <scope>compile</scope>
    </dependency>
</dependencies>      

2、添加測試方法

1.      
package cn.javastack.springboot.starter.sample;
 
import cn.javastack.springboot.starter.service.TestService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
 
/**
 * 微信公衆号:Java技術棧
 */
@SpringBootApplication
public class Application {
 
    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
 
    @Bean
    public CommandLineRunner commandLineRunner(TestService testService) {
        return (args) -> {
            System.out.println(testService.getServiceName());
        };
    }
 
}      

這個方法的作用是,項目啟動後,調用 TestService 的執行個體方法,輸出方法的值。

關于 CommandLineRunner 有不懂的可以看這篇文章:Spring Boot Runner 啟動器。

3、開啟配置

我們知道這個自定義 Starter 中需要有 javastack.starter.enabled=true 這個參數的值的,是以我們在 application.yml 配置檔案中添加這個配置:

javastack:
  starter:
    enabled: true      

4、運作測試

運作 Application 類的 main 方法,最後會輸出結果:Java技術棧。

當我們把配置改為:

javastack:
  starter:
    enabled: false      

此時,運作報錯,因為沒有這個執行個體啊,自動配置類隻有為 true 時才會配置。

四、總結

本章棧長簡單示範了如何自定義一個 Spring Boot Starter,根據某個參數的值來決定是否自動配置,其實還可以根據是否有某個類、某個Bean……等,可以看下《Spring Boot 最核心的 25 個注解》這篇文章對應的 ConditionOnXXX 系列注解。

其實了解了 Spring Boot 自動配置的原理,自定義一個 Starter 并不難,你可以在這個執行個體基礎上靈活擴充。

本文完整的代碼執行個體 Github 位址:

https://github.com/javastacks/spring-boot-best-practice

趕緊關注加星吧,已經更新一大堆教程了,後續這個教程會持續更新……