Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot: Multiple similar ConfigurationProperties with different Prefixes

Tags:

I'm using Spring Boot and have two very similar services which I'd like to configure in my application.yml.

The configuration looks roughly like this:

serviceA.url=abc.com serviceA.port=80  serviceB.url=def.com serviceB.port=8080 

Is it possible to create one class annotated with @ConfigurationProperties and set the prefix at the injection point?

e.g.

@Component @ConfigurationProperties public class ServiceProperties {    private String url;    private String port;     // Getters & Setters } 

and then in the Services itself:

public class ServiceA {     @Autowired    @SomeFancyAnnotationToSetPrefix(prefix="serviceA")    private ServiceProperties serviceAProperties;     // .... } 

Unfortunately I haven't found something in the documentation about such a feature... Thank you very much for your help!

like image 572
nioe Avatar asked Mar 22 '18 07:03

nioe


People also ask

What is @configurationproperties in Spring Boot?

We use @Configuration so that Spring creates a Spring bean in the application context. We also use @PropertySource to define the location of our properties file. Otherwise, Spring uses the default location (classpath:application.properties). @ConfigurationProperties works best with hierarchical properties that all have the same prefix.

Why do we add a prefix to @configurationproperties?

So let's start by doing that: We use @Configuration so that Spring creates a Spring bean in the application context. @ConfigurationProperties works best with hierarchical properties that all have the same prefix; therefore, we add a prefix of mail.

How to bind all properties starting with prefix “mail” to applicationconfigurationprop class?

Let’s say we want to bind all the properties starting with prefix “mail” to our ApplicationConfigurationProp Class, we can use prefix property on the @ConfigurationProperties annotation. Once we run above application, all properties defined in the property files with prefix “ mail ” will automatically be bind / assigned to this object.

How does spring spring configproperties bind to the hostname?

Spring will automatically bind any property defined in our property file that has the prefix mail and the same name as one of the fields in the ConfigProperties class. Spring uses some relaxed rules for binding properties. As a result, the following variations are all bound to the property hostName:


2 Answers

I achieved almost same thing that you trying. first, register each properties beans.

@Bean @ConfigurationProperties(prefix = "serviceA") public ServiceProperties  serviceAProperties() {     return new ServiceProperties (); }  @Bean @ConfigurationProperties(prefix = "serviceB") public ServiceProperties  serviceBProperties() {     return new ServiceProperties (); } 

and at service(or someplace where will use properties) put a @Qualifier and specified which property would be auto wired .

public class ServiceA {     @Autowired     @Qualifier("serviceAProperties")     private ServiceProperties serviceAProperties;  } 
like image 56
Javvano Avatar answered Sep 28 '22 02:09

Javvano


Following this post Guide to @ConfigurationProperties in Spring Boot you can create a simple class without annotations:

public class ServiceProperties {    private String url;    private String port;     // Getters & Setters } 

And then create the @Configuration class using @Bean annotation:

@Configuration @PropertySource("classpath:name_properties_file.properties") public class ConfigProperties {      @Bean     @ConfigurationProperties(prefix = "serviceA")     public ServiceProperties serviceA() {         return new ServiceProperties ();     }      @Bean     @ConfigurationProperties(prefix = "serviceB")     public ServiceProperties serviceB(){         return new ServiceProperties ();     } } 

Finally you can get the properties as follow:

@SpringBootApplication public class Application implements CommandLineRunner {      @Autowired     private ConfigProperties configProperties ;      private void watheverMethod() {         // For ServiceA properties         System.out.println(configProperties.serviceA().getUrl());          // For ServiceB properties         System.out.println(configProperties.serviceB().getPort());     } } 
like image 33
Jairo Martínez Avatar answered Sep 28 '22 04:09

Jairo Martínez