springboot 配置 Metadata

## 简述 作用 yml中配置自定义属性 [官网文档](https://docs.spring.io/spring-boot/docs/3.0.6/reference/html/configuration-metadata.html#appendix.configuration-metadata.manual-hints.value-providers.spring-profile-name) 1. 配置实体类 ```text @Component @ConfigurationProperties(prefix = "application") #也可以在属性上通过@Value 设置默认值 ``` 2. 配置yaml ```text #编写具体参数带有提示 application: name: Demo version: 0.0.1 website: www.monody.net ``` 3. 运行 ```text 运行之后若想在/target/classes/META-INF 下生成 spring-configuration-metadata.json 需要导入 org.springframework.boot spring-boot-configuration-processor true ``` ## spring-configuration-metadata.json格式 ```text 其中的项目分类在“组”或“属性”下,默认值提示分类在“提示”下 ``` 具体常用属性可查看 [官网文档](https://docs.spring.io/spring-boot/docs/3.0.6/reference/html/configuration-metadata.html#appendix.configuration-metadata.manual-hints.value-providers.spring-profile-name) ## 注意事项 可以嵌套 ```text @ConfigurationProperties(prefix = "my.server") public class MyServerProperties { private String name; private Host host; public static class Host { private String ip; private int port; } #ymal my: server: name: test host: ip: localhost port: 8899 ``` ### 绑定properties文件 ```text @PropertySource(value = {"classpath:metadata/applicationInfo.properties"}) @Component @ConfigurationProperties(prefix = "application") ``` ```text #applicationInfo.properties application.name=Demo application.version=0.0.1 application.website=www.monody.net ``` ```text #在配置文件引用其他变量 ${application.name} #在不存在时配置默认 ${application.name:monody} ``` *

*[返回教程主页](https://www.monody.net/p/blog-page_3.html)*

*

评论