1.API方式直接识别
读取默认namespace
Config config = ConfigService.getAppConfig();
String someKey = "someKeyFromDefaultNamespace";
String someDefaultValue = "someDefaultValueForTheKey";
String value = config.getProperty(someKey, someDefaultValue);
读取指定namespace
String somePublicNamespace = "CAT";
Config config = ConfigService.getConfig(somePublicNamespace); //config instance is singleton for each namespace and is never null
String someKey = "someKeyFromPublicNamespace";
String someDefaultValue = "someDefaultValueForTheKey";
String value = config.getProperty(someKey, someDefaultValue);
2.基于java注解的几种方式
在application.properties/bootstrap.properties中按照如下样例配置即可
apollo.bootstrap.namespaces = application,FX.apollo
配置中增加:apollo.bootstrap.enabled = true 可保证启动就执行
@Configuration
@EnableApolloConf

本文介绍了Apollo配置信息的四种识别和注入方式:API直接读取、基于Java注解、基于Apollo注解以及Spring的XML格式整合。通过API可读取默认和指定namespace的配置;Java注解允许在启动时自动执行配置;Apollo注解提供自动注入Config对象和监听配置变化的功能;而Spring XML整合需要将PropertyPlaceholderConfigurer替换为PropertySourcesPlaceholderConfigurer。
最低0.47元/天 解锁文章
6887

被折叠的 条评论
为什么被折叠?



