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!
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.
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.
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.
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:
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; }
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()); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With