Search in sources :

Example 1 with CachedCompositePropertySource

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);
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) ConfigUtil(com.ctrip.framework.apollo.util.ConfigUtil) CachedCompositePropertySource(com.ctrip.framework.apollo.spring.config.CachedCompositePropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Test(org.junit.Test)

Example 2 with 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);
}
Also used : CompositePropertySource(org.springframework.core.env.CompositePropertySource) CachedCompositePropertySource(com.ctrip.framework.apollo.spring.config.CachedCompositePropertySource) ConfigUtil(com.ctrip.framework.apollo.util.ConfigUtil) Config(com.ctrip.framework.apollo.Config) CachedCompositePropertySource(com.ctrip.framework.apollo.spring.config.CachedCompositePropertySource)

Aggregations

CachedCompositePropertySource (com.ctrip.framework.apollo.spring.config.CachedCompositePropertySource)2 ConfigUtil (com.ctrip.framework.apollo.util.ConfigUtil)2 Config (com.ctrip.framework.apollo.Config)1 Test (org.junit.Test)1 CompositePropertySource (org.springframework.core.env.CompositePropertySource)1 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)1 MutablePropertySources (org.springframework.core.env.MutablePropertySources)1