日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot配置文件有提示

發布時間:2024/4/13 javascript 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot配置文件有提示 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

屬性說明

groups?

properties

hints?

原理

注解解釋器

需要的注解

注解使用

增加文件

參考


在研究spring boot源碼時,會在META-INF目錄下發現spring-configuration-metadata.json的文件,這個文件用于在配置文件中設置屬性值時具有提示功能。這個功能適用于spring boot 2.0以上。

屬性說明

groups?

"groups"是高級別的節點,它們本身不指定一個值,但為properties提供一個有上下文關聯的分組。例如,server.port和server.servlet-path屬性是server組的一部分,@ConfigurationProperties注解指定的屬性前綴,會自動提示屬性名稱前綴。

:不需要每個"property"都有一個"group",一些屬性可以以自己的形式存在。

?

  • name group的全名,該屬性必須
  • type group數據類型的類名。例如,如果group是基于一個被@ConfigurationProperties注解的類,該屬性將包含該類的全限定名。如果基于一個@Bean方法,它將是該方法的返回類型。如果該類型未知,則該屬性將被忽略
  • description 一個簡短的group描述,用于展示給用戶,要.點結尾。如果沒有可用描述,該屬性將被忽略
  • sourceType 來源類名。例如,如果組基于一個被@ConfigurationProperties注解的@Bean方法,該屬性將包含@Configuration類的全限定名,該類包含此方法。如果來源類型未知,則該屬性將被忽略
  • sourceMethod 該組的方法的全名(包含括號及參數類型)。例如,被@ConfigurationProperties注解的@Bean方法名。如果源方法未知,該屬性將被忽略
"groups": [{"name": "server","type": "org.springframework.boot.autoconfigure.web.ServerProperties","sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"},{"name": "spring.jpa.hibernate","type": "org.springframework.boot.autoconfigure.orm.jpa.JpaProperties$Hibernate","sourceType": "org.springframework.boot.autoconfigure.orm.jpa.JpaProperties","sourceMethod": "getHibernate()"}... ]

properties

輸入屬性名稱時的提示,即在配置文件中輸入屬性值,會提示。

  • name 屬性全名,格式為小寫虛線分割的形式(jiuxian.name-en).必須要有的
  • type 屬性數據類型,java.lang.Boolean。類型未知可忽略
  • description 該屬性的描述
  • sourceType 來源類型,例如,如果property來自一個被@ConfigurationProperties注解的類,該屬性將包括該類的全限定名。如果來源類型未知則該屬性會被忽略
  • defaultValue 定義輸入時的默認值,只是提示,并不是真正的默認值,可忽略
  • deprecated 是否廢棄 boolean 值
    • level 級別 error,warning。waring表示屬性可用,可不用。error,表示屬性對預期的是無效的。
    • reason 廢棄原因
    • replacement 替代屬性,為properties 全名
"properties": [{"name": "server.port","type": "java.lang.Integer","sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"},{"name": "spring.jpa.hibernate.ddl-auto","type": "java.lang.String","description": "DDL mode. This is actually a shortcut for the \"hibernate.hbm2ddl.auto\" property.","sourceType": "org.springframework.boot.autoconfigure.orm.jpa.JpaProperties$Hibernate"}... ]

hints?

hints描述某個屬性的屬性值的可選提示。在輸入冒號(:)后的提示

  • name 屬性全名,不能為空
  • values 可選的值
"hints": [{"name": "spring.jpa.hibernate.ddl-auto","values": [{"value": "none","description": "Disable DDL handling."},{"value": "validate","description": "Validate the schema, make no changes to the database."}]} ]

原理

注解解釋器

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

需要的注解

  • @ConfigurationProperties 配置屬性文件,需要指定前綴 prefix
  • @EnableConfigurationProperties 啟用配置,需要指定啟用的配置類
  • @NestedConfigurationProperty 當一個類中引用了外部類,需要在該屬性上加該注解

注解使用

@ConfigurationProperties(prefix="server") public class ServerProperties {private String name;private Host host;// ... getter and setterspublic static class Host {private String ip;private int port;// ... getter and setters}}

增加文件

META-INF/additional-spring-configuration-metadata.json

文件中定義groups,attributes,hints

?

參考

https://docs.spring.io/spring-boot/docs/2.1.7.RELEASE/reference/html/configuration-metadata.html#configuration-metadata-format

?

?

總結

以上是生活随笔為你收集整理的Spring Boot配置文件有提示的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。