1.效果示例
2.问题说明
1.自定义的starter或者模块中的属性配置类在配置文件中给予提示
2.配置类中要有注释说明,且要有set\get方法,直接加@Data注解方便
3.将maven项目clean后install,在target\classes\META-INF下会生成spring-configuration-metadata.json文件,将该文件拷贝到src\main\resources\META-INF下,删除target下的,打包的时候可以去掉依赖包
4.json文件中可以加hints来说明可选值
3.pom依赖配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<!--是否向下传递-->
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
4.配置类
package com.learning.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @Author wangyouhui
* @Description TODO
**/
@Component("testProperties")
@ConfigurationProperties(prefix = "tool")
@Data
public class TestProperties {
/**
* 提示
*/
private String tip;
/**
* 模式
*/
private String model;
}
5.spring-configuration-metadata.json
{
"groups": [
{
"name": "tool",
"type": "com.learning.config.TestProperties",
"sourceType": "com.learning.config.TestProperties"
}
],
"properties": [
{
"name": "tool.model",
"type": "java.lang.String",
"description": "模式",
"sourceType": "com.learning.config.TestProperties"
},
{
"name": "tool.tip",
"type": "java.lang.String",
"description": "提示",
"sourceType": "com.learning.config.TestProperties"
}
],
"hints": [
{
"name": "tool.model",
"values": [
{
"value": "detail",
"description": "详情模式."
},
{
"value": "simple",
"description": "极简模式."
}
]
}
]
}
6.项目结构截图
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/92334.html