SpringBoot自定義啟動類 starter


SpringBoot自定義啟動類 starter

starter機制

SpringBoot中整合了很多的第三方依賴,使用起來只需要配置依賴和配置屬性就可直接使用,非常方便。

我們在開發中可能會遇到一個模塊多個場景重復使用的情況,這時就可以吧模塊抽象出來,自定義成啟動類,在配置文件中進行相關配置就可使用。

第一步 創建項目

創建兩個項目(maven項目和SpringBoot項目),也可以創建一個空項目添加兩個模塊。

創建一個空項目

添加兩個模塊

一個為啟動類maven項目 hancy-hello-spring-boot-starter

一個是自動配置的springboot項目 hancy-hello-spring-boot-starter-autoconfigure

第二步 修改pom.xml文件

hancy-hello-spring-boot-starter項目中添加以下配置

<dependencies>
        <dependency>
            <groupId>com.hancy</groupId>
            <artifactId>hancy-hello-spring-boot-starter-autoconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

hancy-hello-spring-boot-starter-autoconfigure 項目中添加以下配置(刪除其他多余配置,尤其是test測試配置和插件配置,運行時可能會找不到依賴報錯)

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.3.2.RELEASE</version>
        </dependency>

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

第三步 編寫配置類

1、編寫配置文件類

@ConfigurationProperties(prefix = "hancy.hello") //啟動配置文件
public class HelloProperties {
    private String prefix;
    private String suffix;

    public HelloProperties() {
    }

    public HelloProperties(String prefix, String suffix) {
        this.prefix = prefix;
        this.suffix = 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;
    }
}

2、編寫service文件,訪問方法

public class HelloService {

    @Autowired //自動注入配置文件類
    HelloProperties helloProperties;

    public String hello(String name){
        return helloProperties.getPrefix() + ":" + name + helloProperties.getSuffix();
    }
}

3、編寫自動注入配置類

@Configuration
@ConditionalOnWebApplication //web應用才生效
@EnableConfigurationProperties(HelloProperties.class) //啟動配置類
public class HelloServiceAutoConfiguration {
    @Autowired
    HelloProperties helloProperties;

    public HelloServiceAutoConfiguration() {
    }

    @Bean
    public HelloService helloService(){
        return new HelloService();
    }
}

第四步 編寫resource目錄下新建META-INF文件夾,在其下的spring.factories文件

此配置文件是springboot源碼規定讀取其中配置內容的自動配置項,想要通過配置文件配置,必須配置此文件。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.hancy.auto.HelloServiceAutoConfiguration (這里寫全類名,有多個事,使用逗號,隔開)

第五步 將啟動器模塊,和自動配置模塊install到倉庫

先install自動配置,在install啟動模塊

第六步 在其他應用的pom.xml文件中添加啟動器模塊的依賴

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

第七步 在配置文件中添加配置

hancy:
  hello:
    prefix: 你好呀!
    suffix: 歡迎!

第八步 編寫controller驗證

@RestController
@RequestMapping("/test")
public class HelloController {

    @Autowired
    private HelloService helloService;


    @GetMapping("/hello")
    public String handle(){
        return helloService.hello("hancy");
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM