0

我有一组属性如下:

spring.kafka.producer.edwh.bootstrap-servers=localhost:9092
spring.kafka.producer.edwh.properties.enable.idempotence=true
spring.kafka.producer.edwh.retries=10
spring.kafka.producer.edwh.transaction-id-prefix=slv
spring.kafka.producer.edwh.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer
spring.kafka.producer.edwh.properties.spring.json.add.type.headers=false

...我想通过使用以下方式映射到这样的类@ConfigurationProperties(prefix = "spring.kafka.producer.edwh")

@ConfigurationProperties(prefix = "spring.kafka.producer.edwh")
public class EdwhKafkaProducerConfig {

    private String bootstrap_servers;
    private String properties_enable_idempotence;
    private int retries;
    private String transaction_id_prefix;
    private String value_serializer;
    private boolean properties_spring_json_add_type_headers;

}

... 我能怎么做?

4

2 回答 2

0

虚线属性表示单独的对象。所以如果你有

mail.additionalHeaders.redelivery=true
mail.additionalHeaders.secure=true
mail.credentials.username=john
mail.credentials.password=password

然后你的配置类可以如下所示:

@ConfigurationProperties(prefix = "mail")
public class ConfigProperties {
    private AdditionalHeaders additionalHeaders;
    private Credentials credentials;
    // getters setters

    public class AdditionalHeaders {
        private boolean redelivery;
        private boolean secure;
        // getters setters
    }

    public class Credentials {
        private String username;
        private String password;
        // getters setters
    }
}

看看这里: https ://www.baeldung.com/configuration-properties-in-spring-boot

于 2020-03-13T10:13:41.773 回答
0

如果您使用的是 Spring Boot,那么您需要在您的情况下注释您的主应用程序类:

@EnableConfigurationProperties(value = EdwhKafkaProducerConfig.class)

您可能还需要为配置属性定义公共访问器方法。

于 2020-03-13T08:45:11.133 回答