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

使用SpringBoot自定義starter詳解

 更新時(shí)間:2021年05月10日 11:15:34   作者:天涯不歸客  
這篇文章主要介紹了使用Spring Boot自定義starter詳解,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有很好地幫助喲,需要的朋友可以參考下

一、新建一個(gè)工程

工程由xxx-sprig-boot-starterxxx-sprig-boot-starter-configure兩個(gè)模塊組成;

在這里插入圖片描述

xxx-sprig-boot-starter模塊

  • 只用來(lái)做依賴導(dǎo)入
  • 依賴于 xxx-sprig-boot-starter-configure模塊,沒有實(shí)際代碼
<?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.ander</groupId>
    <artifactId>ander-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--依賴ander-spring-boot-starter-configure工程-->
    <dependencies>
        <dependency>
            <groupId>com.ander</groupId>
            <artifactId>ander-spring-boot-starter-configure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

在這里插入圖片描述

xxx-sprig-boot-starter-configure模塊

  • 專門自動(dòng)配置模塊
  • 依賴于spring-boot-starter-web
<?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.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.ander</groupId>
    <artifactId>ander-spring-boot-starter-configure</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ander-spring-boot-starter-configure</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

在這里插入圖片描述 

二、xxx-sprig-boot-starter-configure模塊自動(dòng)配置編碼

2.1 服務(wù)層編碼

/**
 * Service層
 *
 * @Author: Ander
 * @Date: 2021-05-04
 */
public class HelloService {

    private HelloServiceProperties helloServiceProperties;

    public String helloService(String name) {
        return helloServiceProperties.getPrefix() + " "+ name + " " + helloServiceProperties.getSuffix();
    }

    public HelloServiceProperties getHelloServiceProperties() {
        return helloServiceProperties;
    }

    public void setHelloServiceProperties(HelloServiceProperties helloServiceProperties) {
        this.helloServiceProperties = helloServiceProperties;
    }
}

2.2 屬性配置類編碼

/**
 * 屬性配置類
 *
 * @Author: Ander
 * @Date: 2021-05-04
 */
@ConfigurationProperties(prefix = "com.ander")
public class HelloServiceProperties {

    private String prefix = "hi";
    private String suffix = "hello world";

    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.3 starter自動(dòng)配置類編碼

@EnableConfigurationProperties({HelloServiceProperties.class})作用:讓xxxProperties生效加入到容器中

/**
 * 自定義starter自動(dòng)配置類
 *
 * @Author: Ander
 * @Date: 2021-05-04
 */
@Configuration
@ConditionalOnWebApplication // 指定web應(yīng)用才生效
@EnableConfigurationProperties({HelloServiceProperties.class})
public class HelloServiceAutoConfigure {

    @Autowired
    private HelloServiceProperties helloServiceProperties;

    @Bean
    public HelloService helloService() {
        HelloService helloService = new HelloService();
        helloService.setHelloServiceProperties(helloServiceProperties);
        return helloService;
    }
}

2.4 添加自動(dòng)配置類到META-INF路徑下

在這里插入圖片描述

2.5 將工程安裝到本地

注意先安裝xxx-spring-boot-starter-configure,再安裝xxx-spring-boot-starter

在這里插入圖片描述

三、新建一個(gè)工程測(cè)試自定義starter

3.1 編寫controller層

/**
 * starter測(cè)試控制類
 *
 * @Author: Ander
 * @Date: 2021-05-05
 */
@RestController
public class StarterTestController {

    @Autowired
    private HelloService helloService;

    @GetMapping("hello")
    public String hello(String name) {
        return helloService.helloService(name);
    }
}

3.2 編寫配置文件

server.port=8888
com.ander.prefix=HI
com.ander.suffix=HELLO WORLD

四、測(cè)試結(jié)果

4.1 使用starter默認(rèn)配置

在這里插入圖片描述

4.2 使用自定義配置

在這里插入圖片描述

到此這篇關(guān)于使用Spring Boot自定義starter詳解的文章就介紹到這了,更多相關(guān)Spring Boot自定義starter內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java中的break和continue關(guān)鍵字的使用方法總結(jié)

    Java中的break和continue關(guān)鍵字的使用方法總結(jié)

    下面小編就為大家?guī)?lái)一篇Java中的break和continue關(guān)鍵字的使用方法總結(jié)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-11-11
  • scala 讀取txt文件的方法示例

    scala 讀取txt文件的方法示例

    這篇文章主要介紹了scala 讀取txt文件的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Java動(dòng)態(tài)代理實(shí)現(xiàn)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Java動(dòng)態(tài)代理實(shí)現(xiàn)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    動(dòng)態(tài)代理作為代理模式的一種擴(kuò)展形式,廣泛應(yīng)用于框架(尤其是基于AOP的框架)的設(shè)計(jì)與開發(fā),本文將通過(guò)實(shí)例來(lái)講解Java動(dòng)態(tài)代理的實(shí)現(xiàn)過(guò)程
    2017-08-08
  • 如何自動(dòng)生成Mybatis的Mapper文件詳解

    如何自動(dòng)生成Mybatis的Mapper文件詳解

    這篇文章主要給大家介紹了關(guān)于如何自動(dòng)生成Mybatis的Mapper文件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Mybatis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Spring?cache源碼深度解析

    Spring?cache源碼深度解析

    緩存用于提升系統(tǒng)的性能,特別適用于一些對(duì)資源需求比較高的操作,下面這篇文章主要給大家介紹了關(guān)于Spring?cache源碼的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-03-03
  • Java Builder模式實(shí)現(xiàn)原理及優(yōu)缺點(diǎn)解析

    Java Builder模式實(shí)現(xiàn)原理及優(yōu)缺點(diǎn)解析

    這篇文章主要介紹了Java Builder模式實(shí)現(xiàn)原理及優(yōu)缺點(diǎn)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • 帶你了解Java中的異常處理(下)

    帶你了解Java中的異常處理(下)

    這篇文章主要介紹了Java中的異常處理的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下
    2020-08-08
  • Java String字符串補(bǔ)0或空格的實(shí)現(xiàn)代碼

    Java String字符串補(bǔ)0或空格的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Java String字符串補(bǔ)0或空格的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧
    2016-09-09
  • Java8 Comparator: 列表排序的深入講解

    Java8 Comparator: 列表排序的深入講解

    這篇文章主要給大家介紹了關(guān)于Java 8 Comparator: 列表排序的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java8具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • mybatis中的動(dòng)態(tài)sql問(wèn)題

    mybatis中的動(dòng)態(tài)sql問(wèn)題

    這篇文章主要介紹了mybatis中的動(dòng)態(tài)sql問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02

最新評(píng)論

儋州市| 阳高县| 咸阳市| 仙居县| 永州市| 麻江县| 南开区| 玛纳斯县| 淮南市| 渝中区| 蒙山县| 天峻县| 石屏县| 新乡市| 谷城县| 鸡东县| 牙克石市| 高清| 丹棱县| 嘉定区| 古交市| 双桥区| 赞皇县| 崇左市| 金沙县| 梅州市| 安庆市| 巫溪县| 西畴县| 西乌珠穆沁旗| 新泰市| 墨玉县| 进贤县| 定边县| 上栗县| 石嘴山市| 安达市| 石门县| 札达县| 长海县| 南陵县|