元数据格式
配置元数据文件位于 jars 中的 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
“groups” 是更高级别的项,它们本身不指定值,而是为属性提供上下文分组。
例如,server.port
和 server.address
属性是 server
组的一部分。
注意:并不是每个 “property” 都必须有一个 “group”。 某些属性可能独立存在。
最后,“hints” 是用于帮助用户配置给定属性的附加信息。
例如,当开发者配置 spring.jpa.hibernate.ddl-auto
属性时,工具可以使用提示来为 none
、validate
、update
、create
和 create-drop
值提供一些自动完成帮助。
Group Attributes
groups
数组中包含的 JSON 对象可以包含下表中显示的属性:
Name | Type | Purpose |
---|---|---|
|
String |
组的完整名称。 此属性是必需的。 |
|
String |
组的数据类型的类名。
例如,如果组基于用 |
|
String |
可以向用户显示的组的简短描述。
如果没有可用的描述,可以省略。
建议描述为简短的段落,第一行提供简洁的摘要。
描述的最后一行应以句点( |
|
String |
贡献此组的源的类名。
例如,如果组基于用 |
|
String |
贡献此组的方法的完整名称(包括括号和参数类型)(例如,用 |
Property Attributes
properties
数组中包含的 JSON 对象可以包含下表中描述的属性:
Name | Type | Purpose |
---|---|---|
|
String |
属性的完整名称。
名称采用小写点分隔形式(例如, |
|
String |
属性的数据类型的完整签名(例如, |
|
String |
可以向用户显示的属性的简短描述。
如果没有可用的描述,可以省略。
建议描述为简短的段落,第一行提供简洁的摘要。
描述的最后一行应以句点( |
|
String |
贡献此属性的源的类名。
例如,如果属性来自用 |
|
Object |
如果未指定属性,则使用的默认值。 如果属性的类型是数组,它可以是值的数组。 如果默认值未知,可以省略。 |
|
Deprecation |
指定属性是否已弃用。
如果字段未弃用或该信息未知,可以省略。
下一个表提供了有关 |
每个 properties
元素的 deprecation
属性中包含的 JSON 对象可以包含以下属性:
Name | Type | Purpose |
---|---|---|
|
String |
弃用级别,可以是 |
|
String |
说明属性被弃用的原因的简短描述。
如果没有可用的原因,可以省略。
建议描述为简短的段落,第一行提供简洁的摘要。
描述的最后一行应以句点( |
|
String |
替换此弃用属性的属性的完整名称。 如果此属性没有替换,可以省略。 |
|
String |
属性被弃用的版本。 可以省略。 |
注意:在 Spring Boot 1.3 之前,可以使用单个 deprecated
布尔属性代替 deprecation
元素。
这仍然以弃用的方式支持,不应再使用。
如果没有原因和替换可用,应设置一个空的 deprecation
对象。
弃用也可以在代码中通过将 @DeprecatedConfigurationProperty
注解添加到暴露弃用属性的 getter 来声明性地指定。
例如,假设 my.app.target
属性令人困惑,并已重命名为 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
,因为代码仍在处理该属性。
前面的代码确保弃用的属性仍然有效(在幕后委托给 name
属性)。
一旦可以从公共 API 中删除 getTarget
和 setTarget
方法,元数据中的自动弃用提示也会消失。
如果你想保留提示,添加带有 error
弃用级别的手动元数据可确保用户仍然了解该属性。
当提供 replacement
时,这特别有用。
Hint Attributes
hints
数组中包含的 JSON 对象可以包含下表中显示的属性:
Name | Type | Purpose |
---|---|---|
|
String |
此提示引用的属性的完整名称。
名称采用小写点分隔形式(如 |
|
ValueHint[] |
由 |
|
ValueProvider[] |
由 |
每个 hint
元素的 values
属性中包含的 JSON 对象可以包含下表中描述的属性:
Name | Type | Purpose |
---|---|---|
|
Object |
提示引用的元素的有效值。 如果属性的类型是数组,它也可以是值的数组。 此属性是必需的。 |
|
String |
可以向用户显示的值的简短描述。
如果没有可用的描述,可以省略。
建议描述为简短的段落,第一行提供简洁的摘要。
描述的最后一行应以句点( |
每个 hint
元素的 providers
属性中包含的 JSON 对象可以包含下表中描述的属性:
Name | Type | Purpose |
---|---|---|
|
String |
用于为提示引用的元素提供额外内容帮助的提供者的名称。 |
|
JSON object |
提供者支持的任何额外参数(有关更多详细信息,请查看提供者的文档)。 |