溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

springboot自定義starter啟動器怎么用

發(fā)布時間:2021-09-03 13:14:25 來源:億速云 閱讀:135 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了“springboot自定義starter啟動器怎么用”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“springboot自定義starter啟動器怎么用”這篇文章吧。

第一步、創(chuàng)建 xxx-spring-boot-starter 的spring Initializr模塊

springboot自定義starter啟動器怎么用

springboot自定義starter啟動器怎么用

springboot自定義starter啟動器怎么用

springboot自定義starter啟動器怎么用

第二步、刪除不需要的內(nèi)容(啟動類、除下面spring-boot-starter的其它依賴,maven編譯插件)

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

如下是完整的pom.xml
實際上如果當(dāng)前starter需要引用其它依賴加入到dependences里面即可,這里只做演示項目

<?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.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>top.huashengshu</groupId>
    <artifactId>my-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>my-spring-boot-starter</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <!--   保留這個依賴即可,其它依賴都刪除     -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

    </dependencies>
</project>

項目結(jié)構(gòu)截圖

springboot自定義starter啟動器怎么用

第三步、寫代碼,對外提供一些自己寫的類

創(chuàng)建HelloProperties.java,直接復(fù)制下面代碼,然后選擇包進(jìn)行粘貼,Idea會自動創(chuàng)建對應(yīng)類代碼設(shè)置好包名

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

@ConfigurationProperties(prefix = "hello") // 對外提供的前綴,相當(dāng)于其它引入當(dāng)前starter在properties文件使用hello.屬性即可對下面屬性進(jìn)行賦值
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;
    }
}

創(chuàng)建HelloService.java直接復(fù)制下面代碼,選擇包進(jìn)行粘貼即可生成

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

創(chuàng)建配置類(和前面一樣復(fù)制粘貼即可)HelloServiceAutoConfiguration.java,將HelloService注入到IOC容器中

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環(huán)境下才生效,相關(guān)的其它條件可以使用@ConditionXXX
@EnableConfigurationProperties(HelloProperties.class) // 表示HelloProperties作為配置類使用
public class HelloServiceAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;	// 作為配置類目的就是想在sayHello方法返會的字符串加上前綴和后綴

    @Bean
    public HelloService helloService() { // 將HelloService注入到IOC容器
        HelloService service = new HelloService();
        service.setHelloProperties(helloProperties);
        return service;
    }
}

第四步、在resources資源文件夾下創(chuàng)建一個META-INF文件夾,并創(chuàng)建一個spring.factories文件

如下面截圖

springboot自定義starter啟動器怎么用

內(nèi)容則是將@Configuration配置類加入,目的是將配置加入到外部的IOC容器中

org.springframework.boot.autoconfigure.EnableAutoConfiguration=

idea中右鍵copy–》copy reference,將復(fù)制的值填入上面=右邊

注意:如果有多個AutoConfiguration則用逗號分開,還有回車小心前面的空格,最好沒有其它字符。

springboot自定義starter啟動器怎么用

例如:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
top.huashengshu.myspringbootstarter.HelloServiceAutoConfiguration,\
top.yumbo.music.starter.configuration.YumboMusicAutoConfiguration

第五步、將該項目發(fā)布的maven倉庫,或者安裝到本地倉庫中讓其它項目能使用的到

本地安裝為例:

springboot自定義starter啟動器怎么用

成功后即可

springboot自定義starter啟動器怎么用

第六步、測試自己定義的啟動器使用有效

創(chuàng)建一個springboot項目
勾選web模塊即可,然后加入自定義啟動器的gav依賴
在啟動類中加入內(nèi)部類(這里為了方便演示不按照規(guī)范創(chuàng)建包)
如下示例代碼

啟動類

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import top.huashengshu.myspringbootstarter.HelloService;

@SpringBootApplication
public class DemoApplication {
	
    @RestController
    public class HelloController {

        @Autowired
        HelloService helloService; // 注入HelloService

        @GetMapping("/hello") // 暴露一個/hello 請求路徑對外提供服務(wù)
        public String hello(){
            return helloService.sayHello("zhang san"); // 返回帶有前綴和后綴中間是 "zhang san"的字符串
        }

    }
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

properties文件

因為使用了@ConfigurationProperties(prefix = "hello")注解所以在當(dāng)前項目的properties文件中使用hello前綴調(diào)用即可對成員屬性賦值

springboot自定義starter啟動器怎么用

如下

hello.prefix=HUASHENGSHU
hello.suffix=Hello World

運行當(dāng)前項目,訪問/hello驗證是否有效

如下:

springboot自定義starter啟動器怎么用

說明自定義starter成功。

其它業(yè)務(wù)代碼,根據(jù)自己的需求自己加入依賴,也就是說可以自己定義starter提供給其它人用!

以上是“springboot自定義starter啟動器怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI