1. 程式人生 > 其它 >SpringBoot -- 自定義starter (狂神視訊學習筆記)

SpringBoot -- 自定義starter (狂神視訊學習筆記)

6.1 說明

啟動器模組是一個 空 jar 檔案,僅提供輔助性依賴管理,這些依賴可能用於自動裝配或者其他類庫;

命名歸約:

官方命名:

  • 字首:spring-boot-starter-xxx
  • 比如:spring-boot-starter-web....

自定義命名:

  • xxx-spring-boot-starter
  • 比如:mybatis-spring-boot-starter

6.2 編寫啟動器

1、在IDEA中新建一個空專案 spring-boot-starter-diy

2、新建一個普通Maven模組:kuang-spring-boot-starter

3、新建一個Springboot模組:kuang-spring-boot-starter-autoconfigure

4、點選apply即可,基本結構

5、在我們的 starter 中 匯入 autoconfigure 的依賴!

<!-- 啟動器 -->
<dependencies>
    <!--  引入自動配置模組 -->
    <dependency>
        <groupId>com.kuang</groupId>
        <artifactId>kuang-spring-boot-starter-autoconfigure</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

6、將 autoconfigure 專案下多餘的檔案都刪掉,Pom中只留下一個 starter,這是所有的啟動器基本配置!

7、我們編寫一個自己的服務

package com.kuang;

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();
    }

}

8、編寫HelloProperties 配置類

package com.kuang;

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

// 字首 kuang.hello
@ConfigurationProperties(prefix = "kuang.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;
   }
}

9、編寫我們的自動配置類並注入bean,測試!

package com.kuang;

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;
    }

}

10、在resources編寫一個自己的 META-INF\spring.factories

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.kuang.HelloServiceAutoConfiguration

11、編寫完成後,可以安裝到maven倉庫中!

6.3 新建專案測試我們自己寫的啟動器

1、新建一個SpringBoot 專案

2、匯入我們自己寫的啟動器

<dependency>
    <groupId>com.kuang</groupId>
    <artifactId>kuang-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

3、編寫一個 HelloController 進行測試我們自己的寫的介面!

package com.kuang.controller;

@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @RequestMapping("/hello")
    public String hello(){
        return helloService.sayHello("zxc");
    }

}

4、編寫配置檔案 application.properties

kuang.hello.prefix="ppp"
kuang.hello.suffix="sss"