1. 程式人生 > 實用技巧 >手寫springboot屬於自己的starter原始碼

手寫springboot屬於自己的starter原始碼

1,前言

(1)SpringBoot的優點

SpringBoot是新一代流行的Spring應用開發框架,它具有更多的優點:

  • 建立獨立的Spring應用
  • 內嵌Tomcat、Jetty或Undertow(無需部署war包)
  • 提供自用的starter來簡化構建配置
  • 提供指標監控、執行狀況檢查和外部化配置
  • 沒有程式碼生成,也不需要XML配置(約定大於配置)

(2)SpringBoot-starter的作用

SpringBoot擁有很多方便使用的starter(Spring提供的starter命名規範spring-boot-starter-xxx.jar,第三方提供的starter命名規範xxx-spring-boot-starter.jar),比如spring-boot-starter-log4j、mybatis-spring-boot-starter.jar等,各自都代表了一個相對完整的功能模組。

SpringBoot-starter是一個整合接合器,完成兩件事:

  • 引入模組所需的相關jar包
  • 自動配置各自模組所需的屬性

2,為什麼要自定義starter?

在我們的日常開發工作中,經常會有一些獨立於業務之外的配置模組,我們經常將其放到一個特定的包下,然後如果另一個工程需要複用這塊功能的時候,需要將程式碼硬拷貝到另一個工程,重新整合一遍,麻煩至極。如果我們將這些可獨立於業務程式碼之外的功配置模組封裝成一個個starter,複用的時候只需要將其在pom中引用依賴即可,SpringBoot為我們完成自動裝配,簡直不要太爽。

3,自定義starter的實現方式

首先新建一個maven工程,切記,只是一個maven

工程而已,並非springboot工程!

(1)引入自動配置依賴和自動提示依賴

自動提示就是在properties或yml配置檔案中輸入時自動提示!

	<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>
        <!--輸入properties或yml會自動提示-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>

(2)寫一個TestBean,裝載資訊

public class TestBean {
    private String msg;
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    @Override
    public String toString() {
        return "TestBean{" +
                "msg='" + msg + '\'' +
                '}';
    }
}

(3)寫一個Properties類

該類上面的註解@ConfigurationProperties(prefix = "hello")是獲取配置檔案的配置字首為hello的值,然後賦值給msg變數

@ConfigurationProperties(prefix = "hello")
public class HelloServiceProperties {
    private static final String MSG="hello world";
    private String msg=MSG;
    public static String getMSG() {
        return MSG;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
}

(4)寫一個自動配置類 MyAutoConfiguration

引用定義好的配置資訊;在AutoConfiguration中實現所有starter應該完成的操作,並且把這個類加入spring.factories配置檔案中進行宣告

@Configuration
@ConditionalOnClass({TestBean.class})//判斷當前classpath下是否存在指定類,若是則將當前的配置裝載入spring容器
@EnableConfigurationProperties(HelloServiceProperties.class)//啟用自動配置(指定檔案中的配置)
public class MyAutoConfiguration {
    @Autowired
    HelloServiceProperties helloServiceProperties;//注入測試的配置資訊類
    @Bean
    @ConditionalOnMissingBean(TestBean.class) //當前上下文中沒有TestBean例項時建立例項
    public TestBean getTestService(){
        TestBean testBean=new TestBean();
        testBean.setMsg(helloServiceProperties.getMsg());
        return testBean;
    }
}

完成上面的操作還不夠,最重要的一步是接下來的一步
resources資料夾下新建資料夾META-INF/spring.factories,將上面的自定義配置類MyAutoConfiguration的全路徑名+類名配置到該檔案中(遵循spring.factories的格式),這樣隨著專案的啟動就可以實現自動裝配!
檔案內容

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.ftx.tomcat.bean.MyAutoConfiguration

(5)把本maven專案打包,上傳到maven私服

使用maven構建工具打包自定義starter專案

mvn clean install

然後上傳到maven私服上,再新建一個springboot專案,把私服和starter專案的maven依賴配置到pom檔案中

<dependency>
            <groupId>com.ftx.starter</groupId>
            <artifactId>write-starter</artifactId>
            <version>1.3</version>
        </dependency>
<repositories>
        <repository>
            <id>nexus</id>
            <name>nexus</name>
            <url>http://nexus.tiger2.cn/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

可以找到對應的jar包

(6)測試

在新建的springboot專案中寫一個TestController進行測試
注入TestBean,並使用TestBean

@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    TestBean testBean;
    @RequestMapping("/user")
    public String user(){
        User user=new User();
        user.setName(testBean.getMsg());
        return user.toString();
    }
}

然後啟動專案,訪問http://localhost:8080/test/user

因為TestBeanmsg預設是hello world,可以在配置檔案中自定義TestBeanmsg資訊


這就是自動提示的效果

然後重啟專案,重新訪問http://localhost:8080/test/user

到這裡就已經結束了!還沒看出來starter的好處嗎?