springboot 配置文件提示
**********************
配置元数据
The majority of the metadata file is generated automatically at compile time
by processing all items annotated with @ConfigurationProperties
# metadata文件的大部分内容是在编译的时候自动生成的
Spring Boot’s configuration file handling is quite flexible, and it is often the
case that properties may exist that are not bound to a @ConfigurationProperties bean.
# springboot 配置文件很灵活,有时候配置属性没有和@ConfigurationProperties标注的bean绑定
You may also need to tune some attributes of an existing key.
# 有时候也需要对现有的属性进行一些调整
To support such cases and let you provide custom "hints", the annotation processor
automatically merges items from META-INF/additional-spring-configuration-metadata.json
into the main metadata file
# 可以使用META-INF/additional-spring-configuration-metadata.json自定义属性,编译的时候会自动合并
The format of the additional-spring-configuration-metadata.json file is exactly the same
as the regular spring-configuration-metadata.json. The additional properties file is
optional. If you do not have any additional properties, do not add the file
# additional-spring-configuration-metadata.json、spring-configuration-metadata.json文件格式一致
# additional-spring-configuration-metadata.json非必需,如果没有额外的属性,则不需要添加该文件
spring-configuration-metadata.json(部分)
{
"groups": [
{
"name": "server",
"type": "org.springframework.boot.autoconfigure.web.ServerProperties",
"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"
},
{
"name": "server.compression",
"type": "org.springframework.boot.web.server.Compression",
"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties",
"sourceMethod": "getCompression()"
},
{
"name": "server.error",
"type": "org.springframework.boot.autoconfigure.web.ErrorProperties",
"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties",
"sourceMethod": "getError()"
},
{
"name": "server.error.whitelabel",
"type": "org.springframework.boot.autoconfigure.web.ErrorProperties$Whitelabel",
"sourceType": "org.springframework.boot.autoconfigure.web.ErrorProperties",
"sourceMethod": "getWhitelabel()"
}
。。。
],
"properties": [
{
"name": "server.address",
"type": "java.net.InetAddress",
"description": "Network address to which the server should bind.",
"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"
},
{
"name": "server.compression.enabled",
"type": "java.lang.Boolean",
"description": "Whether response compression is enabled.",
"sourceType": "org.springframework.boot.web.server.Compression",
"defaultValue": false
},
{
"name": "server.compression.excluded-user-agents",
"type": "java.lang.String[]",
"description": "Comma-separated list of user agents for which responses should not be compressed.",
"sourceType": "org.springframework.boot.web.server.Compression"
},
{
"name": "server.compression.mime-types",
"type": "java.lang.String[]",
"description": "Comma-separated list of MIME types that should be compressed.",
"sourceType": "org.springframework.boot.web.server.Compression",
"defaultValue": [
"text\/html",
"text\/xml",
"text\/plain",
"text\/css",
"text\/javascript",
"application\/javascript",
"application\/json",
"application\/xml"
]
},
{
"name": "server.compression.min-response-size",
"type": "org.springframework.util.unit.DataSize",
"description": "Minimum \"Content-Length\" value that is required for compression to be performed.",
"sourceType": "org.springframework.boot.web.server.Compression",
"defaultValue": "2KB"
},
{
"name": "server.error.include-binding-errors",
"type": "org.springframework.boot.autoconfigure.web.ErrorProperties$IncludeAttribute",
"description": "When to include \"errors\" attribute.",
"sourceType": "org.springframework.boot.autoconfigure.web.ErrorProperties",
"defaultValue": "never"
},
{
"name": "server.error.include-exception",
"type": "java.lang.Boolean",
"description": "Include the \"exception\" attribute.",
"sourceType": "org.springframework.boot.autoconfigure.web.ErrorProperties",
"defaultValue": false
},
{
"name": "server.error.include-message",
"type": "org.springframework.boot.autoconfigure.web.ErrorProperties$IncludeAttribute",
"description": "When to include \"message\" attribute.",
"sourceType": "org.springframework.boot.autoconfigure.web.ErrorProperties",
"defaultValue": "never"
},
{
"name": "server.error.include-stacktrace",
"type": "org.springframework.boot.autoconfigure.web.ErrorProperties$IncludeAttribute",
"description": "When to include the \"trace\" attribute.",
"sourceType": "org.springframework.boot.autoconfigure.web.ErrorProperties",
"defaultValue": "never"
},
{
"name": "server.error.path",
"type": "java.lang.String",
"description": "Path of the error controller.",
"sourceType": "org.springframework.boot.autoconfigure.web.ErrorProperties",
"defaultValue": "\/error"
},
{
"name": "server.error.whitelabel.enabled",
"type": "java.lang.Boolean",
"description": "Whether to enable the default error page displayed in browsers in case of a server error.",
"sourceType": "org.springframework.boot.autoconfigure.web.ErrorProperties$Whitelabel",
"defaultValue": true
},
{
"name": "server.forward-headers-strategy",
"type": "org.springframework.boot.autoconfigure.web.ServerProperties$ForwardHeadersStrategy",
"description": "Strategy for handling X-Forwarded-* headers.",
"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"
},
{
"name": "server.max-http-header-size",
"type": "org.springframework.util.unit.DataSize",
"description": "Maximum size of the HTTP message header.",
"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties",
"defaultValue": "8KB"
},
{
"name": "server.port",
"type": "java.lang.Integer",
"description": "Server HTTP port.",
"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties",
"defaultValue": 8080
},
{
"name": "server.server-header",
"type": "java.lang.String",
"description": "Value to use for the Server response header (if empty, no header is sent).",
"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"
}
{
"name": "server.shutdown",
"type": "org.springframework.boot.web.server.Shutdown",
"description": "Type of shutdown that the server will support.",
"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties",
"defaultValue": "immediate"
}
。。。
],
"hints": [
{
"name": "server.servlet.jsp.class-name",
"providers": [
{
"name": "class-reference",
"parameters": {
"target": "javax.servlet.http.HttpServlet"
}
}
]
},
{
"name": "server.tomcat.accesslog.encoding",
"providers": [
{
"name": "handle-as",
"parameters": {
"target": "java.nio.charset.Charset"
}
}
]
},
{
"name": "server.tomcat.accesslog.locale",
"providers": [
{
"name": "handle-as",
"parameters": {
"target": "java.util.Locale"
}
}
]
}
。。。
]
}
**************
groups 标签
groups 包含的属性
name:组名
type:组名对应的数据类型
description:组名简短描述
sourceType:元类型,@ConfigurationProperties标注在类上,sourceType为类的名称
@ConfigurationProperties标注在方法上(方法上标注@Bean),sourceType为方法所在的类
sourceMethod:源方法
示例
*************
ServerProperties
@ConfigurationProperties(prefix = "server",ignoreUnknownFields = true)
//ServerProperties上有注解@ConfigurationProperties
public class ServerProperties {
@NestedConfigurationProperty
private final ErrorProperties error = new ErrorProperties();
@NestedConfigurationProperty
private final Compression compression;
public ErrorProperties getError() {
return this.error;
}
public Compression getCompression() {
return this.compression;
}
*************
ErrorProperties
public class ErrorProperties {
private final ErrorProperties.Whitelabel whitelabel;
*************
groups 设置
"groups": [
{
"name": "server",
"type": "org.springframework.boot.autoconfigure.web.ServerProperties",
"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"
},
{
"name": "server.compression",
"type": "org.springframework.boot.web.server.Compression",
//方法返回类型为Compression
"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties",
//方法所在的类为ServerProperties
"sourceMethod": "getCompression()"
},
{
"name": "server.error",
"type": "org.springframework.boot.autoconfigure.web.ErrorProperties",
//方法返回类型为ErrorProperties
"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties",
//方法所在的类为ServerProperties
"sourceMethod": "getError()"
},
{
"name": "server.error.whitelabel",
"type": "org.springframework.boot.autoconfigure.web.ErrorProperties$Whitelabel",
//方法返回类型为Whitelabel(ErrorProperties的内部类)
"sourceType": "org.springframework.boot.autoconfigure.web.ErrorProperties",
//方法所在的类为ServerProperties
"sourceMethod": "getWhitelabel()"
}
**************
properties 标签
properties 可包含的属性
name:property属性名称
type:属性数据类型,如:java.lang.String、java.util.Map<java.lang.String,com.example.MyEnum>
description:属性描述
sourceType:包含该属性的类
defaultValue:属性默认值
deprecation:属性是否被禁用
deprecation属性设置:
level:warning(默认) 应用中仍会使用
error 应用中不会使用
reason:属性禁用理由
replacement:替换的属性
示例
@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;
}
}
"properties": [
{
"name": "server.address",
"type": "java.net.InetAddress",
"description": "Network address to which the server should bind.",
"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"
},
{
"name": "server.compression.enabled",
"type": "java.lang.Boolean",
"description": "Whether response compression is enabled.",
"sourceType": "org.springframework.boot.web.server.Compression",
"defaultValue": false
}
。。。
]
**************
hints 标签
hints 可包含的属性
name:提示的属性名称
values:属性值,ValueHint[]
providers:provider及参数,ValueProvider[]
values 可包含的属性
value:提示的值
description:相关描述
providers 可包含的属性
name:provider名称
parameters:provider支持的参数,数据类型为JSON object
示例
{"hints": [
{
"name": "my.contexts.keys",
"values": [
{
"value": "sample1"
},
{
"value": "sample2"
}
]
}
]}
provider name可选值
# any
The special any provider value permits any additional values to be provided.
This provider is typically used if you have a list of values and any extra
values should still be considered as valid
{"hints": [
{
"name": "system.state",
"values": [
{
"value": "on"
},
{
"value": "off"
}
],
"providers": [
{
"name": "any"
}
]
}
]}
# class-reference:The class-reference provider auto-completes classes available in the project
parameter包含属性
target:The fully qualified name of the class that should be assignable to the chosen value
concrete:Specify whether only concrete classes are to be considered as valid candidates
{"hints": [
{
"name": "server.servlet.jsp.class-name",
"providers": [
{
"name": "class-reference",
"parameters": {
"target": "javax.servlet.http.HttpServlet"
}
}
]
}
]}
# handle-as:lets you substitute the type of the property to a more high-level type.
This typically happens when the property has a java.lang.String type, because you do not
want your configuration classes to rely on classes that may not be on the classpath
parameters 包含唯一属性target,target可选值:
java.lang.Enum:Lists the possible values for the property
java.nio.charset.Charset:Supports auto-completion of charset/encoding values
java.util.Locale: auto-completion of locales (such as en_US)
org.springframework.util.MimeType:Supports auto-completion of content type values (such as text/plain)
org.springframework.core.io.Resource: Supports auto-completion of Spring’s Resource abstraction to refer to a file on the filesystem or on the classpath (such as classpath:/sample.properties)
{"hints": [
{
"name": "spring.liquibase.change-log",
"providers": [
{
"name": "handle-as",
"parameters": {
"target": "org.springframework.core.io.Resource"
}
}
]
}
]}
# logger-name:auto-completes valid logger names and logger groups
parameter 包含的属性
group:Specify whether known groups should be considered
{"hints": [
{
"name": "logging.level.keys",
"values": [
{
"value": "root",
"description": "Root logger used to assign the default logging level."
},
{
"value": "sql",
"description": "SQL logging group including Hibernate SQL logger."
},
{
"value": "web",
"description": "Web logging group including codecs."
}
],
"providers": [
{
"name": "logger-name"
}
]
},
{
"name": "logging.level.values",
"values": [
{
"value": "trace"
},
{
"value": "debug"
},
{
"value": "info"
},
{
"value": "warn"
},
{
"value": "error"
},
{
"value": "fatal"
},
{
"value": "off"
}
],
"providers": [
{
"name": "any"
}
]
}
]}
# spring-bean-reference:auto-completes the beans that are defined in the configuration of the current project
parameters包含属性target
target:The fully qualified name of the bean class that should be assignable to the candidate
{"hints": [
{
"name": "spring.jmx.server",
"providers": [
{
"name": "spring-bean-reference",
"parameters": {
"target": "javax.management.MBeanServer"
}
}
]
}
]}
# spring-profile-name:auto-completes the Spring profiles that are defined in the configuration of the current project
{"hints": [
{
"name": "spring.profiles.active",
"providers": [
{
"name": "spring-profile-name"
}
]
}
]}
**********************
示例
**************
导入 jar 包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
**************
配置文件
application.yml
person:
name: 瓜田李下
age: 20
**************
pojo 层
Person
@Data
@Component
@ConfigurationProperties("person")
public class Person {
private String name;
private Integer age;
}
**************
controller 层
HelloController
@RestController
public class HelloController {
@Resource
private Person person;
@RequestMapping("/hello")
public Person hello(){
System.out.println(person);
return person;
}
}
**********************
使用测试
**************
默认生成的提示文件
点击项目运行,编译后会自动生成spring-configuration-metadata.json
编译后,生成的spring-configuration-metadata.json
{
"groups": [
{
"name": "person",
"type": "com.example.demo.pojo.Person",
"sourceType": "com.example.demo.pojo.Person"
}
],
"properties": [
{
"name": "person.age",
"type": "java.lang.Integer",
"sourceType": "com.example.demo.pojo.Person"
},
{
"name": "person.name",
"type": "java.lang.String",
"sourceType": "com.example.demo.pojo.Person"
}
],
"hints": []
}
**************
自定义配置提示文件
resources 目录下新建META-INF/additional-spring-configuration-metadata.json
additional-spring-configuration-metadata.json
{
"properties": [
{
"name": "person.age",
"type": "java.lang.Integer",
"description": "用户年龄.",
"sourceType": "com.example.demo.pojo.Person"
},
{
"name": "person.name",
"type": "java.lang.String",
"description": "用户姓名.",
"sourceType": "com.example.demo.pojo.Person"
}
]
}
编译后,该文件会自动合并到spring-configuration-metadata.json
编译后,target/classes/META-INF目录下生成spring-configuration-metadata.json
编译后,生成的spring-configuration-metadata.json
{
"groups": [
{
"name": "person",
"type": "com.example.demo.pojo.Person",
"sourceType": "com.example.demo.pojo.Person"
}
],
"properties": [
{
"name": "person.age",
"type": "java.lang.Integer",
"description": "用户年龄.",
"sourceType": "com.example.demo.pojo.Person"
},
{
"name": "person.name",
"type": "java.lang.String",
"description": "用户姓名.",
"sourceType": "com.example.demo.pojo.Person"
}
],
"hints": []
}