1. 程式人生 > >SpringBoot系列之自定義starter實踐教程

SpringBoot系列之自定義starter實踐教程

SpringBoot系列之自定義starter實踐教程

Springboot是有提供了很多starter的,starter翻譯過來可以理解為場景啟動器,所謂場景啟動器配置了自動配置等等對應業務模組的一個工程,有需要時候直接引入專案就可以,比如需要使用rabbitMQ,直接引入spring-boot-starter-activemq既可,詳細介紹可以參考Springboot官方文件關於starters的介紹

檢視官方文件,可以找到如下的命名規範:

其意思是SpringBoot官方的starter命名要定義為spring-boot-starter-*,自定義或者說第三方的要命名為thirdpartyproject-spring-boot-starter

  • 官方名稱空間
    • 字首:“spring-boot-starter-”
    • 模式:spring-boot-starter-模組名
    • 舉例:spring-boot-starter-web、spring-boot-starter-actuator、spring-boot-starter-jdbc
  • 自定義名稱空間
    • 字尾:“-spring-boot-starter”
    • 模式:模組-spring-boot-starter
    • 舉例:mybatis-spring-boot-starter

ok,SpringBoot是可以自定義一些starter來使用的,可以用於方便專案開發,本部落格以例子的方式介紹:

新建一個沒有過多依賴的SpringBoot工程,spring-boot-starter必須引入,其它的可以Junit可以去掉,application類等等都不用,建議先新建一個Empty project,再引入對應配置的方式進行工程建立

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.springboot</groupId>
    <artifactId>custom-spring-boot-starter-autoconfigurer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>custom-spring-boot-starter-autoconfigurer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

    </dependencies>


</project>

模仿其它starter,新建Properties配置類

package com.example.springboot.starter;

import org.springframework.boot.context.properties.ConfigurationProperties;


@ConfigurationProperties(prefix = "custom.hello")
public class HelloProperties {
    private String prefix;
    private String suffix;

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}

編寫業務測試類:

package com.example.springboot.starter.service;

import com.example.springboot.starter.HelloProperties;

public class HelloService {

    HelloProperties helloProperties;

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    public String sayHello(String name){
        return helloProperties.getPrefix()+"-"+name+helloProperties.getSuffix();
    }
}

編寫自定義的自動配置類:

package com.example.springboot.starter;

import com.example.springboot.starter.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * <pre>
 *  自定義的自動配置類
 * </pre>
 *
 * <pre>
 * @author nicky.ma
 * 修改記錄
 *    修改後版本:     修改人:  修改日期: 2020/01/02 14:31  修改內容:
 * </pre>
 */
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;

    @Bean
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}

必須新建META-INF/spring.factories,然後加入如下配置,自動配置類才可以被掃描到,具體原因可以參考我之前的原始碼學習筆記:Springboot原始碼學習專欄

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.example.springboot.starter.HelloServiceAutoConfiguration

example比較簡單,starter就建立好了,接著需要新建一個web工程來實踐,如圖maven配置,引入custom-spring-boot-starter-autoconfigurer依賴

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.example.springboot</groupId>
            <artifactId>custom-spring-boot-starter-autoconfigurer</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>

新建一個介面測試一下即可:

@Autowired
    HelloService helloService;

@GetMapping(value = {"/sayHello/{name}"})
    @ResponseBody
    public String sayHello(@PathVariable("name")String name){
        return helloService.sayHello(name);
    }

執行專案後,返回連結即可:http://localhost:8082/web/sayHello/testname

例子程式碼下載:github下載鏈