SpringBoot中@ConfigurationProperties对于多层嵌套参数的正确加载方法
SpringBoot中@ConfigurationProperties对于多层嵌套参数的正确加载方法
今天想重构一下Kafka代码,将之前用@Value引入的一些参数全部替换掉,因为可读性和维护性太差,这是以前的样子。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| @Value("${spring.kafka.bootstrap-servers}") private String servers; @Value("${spring.kafka.consumer.group-id}") private String groupId; @Value("${spring.kafka.consumer.auto-offset-reset:}") private String reset; @Value("${spring.kafka.consumer.enable-auto-commit}") private String commit; @Value("${spring.kafka.consumer.max-poll-records}") private String records; @Value("${spring.kafka.consumer.max-poll-interval-ms}") private String intervams; @Value("${spring.kafka.consumer.auto-commit-interval}") private String interval; @Value("${spring.kafka.consumer.key-deserializer}") private String key; @Value("${spring.kafka.consumer.value-deserializer}") private String value; @Value("${spring.kafka.consumer.properties.sasl.mechanism:}") private String mechanism; @Value("${spring.kafka.consumer.properties.security.protocol:}") private String protocol;
|
有同学可能会问,为什么不直接用spring集成好的Kafka,因为它封装的太好,反而不能使用一些原生的接口,所以打算重构一遍代码,至于重构的过程和优化的内容,留给以后的文章去讲解,这篇只是想使用@ConfigurationProperties把参数格式化掉,可以直接提取使用,在网上搜了一些文章,写的很乱,有的甚至完全不能实现功能,最后在一通乱怼下终于符合了自己的需求,Kafka的配置参数存在多层结构,所以不能直接配置参数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| spring: kafka: bootstrap-servers: xxx.xxx.xxx.xxx:9092 producer: retries: 0 batch-size: 16384 buffer-memory: 33554432 key-serializer: org.apache.kafka.common.serialization.StringSerializer value-serializer: org.apache.kafka.common.serialization.StringSerializer consumer: group-id: views.invoke auto-offset-reset: earliest enable-auto-commit: true max-poll-records: 100 max-poll-interval-ms: 60000 auto-commit-interval: 100 key-deserializer: org.apache.kafka.common.serialization.StringDeserializer value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
|
只要是类似于这样结构的参数,都可以参考我的写法,理论上是没有问题的。
下面是我怼出来的写法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| @Component @ConfigurationProperties(prefix = "spring.kafka") @Getter @Setter public class KafkaConfig {
private String bootstrapServers; private ProducerProperties producer = new ProducerProperties() ; private ConsumerProperties consumer = new ConsumerProperties();
@NoArgsConstructor @AllArgsConstructor @Getter @Setter public class ProducerProperties { private String retries; private String batchSize; private String bufferMemory; private String keySerializer; private String valueSerializer; }
@NoArgsConstructor @AllArgsConstructor @Getter @Setter public class ConsumerProperties { private String groupId; private String autoOffsetReset; private String enableAutoCommit; private String maxPollRecords; private String maxPollIntervalMs; private String autoCommitInterval; private String keyDeserializer; private String valueDeserializer; }
}
|
这里用到了lombok,代码结构也比较简洁,参数中间的-可以用大写来替代,spring会自动匹配的。
错误的做法
下面这种是很多文章里写的,测试了很多遍,基本获取不到参数,就能得到bootstrapServers。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| @Configuration @EnableAutoConfiguration @Getter @Setter public class KafkaConfig {
private String bootstrapServers;
@Bean @ConfigurationProperties(prefix="spring.kafka.producer") public ProducerMyConfig getProducerConfig(){ return new ProducerMyConfig(); }
@Bean @ConfigurationProperties(prefix="spring.kafka.consumer") public ConsumerMyConfig getConsumerConfig(){ return new ConsumerMyConfig(); }
private class ProducerMyConfig { private String retries; private String batchSize; private String bufferMemory; private String keySerializer; private String valueSerializer; }
private class ConsumerMyConfig { private String groupId; private String autoOffsetReset; private String enableAutoCommit; private String maxPollRecords; private String maxPollIntervalMs; private String autoCommitInterval; private String keyDeserializer; private String valueDeserializer; } }
|
- Title: SpringBoot中@ConfigurationProperties对于多层嵌套参数的正确加载方法
- Author: algorain
- Created at: 2020-04-10 22:13:31
-
Updated at: 2023-05-14 21:39:50
-
Link: http://www.rain1024.com/2020/04/10/springboot中configurationproperties对于多层嵌套参数的正确加载方法/
-
License: This work is licensed under CC BY-NC-SA 4.0.