Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using dependency injection for injecting configuration.
import org.cfg4j.provider.ConfigurationProvider;
import org.cfg4j.provider.ConfigurationProviderBuilder;
import org.cfg4j.source.ConfigurationSource;
import org.cfg4j.source.consul.ConsulConfigurationSourceBuilder;
import org.cfg4j.source.context.environment.ImmutableEnvironment;
import org.cfg4j.source.reload.strategy.PeriodicalReloadStrategy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.net.MalformedURLException;
import java.util.concurrent.TimeUnit;
@Configuration
class Cfg4jBeansConfig {
@Value("${configuration.endpoint.protocol:http}")
private String configurationEndpointProtocol;
@Value("${configuration.endpoint.hostname:youForgotToSpecifyHost}")
private String configurationEndpoint;
@Value("${configuration.endpoint.port:8500}")
private int configurationEndpointPort;
@Bean
ConfigurationSource globalConfigurationSource() throws MalformedURLException {
return new ConsulConfigurationSourceBuilder()
.withHost(configurationEndpoint)
.withPort(configurationEndpointPort)
.build();
}
@Bean
ConfigurationProvider configurationProvider(ConfigurationSource configurationSource,
@Value("${configuration.environment}") String configurationEnvironment) {
return new ConfigurationProviderBuilder()
.withConfigurationSource(configurationSource)
.withEnvironment(new ImmutableEnvironment("app/" + configurationEnvironment + "/feature"))
.withReloadStrategy(new PeriodicalReloadStrategy(30, TimeUnit.SECONDS))
.build();
}
public interface SearchConfig {
String defaultQuery();
}
@Bean
SearchConfig searchConfig(ConfigurationProvider configurationProvider) {
return configurationProvider.bind("search", SearchConfig.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.