最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot自定義starter啟動器的具體使用實(shí)踐

 更新時間:2021年09月03日 11:48:09   作者:詩水人間  
本文主要介紹了springboot自定義starter啟動器的具體使用實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

在這里插入圖片描述

填寫group 和 artifact其它默認(rèn)即可

在這里插入圖片描述

在這里插入圖片描述

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

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

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

<?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>

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

在這里插入圖片描述

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

創(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文件

如下面截圖

在這里插入圖片描述

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

org.springframework.boot.autoconfigure.EnableAutoConfiguration=

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

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

在這里插入圖片描述

例如:

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

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

本地安裝為例:

在這里插入圖片描述

成功后即可

在這里插入圖片描述

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

創(chuàng)建一個springboot項(xiàng)目
勾選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文件

因?yàn)槭褂昧?code>@ConfigurationProperties(prefix = "hello")注解所以在當(dāng)前項(xiàng)目的properties文件中使用hello前綴調(diào)用即可對成員屬性賦值

在這里插入圖片描述

如下

hello.prefix=HUASHENGSHU
hello.suffix=Hello World

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

如下:

在這里插入圖片描述

說明自定義starter成功。

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

到此這篇關(guān)于springboot自定義starter啟動器的具體使用實(shí)踐的文章就介紹到這了,更多相關(guān)springboot自定義starter啟動器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

丹棱县| 峨山| 红河县| 柏乡县| 长白| 丘北县| 龙山县| 当阳市| 安阳县| 山东省| 龙口市| 文成县| 巴彦淖尔市| 浏阳市| 昌图县| 聂荣县| 高密市| 三江| 利津县| 屏东县| 峨眉山市| 南京市| 夹江县| 甘南县| 吉隆县| 万荣县| 方城县| 巩留县| 禹州市| 林芝县| 泊头市| 柘城县| 福贡县| 连山| 木兰县| 洪江市| 嵩明县| 桃园市| 嵊州市| 城固县| 北京市|