1. 程式人生 > 程式設計 >springboot自定義starter實現過程圖解

springboot自定義starter實現過程圖解

這篇文章主要介紹了springboot自定義starter實現過程圖解,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

1、建立一個Empty Project

2、在該工程中點選+,選擇new module,新建一個maven工程

springboot自定義starter實現過程圖解

springboot自定義starter實現過程圖解

springboot自定義starter實現過程圖解

點選確定。

3、在該工程中點選+,選擇new module,新建一個Spring Initializr工程

springboot自定義starter實現過程圖解

後面直接預設next,然後點選finishi。

兩個都建立完畢之後點選apply,點選OK。得到如下結構:

springboot自定義starter實現過程圖解

4、在gong-spring-boot-starter中引入gong-spring-boot-starter-autoconfigurer,即在gong-spring-boot-starter的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>com.gong.starter</groupId>
  <artifactId>gong.spring-boot-starter</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
    <!--引入自動配置模組-->
    <dependency>
      <groupId>com.gong.starter</groupId>
      <artifactId>gong-spring-boot-starter-autoconfigurer</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>

我們看下gong-spring-boot-starter-autoconfigurer中的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.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.gong.starter</groupId>
  <artifactId>gong-spring-boot-starter-autoconfigurer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>gong-spring-boot-starter-autoconfigurer</name>
  <description>Demo project for Spring Boot</description>

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

  <dependencies>
    <!--引入spring-boot-starter:所有starter基本配置-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>

  </dependencies>

</project>

刪除掉建立專案時自動配置的其它東西,只需要一個標紅的依賴即可。

5、在gong-spring-boot-starter-autoconfigurer就可以編寫場景啟動的相關邏輯啦。

(1)新建如下目錄結構及檔案

springboot自定義starter實現過程圖解

springboot啟動入口可以刪去,resources下檔案刪去,test資料夾刪去。在com.gong.starter下新建以上三個java檔案,在resources下新建META-INF資料夾,再新建spring.factories檔案。

HelloService.java

package com.gong.starter;

public class HelloService {

  HelloProperties helloProperties;

  public HelloProperties getHelloProperties() {
    return helloProperties;
  }

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

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

sayHelloGong方法可以獲取HelloProperties中的屬性,包括字首和字尾,然後返回:字首-name字尾。

HelloProperties.java

package com.gong.starter;

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

//繫結所有以gong.hello開頭的配置
@ConfigurationProperties(prefix = "gong.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;
  }
}

繫結配置以及定義屬性。

HelloServiceAutoConfiguration.java

package com.gong.starter;

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;


@Configuration //是一個配置類
@ConditionalOnWebApplication //web應用才生效
@EnableConfigurationProperties(HelloProperties.class) //讓屬性檔案生效
public class HelloServiceAutoConfiguration {

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

自動配置類。要加入三個註解,並對方法使用@Bean標註。

最後,就是在spring.factories中進行配置自動註冊:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.gong.starter.HelloServiceAutoConfiguration

這樣,我們自己定義的場景啟動器就完成了。

接下來新建一個springboot專案進行測試,首先在pom.xml中匯入自己定義的場景啟動器:

<!--引入自定義的starter-->
    <dependency>
      <groupId>com.gong.starter</groupId>
      <artifactId>gong.spring-boot-starter</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

然後編寫application.properties定義場景啟動器的屬性:

gong.hello.prefix=GONG
gong.hello.suffix=HELLO WORLD

接著編寫一個測試類:

package com.gong.springbootjpa.controller;

import com.gong.starter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

  @Autowired
  HelloService helloService;

  @GetMapping("/hello")
  public String hello(){
    return helloService.sayHelloGong("haha");
  }
}

啟動伺服器:

springboot自定義starter實現過程圖解

完美。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。