user13040636
user13040636

Reputation:

How map properties file using @Configuration properites prefix with dotted properties in Spring?

I have a group of properties as follow:

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

... And I want to map in a class like this by using @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;

}

... How can I do?

Upvotes: 0

Views: 1310

Answers (2)

Babyburger
Babyburger

Reputation: 1830

The dotted properties denote separate objects. So if you have

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

Then your config class can look like this:

@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
    }
}

Have a look here: https://www.baeldung.com/configuration-properties-in-spring-boot

Upvotes: 1

NimbleNavigator
NimbleNavigator

Reputation: 138

If you are using Spring Boot, then you need to annotate your main application class with, in your case:

@EnableConfigurationProperties(value = EdwhKafkaProducerConfig.class)

You may also need to define public accessor methods for your configuration properties.

Upvotes: 0

Related Questions