1. 引入maven依赖
org.springframework.boot
spring-boot-configuration-processor
true
注:该依赖非常强大,有兴趣可下载源码查看实现方式,基于工厂模式
2.配置类
@Configuration
@ConfigurationProperties(prefix = "rocketmq.consume")
public class MqConsumeClientConfig {
private String nameServer;
private String groupName;
private String instanceName;
private String isTransaction;
private Map topics;
private Map tags;
}
@Configuration
@ConfigurationProperties(prefix = "rocketmq.produce")
public class MqProduceClientConfig {
private String nameServer;
private String groupName;
private String instanceName;
private String isTransaction;
private Map topics;
private Map tags;
@Bean(name = "mqProducer")
public DefaultRocketMqProducer defaultRocketMqProducer() {
return new DefaultRocketMqProducer();
}
@Bean(name = "producer")
public DefaultMQProducer defaultMQProducer( ) {
DefaultMQProducer producer = new DefaultMQProducer(groupName);
producer.setNamesrvAddr(nameServer);
producer.setVipChannelEnabled(false);
try {
producer.start();
} catch (MQClientException e) {
System.exit(1);
return null;
}
return producer;
}
}
@Configuration
@ConfigurationProperties(prefix = "rocketmq.produce")
配合使用,@Configuration 指明配置类;@ConfigurationProperties(prefix = "rocketmq.*") 指明前缀
3. 配置文件
rocketmq:
produce:
nameServer: 127.0.0.1:9876
groupName: ProduceAccountGroupDev
instanceName: ProduceAccountInstanceDev
isTransaction: 0
topics:
key1: AccountTopicDev
key2: AnswerTopicDev
tags:
key1: AccountTag1Dev
key2: AccountTag1Dev
consume:
nameServer: 127.0.0.1:9876
groupName: ConsumeAccountGroupDev
instanceName: ConsumeAccountInstanceDev
isTransaction: 0
topics:
key1: AccountTopicDev
key2: AnswerTopicDev
tags:
key1: All
key2: All
二、实现自动提示
一般在我们开发中,属性文件会产生一个自动提示,这个自定义提示也可以把我们的配置类添加到提示中。其实这是元数据文件气的作用,
提供所有支持的配置属性的详细信息。这些文件旨在允许IDE开发人员在用户使用application.properties 或application.yml文件时提供上下文帮助和“代码完成” 。
主要的元数据文件是在编译器通过处理所有被@ConfigurationProperties注解的节点来自动生成的。
1. 元数据 spring-configuration-metadata.json
配置元数据位于jars文件中的META-INF/spring-configuration-metadata.json,它们使用一个具有”groups”或”properties”分类节点的简单JSON格式:
{"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"
},
{
"name": "server.servlet-path",
"type": "java.lang.String",
"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"
"defaultValue": "/"
}
...
]}
每个”property”是一个配置节点,用户可以使用特定的值指定它。例如,server.port和server.servlet-path可能在application.properties中如以下定义
server.port=9090
server.servlet-path=/home
“groups”是高级别的节点,它们本身不指定一个值,但为properties提供一个有上下文关联的分组。例如,server.port和server.servlet-path属性是server组的一部分。
注:不需要每个”property”都有一个”group”,一些属性可以以自己的形式存在。
2. 配置元数据步骤
1) 仍然是引入同样的依赖
2) 在\resources\META-INF\下添加spring-configuration-metadata.json文件。该文件可以由idea生成。
3. 生成元数据文件
1)仍然是引入同样的依赖
2)利用idea生成spring-configuration-metadata.json文件
在idea中, Ctrl + Alt + S 快捷键打开Settings,搜索Annotation Processors,接下来勾住Enable annonation processing保存;重新编译项目。
此时可以在编译后的文件中看到自动生成的spring-configuration-metadata.json。
进一步打开文件如下:
{
"hints": [],
"groups": [
{
"sourceType": "com.feeler.interlive.config.MqConsumeClientConfig",
"name": "rocketmq.consume",
"type": "com.feeler.interlive.config.MqConsumeClientConfig"
},
{
"sourceType": "com.feeler.interlive.config.MqProduceClientConfig",
"name": "rocketmq.produce",
"type": "com.feeler.interlive.config.MqProduceClientConfig"
}
],
"properties": [
{
"sourceType": "com.feeler.interlive.config.MqConsumeClientConfig",
"name": "rocketmq.consume.group-name",
"type": "java.lang.String"
},
{
"sourceType": "com.feeler.interlive.config.MqConsumeClientConfig",
"name": "rocketmq.consume.instance-name",
"type": "java.lang.String"
},
{
"sourceType": "com.feeler.interlive.config.MqConsumeClientConfig",
"name": "rocketmq.consume.is-transaction",
"type": "java.lang.String"
},
{
"sourceType": "com.feeler.interlive.config.MqConsumeClientConfig",
"name": "rocketmq.consume.name-server",
"type": "java.lang.String"
},
{
"sourceType": "com.feeler.interlive.config.MqConsumeClientConfig",
"name": "rocketmq.consume.tags",
"type": "java.util.Map"
},
{
"sourceType": "com.feeler.interlive.config.MqConsumeClientConfig",
"name": "rocketmq.consume.topics",
"type": "java.util.Map"
},
{
"sourceType": "com.feeler.interlive.config.MqProduceClientConfig",
"name": "rocketmq.produce.group-name",
"type": "java.lang.String"
},
{
"sourceType": "com.feeler.interlive.config.MqProduceClientConfig",
"name": "rocketmq.produce.instance-name",
"type": "java.lang.String"
},
{
"sourceType": "com.feeler.interlive.config.MqProduceClientConfig",
"name": "rocketmq.produce.is-transaction",
"type": "java.lang.String"
},
{
"sourceType": "com.feeler.interlive.config.MqProduceClientConfig",
"name": "rocketmq.produce.name-server",
"type": "java.lang.String"
},
{
"sourceType": "com.feeler.interlive.config.MqProduceClientConfig",
"name": "rocketmq.produce.tags",
"type": "java.util.Map"
},
{
"sourceType": "com.feeler.interlive.config.MqProduceClientConfig",
"name": "rocketmq.produce.topics",
"type": "java.util.Map"
}
]
}
到此为止,已经实现了基于前缀配置和自动提示功能
参考:https://blog.csdn.net/L_Sail/article/details/70342023