esha ingle
esha ingle

Reputation: 163

How to read @ConfigurationProperties for different prefixes in single class

I have 2 different application.yaml at github for spring-boot application. Below are the sample content for both :

first application.yaml : application: name: sample-service second application.yaml : common: kafka: topic: test

I created one Configuration class(abc.java) with @ConfigurationProperties( prefix='common.kafka') and another Configuration class(xyz.java) with @ConfigurationProperties( prefix='application')

Here in xyz.java, I am inheriting abc.java. While executing xyz.java, I am not able to access properties of abc.java, getting NPE. It is achieved if I keep same prefix hierarchy i.e. @ConfigurationProperties( prefix='application')

In short, I want to access both application.yaml configurations in single class of spring-boot micro service. However, I am not able to achieve it. Please provide any suggestion to access both properties.

Upvotes: 0

Views: 2354

Answers (1)

Evgeni Atanasov
Evgeni Atanasov

Reputation: 520

Why do you have to inherit one class into the another?

Isn't it better to have new class that has the two @ConfigurationProperties annotated classes as properties.

E.g.

@Component
public class Properties {

    @Autowired
    ApplicationProperties applicationProperties;

    @Auworied
    KafkaProperties kafkaProperties;
}

and just use your Properties.class wherever you need to

Upvotes: 1

Related Questions