在配置additional-spring-configuration-metadata.json
文件后,在开发人员的IDE工具使用个人编写的配置读取很有效的在application.properties
或application.yml
文件下完成提示。
配置元数据文件位于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"}...
],"properties": [{"name": "server.port","type": "java.lang.Integer","sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"}...
],"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."}]}
]}
properties
表格进行配置上的含义:
名称 | 类型 | 目的 |
name | String | 属性的全名。名称采用小写的周期分隔形式(例如server.address)。此属性是强制性的。 |
type | String | 属性的数据类型的完整签名(例如java.lang.String),但也是完整的泛型类型(例如java.util.Map )。您可以使用此属性来指导用户可以输入的值的类型。为了保持一致性,通过使用其包装对应项(例如,boolean变为java.lang.Boolean)来指定基元的类型。请注意,此类可能是一个复杂类型,它从Stringas绑定的值转换而来。如果类型未知,则可以省略。 |
description | String | 可以向用户显示的组的简短描述。如果没有可用的描述,则可以省略。建议描述为简短段落,第一行提供简明摘要。描述中的最后一行应以句点(.)结尾。 |
sourceType | String | 贡献此属性的源的类名称。例如,如果属性来自带注释的类@ConfigurationProperties,则此属性将包含该类的完全限定名称。如果源类型未知,则可以省略。 |
defaultValue | Object | 默认值,如果未指定属性,则使用该值。如果属性的类型是数组,则它可以是值数组。如果默认值未知,则可以省略。 |
groups组比properties多了一个属性 sourceMethod
sourceMethod | String | 获取对象的方法的全名(包括括号和参数类型)(例如,@ConfigurationProperties注释的@Bean方法的名称)。如果源方法未知,则可以省略。 |
案例:自定义实现mongodb信息的加载
定义配置类,使用@Component和@ConfigurationProperties注解
@Component
@Getter
@Setter
@ToString
@ConfigurationProperties(prefix = "zhong.data.mongodb")
public class MongoDbProperties {//数据库名称private String database;//用户名private String username;//密码private String password;//地址 host:portprivate String address;//设置你的认证数据库,如果有的话private String authenticationDatabase;private MongoDbFactoryProperties mongoDbFactoryProperties;@Beanpublic MongoDbFactoryProperties getMongoDbFactory(@Autowired MongoDbFactoryProperties mongoDbFactoryProperties){return this.mongoDbFactoryProperties = mongoDbFactoryProperties;}
}
additional-spring-configuration-metadata.json的内容如下:
{"groups": [{"name": "zhong.data.mongodb.database","type": "java.lang.String","description": "mongodb的数据库名称","sourceType": "com.zhong.springdemo.mangodbdome.configure.MongoDbProperties"},{"name": "zhong.data.mongodb.mongoDbFactoryProperties","type": "com.zhong.springdemo.mangodbdome.configure.MongoDbFactoryProperties","description": "线程池配置信息","sourceType": "com.zhong.springdemo.mangodbdome.configure.MongoDbProperties","sourceMethod": "getMongoDbFactory()"}],"properties": [{"name": "zhong.data.mongodb.username","type": "java.lang.String","description": "mongodb的用户名","sourceType": "com.zhong.springdemo.mangodbdome.configure.MongoDbProperties","defaultValue": ""},{"name": "zhong.data.mongodb.password","type": "java.lang.String","description": "mongodb的密码","sourceType": "com.zhong.springdemo.mangodbdome.configure.MongoDbProperties","defaultValue": "123456"},{"name": "zhong.data.mongodb.address","type": "java.lang.String","description": "mongodb的用户名地址","sourceType": "com.zhong.springdemo.mangodbdome.configure.MongoDbProperties","defaultValue": "127.0.0.1:27017"}],"hints": []
}
这样我们在application.proerties或application.yaml时使用了。
需要注意的是,如果需要将我们自定义的设置加载容器中,则需要保证在程序启动时能扫描到指定的jar包路径。
方法是:在@SpringBootApplication注解中配置scanBasePackages
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, MongoAutoConfiguration.class,MongoDataAutoConfiguration.class}, scanBasePackages = {"com.zhong.springdemo.mangodbdome"})
@EnableSwagger2Doc
public class WebDemoApplication {public static void main(String[] args){SpringApplication.run(WebDemoApplication.class, args);}
}
参考:
Appendix B. Configuration Metadata
springboot之additional-spring-configuration-metadata.json自定义提示 - Purgeyao - 博客园