Search in sources :

Example 1 with ConfigPropertySourceFactory

use of com.ctrip.framework.apollo.spring.config.ConfigPropertySourceFactory 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)1 ConfigPropertySource (com.ctrip.framework.apollo.spring.config.ConfigPropertySource)1 ConfigPropertySourceFactory (com.ctrip.framework.apollo.spring.config.ConfigPropertySourceFactory)1 ArrayList (java.util.ArrayList)1 ConfigurableBootstrapContext (org.springframework.boot.ConfigurableBootstrapContext)1 ConfigData (org.springframework.boot.context.config.ConfigData)1 BindHandler (org.springframework.boot.context.properties.bind.BindHandler)1 Binder (org.springframework.boot.context.properties.bind.Binder)1 PropertySource (org.springframework.core.env.PropertySource)1