国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

使用SpringBoot自定義starter詳解

發(fā)布時(shí)間:2021-07-06 11:13 來(lái)源:腳本之家 閱讀:0 作者:天涯不歸客 欄目: 開(kāi)發(fā)技術(shù)

目錄

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

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

xxx-sprig-boot-starter模塊

  • 只用來(lái)做依賴(lài)導入
  • 依賴(lài)于 xxx-sprig-boot-starter-configure模塊,沒(méi)有實(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>

    <!--依賴(lài)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模塊

  • 專(zhuān)門(mén)自動(dòng)配置模塊
  • 依賴(lài)于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 屬性配置類(lèi)編碼

/**
 * 屬性配置類(lèi)
 *
 * @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)配置類(lèi)編碼

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

/**
 * 自定義starter自動(dòng)配置類(lèi)
 *
 * @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 添加自動(dòng)配置類(lèi)到META-INF路徑下

2.5 將工程安裝到本地

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

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

3.1 編寫(xiě)controller層

/**
 * starter測試控制類(lèi)
 *
 * @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 編寫(xiě)配置文件

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

四、測試結果

4.1 使用starter默認配置

4.2 使用自定義配置

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

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。

国产精品久久国产三级国| 久久亚洲中文字幕伊人久久大| 人妻夜夜爽天天爽三区丁香花| 野外做受又硬又粗又大视频| 亚洲熟妇久久精品| 97人妻无码一区二区精品免费|