use of com.ctrip.framework.apollo.spring.config.CachedCompositePropertySource in project apollo by ctripcorp.
the class ApolloApplicationContextInitializerTest method testPropertyNamesCacheEnabled.
@Test
public void testPropertyNamesCacheEnabled() {
ConfigurableEnvironment environment = mock(ConfigurableEnvironment.class);
MutablePropertySources propertySources = new MutablePropertySources();
when(environment.getPropertySources()).thenReturn(propertySources);
when(environment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_NAMESPACES, ConfigConsts.NAMESPACE_APPLICATION)).thenReturn("");
apolloApplicationContextInitializer.initialize(environment);
assertTrue(propertySources.contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME));
assertFalse(propertySources.iterator().next() instanceof CachedCompositePropertySource);
ConfigUtil configUtil = new ConfigUtil();
configUtil = spy(configUtil);
when(configUtil.isPropertyNamesCacheEnabled()).thenReturn(true);
MockInjector.setInstance(ConfigUtil.class, configUtil);
apolloApplicationContextInitializer = new ApolloApplicationContextInitializer();
propertySources.remove(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME);
apolloApplicationContextInitializer.initialize(environment);
assertTrue(propertySources.contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME));
assertTrue(propertySources.iterator().next() instanceof CachedCompositePropertySource);
}
use of com.ctrip.framework.apollo.spring.config.CachedCompositePropertySource in project apollo by ctripcorp.
the class ApolloApplicationContextInitializer method initialize.
/**
* Initialize Apollo Configurations Just after environment is ready.
*
* @param environment
*/
protected void initialize(ConfigurableEnvironment environment) {
if (environment.getPropertySources().contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
// already initialized, replay the logs that were printed before the logging system was initialized
DeferredLogger.replayTo();
return;
}
String namespaces = environment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_NAMESPACES, ConfigConsts.NAMESPACE_APPLICATION);
logger.debug("Apollo bootstrap namespaces: {}", namespaces);
List<String> namespaceList = NAMESPACE_SPLITTER.splitToList(namespaces);
CompositePropertySource composite;
final ConfigUtil configUtil = ApolloInjector.getInstance(ConfigUtil.class);
if (configUtil.isPropertyNamesCacheEnabled()) {
composite = new CachedCompositePropertySource(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME);
} else {
composite = new CompositePropertySource(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME);
}
for (String namespace : namespaceList) {
Config config = ConfigService.getConfig(namespace);
composite.addPropertySource(configPropertySourceFactory.getConfigPropertySource(namespace, config));
}
environment.getPropertySources().addFirst(composite);
}
Aggregations