You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Spring @ConfigurationProperties将配置绑定为Java POJO Map?

实现步骤


1. 调整配置文件

给所有Kafka监听配置加上统一前缀,方便Spring定位绑定的根节点,调整后的application.properties示例:

kafka.listener.topic1.topicname =testTopic1
kafka.listener.topic1.listenerid=id1
kafka.listener.topic1.max.poll.records=5
kafka.listener.topic2.topicname =testTopic2
kafka.listener.topic2.listenerid=id2
kafka.listener.topic2.max.poll.records=5

2. 编写属性映射POJO

Spring Boot的宽松绑定规则会自动匹配配置中的点分隔、小写属性到POJO的驼峰属性,无需额外转换配置:

// 若使用Lombok,直接加@Data注解即可省去手动编写getter/setter
public class CustomKafkaListenerProperty {
    private String topicname;
    private String listenerId;
    private String maxPollRecords;

    public String getTopicname() {
        return topicname;
    }

    public void setTopicname(String topicname) {
        this.topicname = topicname;
    }

    public String getListenerId() {
        return listenerId;
    }

    public void setListenerId(String listenerId) {
        this.listenerId = listenerId;
    }

    public String getMaxPollRecords() {
        return maxPollRecords;
    }

    public void setMaxPollRecords(String maxPollRecords) {
        this.maxPollRecords = maxPollRecords;
    }
}

3. 编写配置属性绑定类

使用@ConfigurationProperties指定前缀,声明对应Map结构的属性即可完成自动绑定:

import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.Map;

@ConfigurationProperties(prefix = "kafka")
public class KafkaListenerProperties {
    // Map的key自动对应配置中的topic1、topic2,value自动绑定为CustomKafkaListenerProperty实例
    private Map<String, CustomKafkaListenerProperty> listener;

    public Map<String, CustomKafkaListenerProperty> getListener() {
        return listener;
    }

    public void setListener(Map<String, CustomKafkaListenerProperty> listener) {
        this.listener = listener;
    }
}

4. 开启配置属性扫描

在项目启动类上添加@EnableConfigurationProperties注解,注册我们编写的配置类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication
@EnableConfigurationProperties(KafkaListenerProperties.class)
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

使用方式

在需要用到配置的Bean中直接注入KafkaListenerProperties即可读取对应配置:

@Service
public class KafkaConsumerService {
    private final KafkaListenerProperties listenerProperties;

    // 构造方法注入
    public KafkaConsumerService(KafkaListenerProperties listenerProperties) {
        this.listenerProperties = listenerProperties;
    }

    public void initConsumer() {
        // 获取topic1的配置
        CustomKafkaListenerProperty topic1Config = listenerProperties.getListener().get("topic1");
        System.out.println(topic1Config.getTopicname()); // 输出testTopic1
    }
}

内容的提问来源于stack exchange,提问作者Digital

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 16:24:01