当前位置 主页 > 服务器问题 > Linux/apache问题 >
目的
配置自动提示的辅助功能可以让配置写起来更快,准确率大大提高。
springboot jar 包含提供所有支持的配置属性细节的元数据文件。文件的目的是为了让 IDE 开发者在用户使用 application.properties 或 application.yml 文件时提供上下文帮助和代码补全。
大多数元数据文件是在编译时通过处理用 @ConfigurationProperties 注释的所有项自动生成的。也可以手动编写部分元数据。
版本
参考 SpringBoot 2.2.0.RELEASE 文档
文件
jar包中的 META-INF/spring-configuration-metadata.json (自动生成)或 META-INF/additional-spring-configuration-metadata.json (手动添加)
实战
<!-- 引入相关依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> @Configuration @ConfigurationProperties(prefix = "file.upload") public class FileUploadConfig { /** Maximum number of bytes per file */ private String maxSize = "1024M"; /** 不允许的文件后缀 */ private String rejectSuffix; //注意:使用的时候必须要有getter/setter,否则不会自动生成该属性对应的提示 //此处因为篇幅原因省略 getter/setter } @Configuration @ConfigurationProperties("map.test") public class MapTestConfig { /** 测试Map类型数据的提示 */ private Map<String, Object> data; //注意:使用的时候必须要有getter/setter,否则不会自动生成该属性对应的提示 //此处因为篇幅原因省略 getter/setter }
中文注释会乱码,以上故意用中文注释的地方,会在下面文件中指定对应的描述,看是否会覆盖。
additional-spring-configuration-metadata.json { "properties": [ { "name": "file.upload.reject-suffix", "type": "java.lang.String", "defaultValue": "exe,jar", "description": "The file suffix is not allowed.", "sourceType": "com.lw.metadata.config.FileUploadConfig" }, { "name": "map.test.data", "type": "java.util.Map", "description": "Tips for testing Map type data.", "sourceType": "com.lw.metadata.config.MapTestConfig" } ], "hints": [ { "name": "map.test.data.keys", "values": [ { "value": "name", "description": "The name of the person." }, { "value": "sex", "description": "The sex of the person." } ] } ] }
maven compile 之后,生成的 additional-spring-configuration-metadata.json 与源码中的一样,生成的 spring-configuration-metadata.json 如下:
{ "groups": [ { "name": "file.upload", "type": "com.lw.metadata.config.FileUploadConfig", "sourceType": "com.lw.metadata.config.FileUploadConfig" }, { "name": "map.test", "type": "com.lw.metadata.config.MapTestConfig", "sourceType": "com.lw.metadata.config.MapTestConfig" } ], "properties": [ { "name": "file.upload.max-size", "type": "java.lang.String", "description": "Maximum number of bytes per file", "sourceType": "com.lw.metadata.config.FileUploadConfig", "defaultValue": "1024M" }, { "name": "file.upload.reject-suffix", "type": "java.lang.String", "description": "The file suffix is not allowed.", "sourceType": "com.lw.metadata.config.FileUploadConfig", "defaultValue": "exe,jar" }, { "name": "map.test.data", "type": "java.util.Map<java.lang.String,java.lang.Object>", "description": "Tips for testing Map type data.", "sourceType": "com.lw.metadata.config.MapTestConfig" } ], "hints": [ { "name": "map.test.data.keys", "values": [ { "value": "name", "description": "The name of the person." }, { "value": "sex", "description": "The sex of the person." } ] } ] }