I am new to Spring and not sure if there is a simple approach to what I am trying to do. Before I migrated some existing code base to Spring, I was manually loading configuration properties in a singleton class called ConfigurationProvider
- pretty straight forward.
Now that I have migrated to Spring Cloud Config, I am trying to determine a pragmatic approach to a global configuration object with little to no manual setup. My current implementation is a not-so-singleton class that almost accomplishes what I am looking to do but comes with a design flaw.
@Configuration
public class ConfigurationProvider {
private static ConfigurationProvider _instance;
@Autowired
private StorageConfiguration storage;
// this being the design flaw
public ConfigurationProvider() {
_instance = this;
}
public static ConfigurationProvider getInstance() {
return _instance;
}
...
}
I considered throwing an exception if ConfigurationProvider::_instance
is already initialized but this is just tacking onto the existing code smell. With all the bells and whistles of Spring Boot, I'd imagine there is a cleaner approach using one of the hundreds of annotations that is strapped with this framework.