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

springboot starter自定義實現(xiàn)公共模塊方式

 更新時間:2024年08月27日 14:13:37   作者:程序猿20  
這篇文章主要介紹了springboot starter自定義實現(xiàn)公共模塊方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

SpringBoot中的starter是一種非常重要的機制,能夠拋棄以前繁雜的配置,將其統(tǒng)一集成進starter,應用者只需要在maven中引入starter依賴,SpringBoot就能自動掃描到要加載的信息并啟動相應的默認配置。

我們將可獨立于業(yè)務代碼之外的功配置模塊封裝成一個個starter,復用的時候只需要將其在pom中引用依賴即可,SpringBoot為我們完成自動裝配。

比如登錄模塊,基于aop的日志模塊等。

SpringBoot提供的starter以spring-boot-starter-xxx的方式命名的。官方建議自定義的starter使用xxx-spring-boot-starter命名規(guī)則。以區(qū)分SpringBoot生態(tài)提供的starter。

以下是定義一個starter的步驟:

1. 建立一個父項目

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.9.RELEASE</version>
    </parent>

    <groupId>org.example</groupId>
    <artifactId>springboot-starter-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>springboot-starter-config</module>
        <module>springboot-starter-application</module>
    </modules>

    <packaging>pom</packaging>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

2. 新建一個maven module項目作為自定義starter項目

pom.xml如下:

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

    <parent>
        <artifactId>springboot-starter-demo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springboot-starter-config</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

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

</project>

3. 定義屬性類

package com.demo;

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

/**
 * 描述:配置信息 實體
 *
 **/
@ConfigurationProperties(prefix = "demo")
public class DemoProperties {
    private String params1;
    private String params2;
    private Integer minLength;


    public String getParams1() {
        return params1;
    }

    public void setParams1(String params1) {
        this.params1 = params1;
    }

    public String getParams2() {
        return params2;
    }

    public void setParams2(String params2) {
        this.params2 = params2;
    }

    public Integer getMinLength() {
        return minLength;
    }

    public void setMinLength(Integer minLength) {
        this.minLength = minLength;
    }
}

4. 定義服務類

package com.demo.service;

import com.demo.DemoProperties;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * author:HUAWEI
 */
public class DemoService {

    @Autowired
    private DemoProperties demoProperties;

    public String params1;
    public String params2;

    public DemoService(String param1, String param2) {
        this.params1 = param1;
        this.params2 = param2;
    }

    public String paramsInfo() {
        return this.params1 + "!  " + params2;
    }

    public boolean isValidLength(String param) {

        int length = param.length();
        Integer minLength = demoProperties.getMinLength();

        if (length < minLength.intValue()) {
            return false;
        } else {
            return true;
        }

    }
}

5. 定義配置類

package com.demo;

import com.demo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 描述:配置類
 *
 **/
@Configuration
@EnableConfigurationProperties(DemoProperties.class)
@ConditionalOnProperty(
        prefix = "demo",
        name = "enable",
        havingValue = "true"
)
public class DemoStarterAutoConfig {
    @Autowired
    private DemoProperties demoProperties;

    @Bean(name = "demo")
    public DemoService demoService(){
        return new DemoService(demoProperties.getParams1(), demoProperties.getParams2());
    }
}

6. 重要的一步

resource目錄下添加META-INF目錄,在其下面建立文件spring.factories,內(nèi)容如下:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.demo.DemoStarterAutoConfig

經(jīng)過以上步驟,starter就完成了。

以下是建立測試項目的步驟。

7. 建立maven module測試項目

<?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">
    <parent>
        <artifactId>springboot-starter-demo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springboot-starter-application</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

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

        <dependency>
            <groupId>org.example</groupId>
            <artifactId>springboot-starter-config</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

8. 添加配置文件application.yml

server:
  port: 8010


demo:
  enable: true
  params1: 參數(shù)1
  params2: 參數(shù)2
  minLength: 4

9. 添加測試controller

package com.demo.starter.controller;

import com.demo.service.DemoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
public class AppController {

    @Resource(name = "demo")
    private DemoService demoService;

    @GetMapping("/test")
    public String test(){

        boolean valid = demoService.isValidLength("test");
        if(valid)
            return demoService.paramsInfo();
        else
            return "無效數(shù)據(jù)";
    }
}

