引入配置管理
在开发一个组件的时候,需要引入者提供配置选项,比如是否开启,比如配置的规则,需要在引入方进行配置。
- 组件申明配置类
使用@ConfigurationProperties(“xx.prefix”)
@ConfigurationProperties("nacos.config")
public class NacosConfigProperties {
private String serverAddr;
private String contextPath;
// todo. get set
}
- 在META-INF配置提示语
在META-INF中新创 spring-configuration-metadata.json
{
hints: [ ],
groups: [ ],
properties: [
{
sourceType: "com.alibaba.boot.nacos.config.properties.NacosConfigProperties",
name: "nacos.config.enabled",
description: "enable or disable nacos config feature",
type: "java.lang.Boolean"
}
}
- 在启动类添加@EnableConfigurationProperties({NacosConfigProperties.class})
其中对应的class为配置类