对于最新的稳定版本,请使用 Spring Boot 3.4.0! |
元数据格式
配置元数据文件位于 jar 内的META-INF/spring-configuration-metadata.json
.
它们使用 JSON 格式,其中项分类在“groups”或“properties”下,其他值提示分类在“hints”下,如以下示例所示:
{"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": "server.port",
"type": "java.lang.Integer",
"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"
},
{
"name": "server.address",
"type": "java.net.InetAddress",
"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": [
{
"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."
},
{
"value": "update",
"description": "Update the schema if necessary."
},
{
"value": "create",
"description": "Create the schema and destroy previous data."
},
{
"value": "create-drop",
"description": "Create and then destroy the schema at the end of the session."
}
]
}
]}
每个 “property” 都是用户使用给定值指定的配置项。
例如server.port
和server.address
可以在application.properties
/application.yaml
如下:
-
Properties
-
YAML
server.port=9090
server.address=127.0.0.1
server:
port: 9090
address: 127.0.0.1
“组”是更高级别的项目,它们本身不指定值,而是为属性提供上下文分组。
例如,server.port
和server.address
properties 是server
群。
不要求每个 “property” 都有一个 “group”。 某些属性可能本身就存在。 |
最后,“提示”是用于帮助用户配置给定属性的附加信息。
例如,当开发人员配置spring.jpa.hibernate.ddl-auto
属性中,工具可以使用提示为none
,validate
,update
,create
和create-drop
值。
组属性
包含在groups
数组可以包含下表中显示的属性:
名字 | 类型 | 目的 |
---|---|---|
|
字符串 |
组的全名。 此属性是必需的。 |
|
字符串 |
组数据类型的类名。
例如,如果组基于一个带有 |
|
字符串 |
可向用户显示的组的简短描述。
如果没有可用的描述,则可以省略它。
建议描述为简短的段落,第一行提供简明的摘要。
描述中的最后一行应以句点 ( |
|
字符串 |
提供此组的源的类名。
例如,如果组基于 |
|
字符串 |
提供此组的方法的全名(包括括号和参数类型)(例如,一个 |
属性属性
包含在properties
数组可以包含下表中描述的属性:
名字 | 类型 | 目的 |
---|---|---|
|
字符串 |
属性的全名。
名称采用小写句点分隔形式(例如 |
|
字符串 |
属性数据类型的完整签名(例如 |
|
字符串 |
可向用户显示的属性的简短说明。
如果没有可用的描述,则可以省略它。
建议描述为简短的段落,第一行提供简明的摘要。
描述中的最后一行应以句点 ( |
|
字符串 |
提供此属性的源的类名。
例如,如果属性来自一个带有 |
|
对象 |
默认值,如果未指定属性,则使用该值。 如果属性的类型是数组,它可以是值的数组。 如果默认值未知,则可以省略它。 |
|
折旧 |
指定是否弃用该属性。
如果该字段未弃用或该信息未知,则可以省略该字段。
下表提供了有关 |
包含在deprecation
每个properties
元素可以包含以下属性:
名字 | 类型 | 目的 |
---|---|---|
|
字符串 |
弃用级别,可以是 |
|
字符串 |
弃用属性的原因的简短描述。
如果没有可用的原因,则可以省略。
建议描述为简短的段落,第一行提供简明的摘要。
描述中的最后一行应以句点 ( |
|
字符串 |
替换此已弃用属性的属性的全名。 如果此属性没有替代项,则可以省略它。 |
|
字符串 |
属性已弃用的版本。 可以省略。 |
在 Spring Boot 1.3 之前,单个deprecated boolean 属性代替deprecation 元素。
这仍然以已弃用的方式受支持,不应再使用。
如果没有可用的原因和替换项,则为空的deprecation object 的 |
弃用也可以在代码中以声明方式指定,方法是将@DeprecatedConfigurationProperty
注解传递给 getter 来公开已弃用的属性。
例如,假设my.app.target
property 令人困惑,并被重命名为my.app.name
.
以下示例显示如何处理这种情况:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
@ConfigurationProperties("my.app")
public class MyProperties {
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Deprecated
@DeprecatedConfigurationProperty(replacement = "my.app.name")
public String getTarget() {
return this.name;
}
@Deprecated
public void setTarget(String target) {
this.name = target;
}
}
无法设置level .warning 始终假定,因为代码仍在处理该属性。 |
前面的代码确保 deprecated 属性仍然有效(委托给name
幕后属性)。
一旦getTarget
和setTarget
方法可以从你的公共 API 中删除,元数据中的自动弃用提示也会消失。
如果您想保留提示,请使用error
deprecation 级别可确保用户仍会收到有关该属性的信息。
当replacement
。
Hint 属性
包含在hints
数组可以包含下表中显示的属性:
名字 | 类型 | 目的 |
---|---|---|
|
字符串 |
此提示所引用的属性的全名。
名称采用小写句点分隔形式(例如 |
|
ValueHint[] |
由 |
|
ValueProvider[] |
由 |
包含在values
每个hint
元素可以包含下表中描述的属性:
名字 | 类型 | 目的 |
---|---|---|
|
对象 |
提示引用的元素的有效值。 如果属性的类型是数组,则它也可以是值的数组。 此属性是必需的。 |
|
字符串 |
可向用户显示的值的简短描述。
如果没有可用的描述,则可以省略它。
建议描述为简短的段落,第一行提供简明的摘要。
描述中的最后一行应以句点 ( |
包含在providers
每个hint
元素可以包含下表中描述的属性:
名字 | 类型 | 目的 |
---|---|---|
|
字符串 |
用于为提示引用的元素提供其他内容帮助的提供程序的名称。 |
|
JSON 对象 |
提供程序支持的任何其他参数(有关更多详细信息,请查看提供程序的文档)。 |
重复的元数据项
具有相同“属性”和“组”名称的对象可以在元数据文件中多次出现。 例如,您可以将两个单独的类绑定到同一个前缀,每个类都有可能重叠的属性名称。 虽然元数据中多次出现的相同名称应该不常见,但元数据的使用者应注意确保他们支持它。