亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用SpringBoot自定義starter詳解

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

一、新建一個工程

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

在這里插入圖片描述

xxx-sprig-boot-starter模塊

  • 只用來做依賴導入
  • 依賴于 xxx-sprig-boot-starter-configure模塊,沒有實際代碼
<?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模塊

  • 專門自動配置模塊
  • 依賴于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模塊自動配置編碼

2.1 服務層編碼

/**
 * 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自動配置類編碼

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

/**
 * 自定義starter自動配置類
 *
 * @Author: Ander
 * @Date: 2021-05-04
 */
@Configuration
@ConditionalOnWebApplication // 指定web應用才生效
@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 添加自動配置類到META-INF路徑下

在這里插入圖片描述

2.5 將工程安裝到本地

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

在這里插入圖片描述

三、新建一個工程測試自定義starter

3.1 編寫controller層

/**
 * starter測試控制類
 *
 * @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

四、測試結果

4.1 使用starter默認配置

在這里插入圖片描述

4.2 使用自定義配置

在這里插入圖片描述

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

相關文章

  • Springboot中靜態(tài)文件的兩種引入方式總結

    Springboot中靜態(tài)文件的兩種引入方式總結

    這篇文章主要介紹了Springboot中靜態(tài)文件的兩種引入方式總結,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • SpringSecurity OAtu2+JWT實現(xiàn)微服務版本的單點登錄的示例

    SpringSecurity OAtu2+JWT實現(xiàn)微服務版本的單點登錄的示例

    本文主要介紹了SpringSecurity OAtu2+JWT實現(xiàn)微服務版本的單點登錄的示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Nacos封裝通用HttpClient詳解

    Nacos封裝通用HttpClient詳解

    這篇文章主要介紹了Nacos封裝通用HttpClient用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Java ArrayList 實現(xiàn)實例講解

    Java ArrayList 實現(xiàn)實例講解

    ArrayList是基于數(shù)組實現(xiàn)的,是一個動態(tài)數(shù)組,其容量能自動增長,類似于C語言中的動態(tài)申請內存,動態(tài)增長內存。這篇文章主要介紹了java ArrayList 實現(xiàn)的相關資料,需要的朋友可以參考下
    2016-11-11
  • Jdk1.8 HashMap實現(xiàn)原理詳細介紹

    Jdk1.8 HashMap實現(xiàn)原理詳細介紹

    這篇文章主要介紹了Jdk1.8 HashMap實現(xiàn)原理詳細介紹的相關資料,需要的朋友可以參考下
    2016-12-12
  • SpringBoot采用AJAX實現(xiàn)異步發(fā)布帖子詳解

    SpringBoot采用AJAX實現(xiàn)異步發(fā)布帖子詳解

    Ajax是一種web應用技術,可以借助客戶端腳本(javascript)與服務端應用進行異步通訊,獲取服務端數(shù)據(jù)以后,可以進行局部刷新,進而提高數(shù)據(jù)的響應和渲染速度。所有的Ajax請求都會基于DOM(HTML元素)事件,通過XHR(XMLHttpRequest)對象實現(xiàn)與服務端異步通訊局部更新
    2022-08-08
  • java中TCP/UDP詳細總結

    java中TCP/UDP詳細總結

    本篇文章對Java中的TCP/UDP知識點進行了歸納總結分析。需要的朋友參考下
    2017-04-04
  • Idea jdk版本問題解決方案

    Idea jdk版本問題解決方案

    這篇文章主要介紹了Idea jdk版本問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • Java微信公眾平臺之群發(fā)接口(高級群發(fā))

    Java微信公眾平臺之群發(fā)接口(高級群發(fā))

    這篇文章主要為大家詳細介紹了Java微信公眾平臺之群發(fā)接口,高級群發(fā)功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • 解決Eclipse中java文件的圖標變成空心J的問題

    解決Eclipse中java文件的圖標變成空心J的問題

    這篇文章主要介紹了解決Eclipse中java文件的圖標變成空心J的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01

最新評論