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:
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());
}
}