I have a class of the sort :
@Data
@Component
@ConfigurationProperties("a.config.props")
public class ClientProperties {
private String hostname;
private String port;
private String baseUrl;
private String endpoint;
}
I have tens of property contexts but for 1 line i have to repeat the whole class. Is there a simple and elegant way to have one class and somehow pass the property context dynamically (maybe an array or something of the sort) so that i use the same class.
Yes, we can have more than one spring context files. The following are the 2 ways to configure multiple context files.
Spring @Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.
Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments. You can use properties files, YAML files, environment variables, and command-line arguments to externalize configuration.
Another very simple way to read application properties is to use @Value annotation. Simply annotation the class field with @Value annotation providing the name of the property you want to read from application. properties file and class field variable will be assigned that value.
You can just have a single class that describes your numerous & identical property blocks;
@Data
public class ClientProperties {
private String hostname;
private String port;
private String baseUrl;
private String endpoint;
}
Then refer to it as such to link that single class with different property blocks;
@Configuration
public class PropertyConfigurer {
@Bean
@ConfigurationProperties("props.one")
private ClientProperties propsOne() {
return new ClientProperties();
}
@Bean
@ConfigurationProperties("props.two")
private ClientProperties propsTwo() {
return new ClientProperties();
}
@Bean
@ConfigurationProperties("props.three")
private ClientProperties propsThree() {
return new ClientProperties();
}
}
Then you can access them via their method names as qualifiers;
@Component
public class SomeService {
@Autowired
private ClientProperties propsOne;
@Autowired
private ClientProperties propsTwo;
@Autowired
private ClientProperties propsThree;
... some logic
}
I would suggest to create an abstract class with all the properties and extend the abstract class for as many property variants you have.
@Data
public abstract class BaseClientProperties {
private String hostname;
private String port;
private String baseUrl;
private String endpoint;
}
@Configuration
@ConfigurationProperties("a.config.props1")
public class Client1Properties extends BaseClientProperties{
}
@Configuration
@ConfigurationProperties("a.config.props2")
public class Client2Properties extends BaseClientProperties{
}
Use them as below:
@Service
public class SomeService {
@Autowired
private Client1Properties client1Properties;
@Autowired
private Client2Properties client2Properties;
... service logic
}
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