Search in sources :

Example 1 with Config

use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.

the class JavaConfigPlaceholderTest method testMultiplePropertySourcesWithSameProperties.

@Test
public void testMultiplePropertySourcesWithSameProperties() throws Exception {
    int someTimeout = 1000;
    int anotherTimeout = someTimeout + 1;
    int someBatch = 2000;
    Config application = mock(Config.class);
    when(application.getProperty(eq(TIMEOUT_PROPERTY), anyString())).thenReturn(String.valueOf(someTimeout));
    when(application.getProperty(eq(BATCH_PROPERTY), anyString())).thenReturn(String.valueOf(someBatch));
    mockConfig(ConfigConsts.NAMESPACE_APPLICATION, application);
    Config fxApollo = mock(Config.class);
    when(fxApollo.getProperty(eq(TIMEOUT_PROPERTY), anyString())).thenReturn(String.valueOf(anotherTimeout));
    mockConfig(FX_APOLLO_NAMESPACE, fxApollo);
    check(someTimeout, someBatch, AppConfig3.class);
}
Also used : EnableApolloConfig(com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig) Config(com.ctrip.framework.apollo.Config) Test(org.junit.Test)

Example 2 with Config

use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.

the class XmlConfigPlaceholderTest method testMultiplePropertySourcesWithSameProperties2.

@Test
public void testMultiplePropertySourcesWithSameProperties2() throws Exception {
    int someTimeout = 1000;
    int anotherTimeout = someTimeout + 1;
    int someBatch = 2000;
    Config application = mock(Config.class);
    when(application.getProperty(eq(TIMEOUT_PROPERTY), anyString())).thenReturn(String.valueOf(someTimeout));
    when(application.getProperty(eq(BATCH_PROPERTY), anyString())).thenReturn(String.valueOf(someBatch));
    mockConfig(ConfigConsts.NAMESPACE_APPLICATION, application);
    Config fxApollo = mock(Config.class);
    when(fxApollo.getProperty(eq(TIMEOUT_PROPERTY), anyString())).thenReturn(String.valueOf(anotherTimeout));
    mockConfig(FX_APOLLO_NAMESPACE, fxApollo);
    check("spring/XmlConfigPlaceholderTest6.xml", anotherTimeout, someBatch);
}
Also used : Config(com.ctrip.framework.apollo.Config) Test(org.junit.Test)

Example 3 with Config

use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.

the class ApolloAnnotationProcessor method processField.

@Override
protected void processField(Object bean, String beanName, Field field) {
    ApolloConfig annotation = AnnotationUtils.getAnnotation(field, ApolloConfig.class);
    if (annotation == null) {
        return;
    }
    Preconditions.checkArgument(Config.class.isAssignableFrom(field.getType()), "Invalid type: %s for field: %s, should be Config", field.getType(), field);
    String namespace = annotation.value();
    Config config = ConfigService.getConfig(namespace);
    ReflectionUtils.makeAccessible(field);
    ReflectionUtils.setField(field, bean, config);
}
Also used : Config(com.ctrip.framework.apollo.Config)

Example 4 with Config

use of com.ctrip.framework.apollo.Config in project nutzboot by nutzam.

the class ApolloConfigureChangeStarter method start.

public void start() throws Exception {
    Config config = ConfigService.getAppConfig();
    appContext.getBeans(ConfigChangeListener.class).forEach((listener) -> config.addChangeListener(listener));
}
Also used : ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) Config(com.ctrip.framework.apollo.Config)

Example 5 with Config

use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.

the class ApolloConfigDataLoader method load.

@Override
public ConfigData load(ConfigDataLoaderContext context, ApolloConfigDataResource resource) throws IOException, ConfigDataResourceNotFoundException {
    ConfigurableBootstrapContext bootstrapContext = context.getBootstrapContext();
    Binder binder = bootstrapContext.get(Binder.class);
    BindHandler bindHandler = this.getBindHandler(context);
    bootstrapContext.registerIfAbsent(ApolloConfigDataLoaderInitializer.class, InstanceSupplier.from(() -> new ApolloConfigDataLoaderInitializer(this.log, binder, bindHandler, bootstrapContext)));
    ApolloConfigDataLoaderInitializer apolloConfigDataLoaderInitializer = bootstrapContext.get(ApolloConfigDataLoaderInitializer.class);
    // init apollo client
    List<PropertySource<?>> initialPropertySourceList = apolloConfigDataLoaderInitializer.initApolloClient();
    // load config
    bootstrapContext.registerIfAbsent(ConfigPropertySourceFactory.class, InstanceSupplier.from(() -> SpringInjector.getInstance(ConfigPropertySourceFactory.class)));
    ConfigPropertySourceFactory configPropertySourceFactory = bootstrapContext.get(ConfigPropertySourceFactory.class);
    String namespace = resource.getNamespace();
    Config config = ConfigService.getConfig(namespace);
    ConfigPropertySource configPropertySource = configPropertySourceFactory.getConfigPropertySource(namespace, config);
    List<PropertySource<?>> propertySourceList = new ArrayList<>();
    propertySourceList.add(configPropertySource);
    propertySourceList.addAll(initialPropertySourceList);
    log.debug(Slf4jLogMessageFormatter.format("apollo client loaded namespace [{}]", namespace));
    return new ConfigData(propertySourceList);
}
Also used : Binder(org.springframework.boot.context.properties.bind.Binder) Config(com.ctrip.framework.apollo.Config) ConfigPropertySource(com.ctrip.framework.apollo.spring.config.ConfigPropertySource) ConfigData(org.springframework.boot.context.config.ConfigData) ArrayList(java.util.ArrayList) ConfigPropertySourceFactory(com.ctrip.framework.apollo.spring.config.ConfigPropertySourceFactory) BindHandler(org.springframework.boot.context.properties.bind.BindHandler) ConfigurableBootstrapContext(org.springframework.boot.ConfigurableBootstrapContext) PropertySource(org.springframework.core.env.PropertySource) ConfigPropertySource(com.ctrip.framework.apollo.spring.config.ConfigPropertySource)

Aggregations

Config (com.ctrip.framework.apollo.Config)94 Test (org.junit.Test)79 EnableApolloConfig (com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig)39 Matchers.anyString (org.mockito.Matchers.anyString)31 ApolloConfig (com.ctrip.framework.apollo.spring.annotation.ApolloConfig)26 ConfigChangeListener (com.ctrip.framework.apollo.ConfigChangeListener)22 SimpleConfig (com.ctrip.framework.apollo.internals.SimpleConfig)20 ConfigChangeEvent (com.ctrip.framework.apollo.model.ConfigChangeEvent)16 BaseIntegrationTest (com.ctrip.framework.apollo.BaseIntegrationTest)13 ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)13 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)13 ApolloConfigChangeListener (com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener)10 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)10 Properties (java.util.Properties)6 ConfigFactory (com.ctrip.framework.apollo.spi.ConfigFactory)5 PureApolloConfigFactory (com.ctrip.framework.apollo.config.data.internals.PureApolloConfigFactory)4 DefaultConfigFactory (com.ctrip.framework.apollo.spi.DefaultConfigFactory)4 OrderedProperties (com.ctrip.framework.apollo.util.OrderedProperties)4 Set (java.util.Set)4 ApolloConfigNotification (com.ctrip.framework.apollo.core.dto.ApolloConfigNotification)3