读取默认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);
在application.properties/bootstrap.properties中按照如下样例配置即可
apollo.bootstrap.namespaces = application,FX.apollo
配置中增加:apollo.bootstrap.enabled = true 可保证启动就执行
@Configuration
@EnableApolloConfig(order = 2)
public class SomeAppConfig {
@Bean
public TestJavaConfigBean javaConfigBean() {
return new TestJavaConfigBean();
}
}
@Configuration
@EnableApolloConfig(value = {"FX.apollo", "FX.soa"}, order = 1)
public class AnotherAppConfig {}
//后续在具体的类中可直接使用------TestJavaConfigBean
public class TestJavaConfigBean {
@Value("{batch:200}")
public void setBatch(int batch) {
this.batch = batch;
}
public int getTimeout() {
return timeout;
}
public int getBatch() {
return batch;
}
}
@ConfigurationProperties(prefix = "redis.cache")
public class SampleRedisConfig {
private int expireSeconds;
private int commandTimeout;
public void setExpireSeconds(int expireSeconds) {
this.expireSeconds = expireSeconds;
}
public void setCommandTimeout(int commandTimeout) {
this.commandTimeout = commandTimeout;
}
}
1.@ApolloConfig 用来自动注入Config对象
2.@ApolloConfigChangeListener 用来自动注册ConfigChangeListener
3.@ApolloJsonValue 用来把配置的json字符串自动注入为对象
- @Configuration
- @EnableApolloConfig
- public class AppConfig {
- @Bean
- public TestApolloAnnotationBean testApolloAnnotationBean() {
- return new TestApolloAnnotationBean();
- }
- }
- public class TestApolloAnnotationBean {
- @ApolloConfig
- private Config config; //inject config for namespace application
- @ApolloConfig("application")
- private Config anotherConfig; //inject config for namespace application
- @ApolloConfig("FX.apollo")
- private Config yetAnotherConfig; //inject config for namespace FX.apollo
- /**
- * ApolloJsonValue annotated on fields example, the default value is specified as empty list - []
- * <br />
- * jsonBeanProperty=[{"someString":"hello","someInt":100},{"someString":"world!","someInt":200}]
- */
- @ApolloJsonValue("${jsonBeanProperty:[]}")
- private List<JsonBean> anotherJsonBeans;
-
- @Value("${batch:100}")
- private int batch;
-
- //config change listener for namespace application
- @ApolloConfigChangeListener
- private void someOnChange(ConfigChangeEvent changeEvent) {
- //update injected value of batch if it is changed in Apollo
- if (changeEvent.isChanged("batch")) {
- batch = config.getIntProperty("batch", 100);
- }
- }
- //config change listener for namespace application
- @ApolloConfigChangeListener("application")
- private void anotherOnChange(ConfigChangeEvent changeEvent) {
- //do something
- }
- //config change listener for namespaces application and FX.apollo
- @ApolloConfigChangeListener({"application", "FX.apollo"})
- private void yetAnotherOnChange(ConfigChangeEvent changeEvent) {
- //do something
- }
-
- //example of getting config from Apollo directly
- //this will always return the latest value of timeout
- public int getTimeout() {
- return config.getIntProperty("timeout", 200);
- }
-
- //example of getting config from injected value
- //the program needs to update the injected value when batch is changed in Apollo using @ApolloConfigChangeListener shown above
- public int getBatch() {
- return this.batch;
- }
-
- private static class JsonBean{
- private String someString;
- private int someInt;
- }
- }
-
如果之前有使用org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的,请替换成org.springframework.context.support.PropertySourcesPlaceholderConfigurer。Spring 3.1以后就不建议使用PropertyPlaceholderConfigurer了,要改用PropertySourcesPlaceholderConfigurer。
- <?xml version="1.0" encoding="UTF-8"?>
-
- <beans xmlns="[<u>http://www.springframework.org/schema/beans</u>](http://www.springframework.org/schema/beans)"
-
- xmlns:xsi="[<u>http://www.w3.org/2001/XMLSchema-instance</u>](http://www.w3.org/2001/XMLSchema-instance)"
-
- xmlns:apollo="[<u>http://www.ctrip.com/schema/apollo</u>](http://www.ctrip.com/schema/apollo)"
-
- xsi:schemaLocation="[<u>http://www.springframework.org/schema/beans</u>](http://www.springframework.org/schema/beans) [<u>http://www.springframework.org/schema/beans/spring-beans.xsd</u>](http://www.springframework.org/schema/beans/spring-beans.xsd)
- [<u>http://www.ctrip.com/schema/apollo</u>](http://www.ctrip.com/schema/apollo) [<u>http://www.ctrip.com/schema/apollo.xsd</u>](http://www.ctrip.com/schema/apollo.xsd)">
-
- <apollo:config order="2"/><!—-启用默认的namespaces-->
-
- <!-- 这个是最复杂的配置形式,指示Apollo注入FX.apollo和FX.soa namespace的配置到Spring环境中,并且顺序在application前面 -->
-
- <apollo:config namespaces="FX.apollo,FX.soa" order="1"/>
-
- <bean class="com.ctrip.framework.apollo.spring.TestXmlBean">
-
- <property name="timeout" value="${timeout:100}"/>
-
- <property name="batch" value="${batch:200}"/>
-
- </bean>
-
- </beans>