10. 打開瀏覽器,執(zhí)行測試

自此完成了starter的基本測試。

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Java使用fastjson對String、JSONObject、JSONArray相互轉(zhuǎn)換

    Java使用fastjson對String、JSONObject、JSONArray相互轉(zhuǎn)換

    這篇文章主要介紹了Java使用fastjson對String、JSONObject、JSONArray相互轉(zhuǎn)換,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • springboot如何重定向攜帶數(shù)據(jù) RedirectAttributes

    springboot如何重定向攜帶數(shù)據(jù) RedirectAttributes

    這篇文章主要介紹了springboot如何重定向攜帶數(shù)據(jù) RedirectAttributes,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • JAVA中的deflate壓縮實現(xiàn)方法

    JAVA中的deflate壓縮實現(xiàn)方法

    下面小編就為大家?guī)硪黄狫AVA中的deflate壓縮實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-09-09
  • Spring Bean常用依賴注入方式詳解

    Spring Bean常用依賴注入方式詳解

    這篇文章主要介紹了Spring Bean常用三種依賴注入方式詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • SpringBoot整合SpringDataJPA的示例

    SpringBoot整合SpringDataJPA的示例

    本文主要介紹了SpringBoot整合SpringDataJPA的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-06-06
  • IntelliJ IDEA 2020.3.3現(xiàn)已發(fā)布!新增“受信任項目”功能

    IntelliJ IDEA 2020.3.3現(xiàn)已發(fā)布!新增“受信任項目”功能

    這篇文章主要介紹了IntelliJ IDEA 2020.3.3現(xiàn)已發(fā)布!新增“受信任項目”功能,本文給大家分享了idea2020.3.3激活碼的詳細破解教程,每種方法都很好用,使用idea2020.3以下所有版本,需要的朋友可以參考下
    2021-03-03
  • Mybatis-Plus開發(fā)提速器mybatis-plus-generator-ui詳解

    Mybatis-Plus開發(fā)提速器mybatis-plus-generator-ui詳解

    這篇文章主要介紹了Mybatis-Plus開發(fā)提速器mybatis-plus-generator-ui,本文簡要介紹一款基于Mybatis-Plus的代碼自助生成器,文章通過實例集成的方式來詳細講解mybatis-plus-generator-ui,從相關概念到實際集成案例,以及具體的擴展開發(fā)介紹,需要的朋友可以參考下
    2022-11-11
  • Spring中事件發(fā)布機制及流程詳解

    Spring中事件發(fā)布機制及流程詳解

    這篇文章主要介紹了Spring中事件發(fā)布機制及流程詳解,在分析源碼的過程中,也是大量使用了事件機制,在我分析的這篇博客中,有不少地方都運用了事件發(fā)布機制,所以本文的目的是從SpringBoot中學習到事件的發(fā)布流程,需要的朋友可以參考下
    2023-11-11
  • Java中的ArrayList底層源碼分析

    Java中的ArrayList底層源碼分析

    這篇文章主要介紹了Java中的ArrayList底層源碼分析,通過下標讀取元素的速度很快,這是因為ArrayList底層基于數(shù)組實現(xiàn),可以根據(jù)下標快速的找到內(nèi)存地址,接著讀取內(nèi)存地址中存放的數(shù)據(jù),需要的朋友可以參考下
    2023-12-12
  • Java中對象序列化與反序列化詳解

    Java中對象序列化與反序列化詳解

    這篇文章主要介紹了Java中對象序列化與反序列化,較為詳細的分析了java中對象序列化的概念、原理、實現(xiàn)方法及相關注意事項,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09

最新評論

揭西县| 防城港市| 承德市| 余干县| 双峰县| 阿拉善左旗| 新竹县| 文成县| 兖州市| 夏河县| 土默特左旗| 滨海县| 广灵县| 徐州市| 全南县| 环江| 宣城市| 许昌市| 金阳县| 广元市| 镇平县| 白山市| 丘北县| 华坪县| 永安市| 伊宁县| 吉安县| 左贡县| 得荣县| 大同县| 青岛市| 祁连县| 铅山县| 囊谦县| 浙江省| 兴安县| 沂水县| 维西| 石棉县| 海淀区| 眉山市|