Commit 9683e5b7 by nobodyiam

simplify spring demo

parent 84053f70
package com.ctrip.framework.apollo.demo.spring.common.bean;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
......@@ -16,7 +11,8 @@ import javax.annotation.PostConstruct;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@Component
@RefreshScope
@Component("annotatedBean")
public class AnnotatedBean {
private static final Logger logger = LoggerFactory.getLogger(AnnotatedBean.class);
......@@ -24,18 +20,10 @@ public class AnnotatedBean {
private int timeout;
private int batch;
@ApolloConfig
private Config config;
@ApolloConfig("FX.apollo")
private Config anotherConfig;
@PostConstruct
void initialize() {
logger.info("timeout is {}", timeout);
logger.info("batch is {}", batch);
logger.info("Keys for config: {}", config.getPropertyNames());
logger.info("Keys for anotherConfig: {}", anotherConfig.getPropertyNames());
logger.info("timeout is initialized as {}", timeout);
logger.info("batch is initialized as {}", batch);
}
@Value("${batch:100}")
......@@ -43,28 +31,9 @@ public class AnnotatedBean {
this.batch = batch;
}
@ApolloConfigChangeListener("application")
private void someChangeHandler(ConfigChangeEvent changeEvent) {
logger.info("[someChangeHandler]Changes for namespace {}", changeEvent.getNamespace());
if (changeEvent.isChanged("timeout")) {
refreshTimeout();
}
}
@ApolloConfigChangeListener({"application", "FX.apollo"})
private void anotherChangeHandler(ConfigChangeEvent changeEvent) {
logger.info("[anotherChangeHandler]Changes for namespace {}", changeEvent.getNamespace());
for (String key : changeEvent.changedKeys()) {
ConfigChange change = changeEvent.getChange(key);
logger.info("[anotherChangeHandler]Change - key: {}, oldValue: {}, newValue: {}, changeType: {}",
change.getPropertyName(), change.getOldValue(), change.getNewValue(),
change.getChangeType());
}
}
private void refreshTimeout() {
//do some custom logic to update placeholder value
timeout = config.getIntProperty("timeout", timeout);
logger.info("Refreshing timeout to {}", timeout);
@Override
public String toString() {
return String.format("[AnnotatedBean] timeout: %d, batch: %d", timeout, batch);
}
}
package com.ctrip.framework.apollo.demo.spring.common.bean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* This bean is annotated with @RefreshScope so that it can be 'refreshed' at runtime
* @author Jason Song(song_s@ctrip.com)
*/
@RefreshScope
@Component("refreshScopeBean")
public class RefreshScopeBean {
private static final Logger logger = LoggerFactory.getLogger(RefreshScopeBean.class);
@Value("${timeout:200}")
private int timeout;
@Value("${batch:100}")
private int batch;
/**
* this method will be called when the bean is first initialized, and when it is 'refreshed' as well
*/
@PostConstruct
void initialize() {
logger.info("timeout is {}", timeout);
logger.info("batch is {}", batch);
}
@Override
public String toString() {
return String.format("[RefreshScopeBean] timeout: %d, batch: %d", timeout, batch);
}
}
package com.ctrip.framework.apollo.demo.spring.common.config;
import com.ctrip.framework.apollo.demo.spring.common.bean.NormalBean;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
......@@ -14,11 +10,4 @@ import org.springframework.context.annotation.Configuration;
@Configuration
@EnableApolloConfig(value = "application", order = 10)
public class AppConfig {
@Bean("normalBean")
@RefreshScope
public NormalBean normalBean(@Value("${batch:100}") int batch) {
NormalBean bean = new NormalBean();
bean.setBatch(batch);
return bean;
}
}
package com.ctrip.framework.apollo.demo.spring.common.refresh;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.demo.spring.common.bean.NormalBean;
import com.ctrip.framework.apollo.demo.spring.common.bean.RefreshScopeBean;
import com.ctrip.framework.apollo.demo.spring.common.bean.AnnotatedBean;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* To refresh the config bean when config is changed
*
* @author Jason Song(song_s@ctrip.com)
*/
@Component
public class ApolloRefreshConfig implements ConfigChangeListener {
public class ApolloRefreshConfig {
private static final Logger logger = LoggerFactory.getLogger(ApolloRefreshConfig.class);
@ApolloConfig
private Config config;
@ApolloConfig("FX.apollo")
private Config anotherConfig;
@Autowired
private RefreshScope refreshScope;
@Autowired
private RefreshScopeBean refreshScopeBean;
@Autowired
private NormalBean normalBean;
private AnnotatedBean annotatedBean;
@PostConstruct
private void init() {
config.addChangeListener(this);
anotherConfig.addChangeListener(this);
}
@Override
public void onChange(ConfigChangeEvent changeEvent) {
//could also call refreshScope.refreshAll();
logger.info("refreshScopeBean before refresh {}", refreshScopeBean.toString());
@ApolloConfigChangeListener({"application", "FX.apollo"})
private void onChange(ConfigChangeEvent changeEvent) {
if (changeEvent.isChanged("timeout") || changeEvent.isChanged("batch")) {
logger.info("before refresh {}", annotatedBean.toString());
//could also call refreshScope.refreshAll();
refreshScope.refresh("refreshScopeBean");
logger.info("refreshScopeBean after refresh {}", refreshScopeBean.toString());
logger.info("normalBean with refresh scope before refresh {}", normalBean.toString());
refreshScope.refresh("normalBean");
logger.info("normalBean with refresh scope after refresh {}", normalBean.toString());
refreshScope.refresh("annotatedBean");
logger.info("after refresh {}", annotatedBean.toString());
}
}
}
package com.ctrip.framework.apollo.demo.spring.javaConfigDemo.config;
import com.ctrip.framework.apollo.demo.spring.common.refresh.ApolloRefreshConfig;
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
......
package com.ctrip.framework.apollo.demo.spring.springBootDemo.config;
import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
......@@ -25,8 +22,8 @@ public class SampleRedisConfig {
private int commandTimeout;
@PostConstruct
private void init() {
logger.info("ConfigurationProperties sample - expireSeconds: {}, clusterNodes: {}, commandTimeout: {}",
private void initialize() {
logger.info("SampleRedisConfig initialized - expireSeconds: {}, clusterNodes: {}, commandTimeout: {}",
expireSeconds, clusterNodes, commandTimeout);
}
......
package com.ctrip.framework.apollo.demo.spring.springBootDemo.refresh;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.demo.spring.common.refresh.ApolloRefreshConfig;
import com.ctrip.framework.apollo.demo.spring.springBootDemo.config.SampleRedisConfig;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -13,13 +11,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@Component
public class SpringBootApolloRefreshConfig implements ConfigChangeListener {
public class SpringBootApolloRefreshConfig {
private static final Logger logger = LoggerFactory.getLogger(SpringBootApolloRefreshConfig.class);
@Autowired
......@@ -28,21 +24,13 @@ public class SpringBootApolloRefreshConfig implements ConfigChangeListener {
@Autowired
private SampleRedisConfig sampleRedisConfig;
@ApolloConfig
private Config config;
@Autowired
private RefreshScope refreshScope;
@PostConstruct
private void init() {
config.addChangeListener(this);
}
@Override
@ApolloConfigChangeListener
public void onChange(ConfigChangeEvent changeEvent) {
logger.info("sampleRedisConfig before refresh {}", sampleRedisConfig.toString());
logger.info("before refresh {}", sampleRedisConfig.toString());
refreshScope.refresh("sampleRedisConfig");
logger.info("sampleRedisConfig after refresh {}", sampleRedisConfig.toString());
logger.info("after refresh {}", sampleRedisConfig.toString());
}
}
package com.ctrip.framework.apollo.demo.spring.common.bean;
package com.ctrip.framework.apollo.demo.spring.xmlConfigDemo.bean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import javax.annotation.PostConstruct;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public class NormalBean {
private static final Logger logger = LoggerFactory.getLogger(NormalBean.class);
public class XmlBean {
private static final Logger logger = LoggerFactory.getLogger(XmlBean.class);
@Value("${timeout:200}")
private int timeout;
private int batch;
@PostConstruct
void initialize() {
logger.info("timeout is {}", timeout);
logger.info("batch is {}", batch);
public void setTimeout(int timeout) {
logger.info("updating timeout, old value: {}, new value: {}", this.timeout, timeout);
this.timeout = timeout;
}
public void setBatch(int batch) {
logger.info("updating batch, old value: {}, new value: {}", this.batch, batch);
this.batch = batch;
}
@Override
public String toString() {
return String.format("[NormalBean] timeout: %d, batch: %d", timeout, batch);
public int getTimeout() {
return timeout;
}
public int getBatch() {
return batch;
}
}
package com.ctrip.framework.apollo.demo.spring.xmlConfigDemo.refresh;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.demo.spring.xmlConfigDemo.bean.XmlBean;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public class ManualRefreshUtil {
private static final Logger logger = LoggerFactory.getLogger(ManualRefreshUtil.class);
@ApolloConfig
private Config config;
@Autowired
private XmlBean xmlBean;
@ApolloConfigChangeListener
private void onChange(ConfigChangeEvent changeEvent) {
if (changeEvent.isChanged("timeout")) {
logger.info("Manually refreshing xmlBean.timeout");
xmlBean.setTimeout(config.getIntProperty("timeout", xmlBean.getTimeout()));
}
if (changeEvent.isChanged("batch")) {
logger.info("Manually refreshing xmlBean.batch");
xmlBean.setBatch(config.getIntProperty("batch", xmlBean.getBatch()));
}
}
}
......@@ -2,21 +2,20 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:apollo="http://www.ctrip.com/schema/apollo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.ctrip.com/schema/apollo http://www.ctrip.com/schema/apollo.xsd">
<apollo:config order="10"/>
<apollo:config namespaces="FX.apollo" order="11"/>
<bean name="normalBean" id="normalBean" class="com.ctrip.framework.apollo.demo.spring.common.bean.NormalBean"
scope="refresh">
<aop:scoped-proxy proxy-target-class="true"/>
<bean class="com.ctrip.framework.apollo.demo.spring.xmlConfigDemo.bean.XmlBean">
<property name="timeout" value="${timeout:200}"/>
<property name="batch" value="${batch:100}"/>
</bean>
<bean class="com.ctrip.framework.apollo.demo.spring.xmlConfigDemo.refresh.ManualRefreshUtil"/>
<!-- to support RefreshScope -->
<bean class="org.springframework.cloud.autoconfigure.RefreshAutoConfiguration"/>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment