国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

如何生成 Spring 屬性文檔

您是否正在努力使 Spring 配置屬性的文檔與代碼保持一致?了解如何解決本文中的問(wèn)題。

介紹

幾乎每個(gè) Spring(啟動(dòng))應(yīng)用程序都會(huì)使用配置屬性。這些配置屬性確保應(yīng)用程序中的某些項(xiàng)目可以通過(guò) application.properties(或 YAML)文件進(jìn)行配置。然而,還需要記錄這些屬性,以便人們了解這些屬性的含義、如何使用它們等。這通常記錄在自述文件中。當(dāng)屬性存在于還包含文檔和注釋的 Java 類(lèi)中時(shí),需要手動(dòng)維護(hù)此 README 文件。

如果文檔位于一個(gè)位置(Java 類(lèi),靠近代碼)并且可以從代碼中生成文檔,這不是很好嗎?好消息!這正是Spring Configuration Property Documenter Maven 插件(https://github.com/rodnansol/spring-configuration-property-documenter)將為您做的事情!在本文的其余部分中,您將探索此 Maven 插件的一些功能以及如何輕松地將其合并到您的項(xiàng)目中。官方文檔更詳細(xì),可以在這里找到(https://github.com/rodnansol/spring-configuration-property-documenter/blob/master/docs/modules/ROOT/pages/index.adoc)。

這篇博文中使用的資源可以在GitHub上找到。(https://github.com/mydeveloperplanet/myspringconfigdocplanet)

申請(qǐng)樣本

首先,您需要?jiǎng)?chuàng)建一個(gè)基本的示例應(yīng)用程序。導(dǎo)航到Spring Initializr(https://start.spring.io/)并選擇依賴項(xiàng)Spring Web、Lombok和Spring Configuration Processor。

使用 .注釋主 Spring Boot 應(yīng)用程序類(lèi)@ConfigurationPropertiesScan。

@SpringBootApplication
@ConfigurationPropertiesScan("com.mydeveloperplanet.myspringconfigdocplanet.config")
public class MySpringConfigDocPlanetApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(MySpringConfigDocPlanetApplication.class, args);
    }

}

MyFirstProperties在包中創(chuàng)建一個(gè)配置類(lèi)config。配置類(lèi)使用構(gòu)造函數(shù)綁定。另請(qǐng)參閱之前的文章“ Spring Boot 配置屬性解釋”,了解有關(guān)創(chuàng)建配置屬性的不同方法的更多信息。

@Getter
@ConfigurationProperties("my.first.properties")
public class MyFirstProperties {
 
    private final String stringProperty;
 
    private final boolean booleanProperty;
 
    public MyFirstProperties(String stringProperty, boolean booleanProperty) {
        this.stringProperty = stringProperty;
        this.booleanProperty = booleanProperty;
    }
}

此外,還將 a添加到返回屬性的ConfigurationController包中。controller該控制器僅作為如何使用屬性的示例添加。它與本博客沒(méi)有任何關(guān)系。

構(gòu)建應(yīng)用程序。

$ mvn clean verify

運(yùn)行應(yīng)用程序。

$ mvn spring-boot:run


文章來(lái)源地址http://www.zghlxwxcb.cn/article/405.html

調(diào)用 中配置的端點(diǎn)ConfigurationController。

$ curl http://localhost:8080/configuration


看一下目錄target/classes/META-INFspring-configuration-metadata.json這里有一個(gè)文件,其中包含有關(guān)配置類(lèi)的元數(shù)據(jù)。Spring Configuration Property Documenter Maven 插件使用此信息來(lái)生成文檔。

生成此元數(shù)據(jù)文件是因?yàn)槟砑恿薙pring 配置處理器作為依賴項(xiàng)。

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

生成文檔

該插件能夠生成四種不同格式的文檔:

  • ASCII 醫(yī)生

  • 降價(jià)

  • 超文本標(biāo)記語(yǔ)言

  • XML

為了生成文檔,您只需將插件添加到構(gòu)建部分(在添加 Spring 配置處理器依賴項(xiàng)旁邊)。對(duì)于每種格式類(lèi)型,都會(huì)添加一個(gè)執(zhí)行。如果您只需要 markdown 格式的文檔,只需刪除其他執(zhí)行即可。

<build>
  <plugins>
    <plugin>
      <groupId>org.rodnansol</groupId>
      <artifactId>spring-configuration-property-documenter-maven-plugin</artifactId>
      <version>0.6.1</version>
      <executions>
        <execution>
          <id>generate-adoc</id>
          <phase>process-classes</phase>
          <goals>
            <goal>generate-property-document</goal>
          </goals>
          <configuration>
            <type>ADOC</type>
          </configuration>
        </execution>
        <execution>
          <id>generate-markdown</id>
          <phase>process-classes</phase>
          <goals>
            <goal>generate-property-document</goal>
          </goals>
          <configuration>
            <type>MARKDOWN</type>
          </configuration>
        </execution>
        <execution>
          <id>generate-html</id>
          <phase>process-classes</phase>
          <goals>
            <goal>generate-property-document</goal>
          </goals>
          <configuration>
            <type>HTML</type>
          </configuration>
        </execution>
        <execution>
          <id>generate-xml</id>
          <phase>process-classes</phase>
          <goals>
            <goal>generate-property-document</goal>
          </goals>
          <configuration>
            <type>XML</type>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

使用 Maven 執(zhí)行構(gòu)建時(shí)會(huì)生成文檔,但快速的方法是執(zhí)行目標(biāo)process-classes。

$ mvn process-classes

或者您可以調(diào)用特定的執(zhí)行。

$ mvn spring-configuration-property-documenter:generate-property-document@generate-markdown


看一下目錄target/property-docs。對(duì)于每種類(lèi)型,都會(huì)添加配置屬性的文檔。

ASCII Doctor 格式

ASCII Doctor 格式

Markdown 格式

Markdown 格式

HTML 格式

HTML 格式

XML 格式顯示起來(lái)有點(diǎn)復(fù)雜,但它包含上述內(nèi)容的 XML 表示形式。

如果您有一個(gè) Maven 多模塊項(xiàng)目,您可以將不同模塊的所有屬性合并到一個(gè)文件中。文檔中描述了如何執(zhí)行此操作。(https://github.com/rodnansol/spring-configuration-property-documenter/blob/master/docs/modules/ROOT/pages/maven-plugin.adoc)

自定義輸出

在本文的其余部分中,您將繼續(xù)使用 Markdown 格式。在上面的屏幕截圖中,您注意到添加了一個(gè)未知組。這個(gè)群也是空的。默認(rèn)情況下,該組始終添加到輸出中,但可以通過(guò)參數(shù)將其刪除markdownCustomization。文檔(https://github.com/rodnansol/spring-configuration-property-documenter/blob/master/docs/modules/ROOT/pages/template-customization.adoc#template-customizations)中列出了更多可用的自定義項(xiàng)。為了禁用輸出中的未知組,請(qǐng)將參數(shù)設(shè)置includedUnknownGroup為false。

<execution>
  <id>generate-markdown</id>
  <phase>process-classes</phase>
  <goals>
    <goal>generate-property-document</goal>
  </goals>
  <configuration>
    <type>MARKDOWN</type>
      <markdownCustomization>
        <includeUnknownGroup>false</includeUnknownGroup>
      </markdownCustomization>
  </configuration>
</execution>


執(zhí)行降價(jià)生成,您會(huì)注意到輸出中不再存在未知組。

描述和默認(rèn)值

在上面的輸出中,您注意到屬性的描述和默認(rèn)值stringProperty是空的。

創(chuàng)建一個(gè)新的配置類(lèi)MySecondProperties。在表示屬性的字段上方添加 Javadoc,并在構(gòu)造函數(shù)@DefaultValue之前添加注釋。stringProperty

@Getter
@ConfigurationProperties("my.second.properties")
public class MySecondProperties {
 
    /**
     * This is the description for stringProperty
     */
    private final String stringProperty;
 
    /**
     * This is the description for booleanProperty
     */
    private final boolean booleanProperty;
 
    public MySecondProperties(@DefaultValue("default value for stringProperty") String stringProperty,
                              boolean booleanProperty) {
        this.stringProperty = stringProperty;
        this.booleanProperty = booleanProperty;
    }
}


生成文檔,您會(huì)注意到存在描述并且填充了默認(rèn)值stringProperty。

生成文檔,您會(huì)注意到存在描述,并且為 stringProperty 填充了默認(rèn)值。

這太棒了,不是嗎?文檔與代碼一起就在那里,并且 Markdown 中的文檔是從它生成的。

嵌套屬性

它也適用于嵌套屬性嗎?讓我們來(lái)看看吧。創(chuàng)建一個(gè)MyThirdProperties具有嵌套屬性的配置類(lèi)nestedProperty,其中還包含 astringProperty和 a booleanProperty。默認(rèn)booleanProperty為 true。

@Getter
@ConfigurationProperties("my.third.properties")
public class MyThirdProperties {
 
    /**
     * This is the description for stringProperty
     */
    private final String stringProperty;
 
    /**
     * This is the description for booleanProperty
     */
    private final boolean booleanProperty;
 
    private final NestedProperty nestedProperty;
 
    public MyThirdProperties(@DefaultValue("default value for stringProperty") String stringProperty,
                             boolean booleanProperty,
                             @DefaultValue NestedProperty nestedProperty) {
        this.stringProperty = stringProperty;
        this.booleanProperty = booleanProperty;
        this.nestedProperty = nestedProperty;
    }
 
    @Getter
    public static class NestedProperty {
 
        /**
         * This is the description for nested stringProperty
         */
        private final String stringProperty;
 
        /**
         * This is the description for nested booleanProperty
         */
        private final boolean booleanProperty;
 
        public NestedProperty(@DefaultValue("default value for nested stringProperty") String stringProperty,
                              @DefaultValue("true") boolean booleanProperty) {
            this.stringProperty = stringProperty;
            this.booleanProperty = booleanProperty;
        }
    }


生成 Markdown 文檔,您會(huì)注意到該文檔還包含嵌套屬性。

生成 Markdown 文檔,您會(huì)注意到該文檔還包含嵌套屬性。

記錄

由于配置屬性是不可變的,因此使用 Java 記錄比使用 Lombok 更好。創(chuàng)建配置類(lèi)MyFourthProperties并使用Java記錄。問(wèn)題是在哪里添加屬性描述,因?yàn)闆](méi)有可以添加 Javadoc 的字段。

/**
 * @param stringProperty This is the description for stringProperty
 * @param booleanProperty This is the description for booleanProperty
 */
@ConfigurationProperties("my.fourth.properties")
public record MyFourthProperties (@DefaultValue("default value for stringProperty") String stringProperty,
                                  @DefaultValue("true") boolean booleanProperty) {
}


生成 Markdown 文檔并注意描述為空。

生成 Markdown 文檔并注意描述為空。

然而,這不是插件的問(wèn)題。文件中的描述為空spring-configuration-metadata.json,插件僅使用此信息。

Stack Overflow 上有人問(wèn)了這個(gè)問(wèn)題。希望接下來(lái)會(huì)有答案。(https://stackoverflow.com/questions/76935707/how-to-use-the-configuration-processor-with-records)

總結(jié)

Spring Configuration Property Documenter Maven 插件是一項(xiàng)偉大的舉措,旨在使文檔更接近代碼并根據(jù)代碼生成文檔。它填補(bǔ)了我認(rèn)為的一個(gè)空白,這使幾乎所有 Spring 項(xiàng)目受益。


到此這篇關(guān)于如何生成 Spring 屬性文檔的文章就介紹到這了,更多相關(guān)內(nèi)容可以在右上角搜索或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

原文地址:http://www.zghlxwxcb.cn/article/405.html

如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)聯(lián)系站長(zhǎng)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【導(dǎo)出Word】如何使用Java+Freemarker模板引擎,根據(jù)XML模板文件生成Word文檔(只含文本內(nèi)容的模板)

    【導(dǎo)出Word】如何使用Java+Freemarker模板引擎,根據(jù)XML模板文件生成Word文檔(只含文本內(nèi)容的模板)

    這篇文章,主要介紹如何使用Java+Freemarker模板引擎,根據(jù)XML模板文件生成Word文檔。 目錄 一、導(dǎo)出Word文檔 1.1、基礎(chǔ)知識(shí) 1.2、制作模板文件 1.3、代碼實(shí)現(xiàn) (1)引入依賴 (2)創(chuàng)建Freemarker工具類(lèi) (3)測(cè)試案例代碼 (4)運(yùn)行效果 Word文件有兩種后綴格式,分別是:doc和docx,

    2024年02月13日
    瀏覽(29)
  • 【教程】如何使用Java生成PDF文檔?

    在如今數(shù)字化時(shí)代,越來(lái)越多的人使用PDF文檔進(jìn)行信息傳遞和共享。而使用Java生成PDF文檔也成為了一個(gè)非常重要的技能,因?yàn)镴ava作為一種通用的編程語(yǔ)言,可以在不同的操作系統(tǒng)和平臺(tái)上運(yùn)行。下面,我們將為您介紹如何使用Java生成PDF文檔。 PDF文檔的生成通常包括兩個(gè)步驟

    2024年02月02日
    瀏覽(21)
  • Spring AOP官方文檔學(xué)習(xí)筆記(三)之基于xml的Spring AOP

    1.聲明schema,導(dǎo)入命名空間 (1)如果我們想要使用基于xml的spring aop,那么,第一步,我們需要在xml配置文件中聲明spring aop schema,導(dǎo)入命名空間,如下這是一個(gè)標(biāo)準(zhǔn)的模板 (2)在xml配置文件中,所有的切面以及通知等都必須放置于aop:config標(biāo)簽內(nèi) 2.聲明一個(gè)切面 3.聲明一個(gè)切

    2024年02月02日
    瀏覽(24)
  • HarmonyOS鴻蒙基于Java開(kāi)發(fā):Java UI 常用組件 組件通用XML屬性

    目錄 基礎(chǔ)XML屬性 間距相關(guān)的XML屬性 滾動(dòng)條相關(guān)的XML屬性 旋轉(zhuǎn)縮放相關(guān)的XML屬性 焦點(diǎn)相關(guān)的XML屬性 Component是所有組件的基類(lèi),Component支持的XML屬性,其他組件都支持。 Component支持的XML屬性如下表。 表1? 基礎(chǔ)屬性的相關(guān)說(shuō)明 屬性名稱

    2024年01月25日
    瀏覽(25)
  • 【Java】Maven配置文件幫助文檔(settings.xml 和 pom.xml)

    以下幾個(gè)屬性是 settings 屬性的下一級(jí)屬性: localRepository interactiveMode offline pluginGroups proxies servers mirrors profiles activeProfiles localRepository:本地倉(cāng)庫(kù)的路徑,默認(rèn)值為 ${user.home}/.m2/repository interactiveMode:表示Maven是否需要和用戶交互以獲得輸入 offline:表示Maven是否需要在離線模式

    2024年02月13日
    瀏覽(83)
  • JAVA生成xml文件

    自動(dòng)生成xml文件,使用到的jar包為dom4j 三、結(jié)果展示

    2024年02月13日
    瀏覽(25)
  • Spring Boot整合Spring Fox生成Swagger文檔

    Spring Boot整合Spring Fox生成Swagger文檔

    Springfox是一個(gè)用于在Spring應(yīng)用程序中生成Swagger文檔的開(kāi)源庫(kù)。它提供了一組注解和工具,可以將你的API代碼和文檔整合在一起,方便生成和展示API的Swagger文檔。 使用Springfox,你可以在Spring Boot項(xiàng)目中集成Swagger,并通過(guò)Swagger UI查看和測(cè)試API。它提供了一些注解,如 @Api 、 @

    2024年02月08日
    瀏覽(17)
  • Spring Boot集成JasperReport生成文檔

    Spring Boot集成JasperReport生成文檔

    由于工作需要,要實(shí)現(xiàn)后端根據(jù)模板動(dòng)態(tài)填充數(shù)據(jù)生成PDF文檔,通過(guò)技術(shù)選型,使用Ireport5.6來(lái)設(shè)計(jì)模板,結(jié)合JasperReports5.6工具庫(kù)來(lái)調(diào)用渲染生成PDF文檔。 一、使用Ireport designer 5.6設(shè)計(jì)模板 ireport的使用由于時(shí)間關(guān)系不便多說(shuō),設(shè)計(jì)好之后,將其進(jìn)行編譯生成jasper文件,然后將

    2024年02月09日
    瀏覽(18)
  • 如何利用代碼快速生成mapper.xml的<resultMap>

    如何利用代碼快速生成mapper.xml的<resultMap>

    一,問(wèn)題引入 當(dāng)我們開(kāi)發(fā) mapper.xml ----dao接層 ----service接口----serviceImp ----controller層, 其中在mapper.xml編寫(xiě)查詢語(yǔ)句的sql時(shí)會(huì)遇到sql查詢到的結(jié)果 涉及到多張表的字段,或者單張表的字段過(guò)多時(shí), 這時(shí)候我們就需寫(xiě)一個(gè) resultMap來(lái)封裝一下這段sql的返回結(jié)果,這個(gè) resultMap標(biāo)簽長(zhǎng)

    2023年04月25日
    瀏覽(20)
  • 編碼技巧:如何在Golang中高效解析和生成XML

    編碼技巧:如何在Golang中高效解析和生成XML

    在當(dāng)今數(shù)據(jù)驅(qū)動(dòng)的編程世界中,有效地處理各種數(shù)據(jù)格式是每個(gè)開(kāi)發(fā)人員必備的技能之一。其中,XML(可擴(kuò)展標(biāo)記語(yǔ)言)作為一種廣泛使用的標(biāo)記語(yǔ)言,它的靈活性和可擴(kuò)展性使其在配置文件、網(wǎng)絡(luò)服務(wù)以及跨平臺(tái)數(shù)據(jù)交換中占據(jù)重要地位。然而,對(duì)于剛接觸Golang的開(kāi)發(fā)者來(lái)

    2024年01月16日
    瀏覽(17)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包