Skip to content

Commit 9683e5b

Browse files
committed
simplify spring demo
1 parent 84053f7 commit 9683e5b

File tree

11 files changed

+101
-185
lines changed

11 files changed

+101
-185
lines changed
Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,39 @@
11
package com.ctrip.framework.apollo.demo.spring.common.bean;
22

3-
import com.ctrip.framework.apollo.Config;
4-
import com.ctrip.framework.apollo.model.ConfigChange;
5-
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
6-
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
7-
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
8-
93
import org.slf4j.Logger;
104
import org.slf4j.LoggerFactory;
115
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.cloud.context.config.annotation.RefreshScope;
127
import org.springframework.stereotype.Component;
138

149
import javax.annotation.PostConstruct;
1510

1611
/**
1712
* @author Jason Song([email protected])
1813
*/
19-
@Component
14+
@RefreshScope
15+
@Component("annotatedBean")
2016
public class AnnotatedBean {
2117
private static final Logger logger = LoggerFactory.getLogger(AnnotatedBean.class);
2218

2319
@Value("${timeout:200}")
2420
private int timeout;
2521
private int batch;
2622

27-
@ApolloConfig
28-
private Config config;
29-
@ApolloConfig("FX.apollo")
30-
private Config anotherConfig;
31-
3223
@PostConstruct
3324
void initialize() {
34-
logger.info("timeout is {}", timeout);
35-
logger.info("batch is {}", batch);
36-
37-
logger.info("Keys for config: {}", config.getPropertyNames());
38-
logger.info("Keys for anotherConfig: {}", anotherConfig.getPropertyNames());
25+
logger.info("timeout is initialized as {}", timeout);
26+
logger.info("batch is initialized as {}", batch);
3927
}
4028

4129
@Value("${batch:100}")
4230
public void setBatch(int batch) {
4331
this.batch = batch;
4432
}
4533

46-
@ApolloConfigChangeListener("application")
47-
private void someChangeHandler(ConfigChangeEvent changeEvent) {
48-
logger.info("[someChangeHandler]Changes for namespace {}", changeEvent.getNamespace());
49-
if (changeEvent.isChanged("timeout")) {
50-
refreshTimeout();
51-
}
52-
}
53-
54-
@ApolloConfigChangeListener({"application", "FX.apollo"})
55-
private void anotherChangeHandler(ConfigChangeEvent changeEvent) {
56-
logger.info("[anotherChangeHandler]Changes for namespace {}", changeEvent.getNamespace());
57-
for (String key : changeEvent.changedKeys()) {
58-
ConfigChange change = changeEvent.getChange(key);
59-
logger.info("[anotherChangeHandler]Change - key: {}, oldValue: {}, newValue: {}, changeType: {}",
60-
change.getPropertyName(), change.getOldValue(), change.getNewValue(),
61-
change.getChangeType());
62-
}
63-
}
6434

65-
private void refreshTimeout() {
66-
//do some custom logic to update placeholder value
67-
timeout = config.getIntProperty("timeout", timeout);
68-
logger.info("Refreshing timeout to {}", timeout);
35+
@Override
36+
public String toString() {
37+
return String.format("[AnnotatedBean] timeout: %d, batch: %d", timeout, batch);
6938
}
7039
}

apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/spring/common/bean/NormalBean.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/spring/common/bean/RefreshScopeBean.java

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package com.ctrip.framework.apollo.demo.spring.common.config;
22

3-
import com.ctrip.framework.apollo.demo.spring.common.bean.NormalBean;
43
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
54

6-
import org.springframework.beans.factory.annotation.Value;
7-
import org.springframework.cloud.context.config.annotation.RefreshScope;
8-
import org.springframework.context.annotation.Bean;
95
import org.springframework.context.annotation.Configuration;
106

117
/**
@@ -14,11 +10,4 @@
1410
@Configuration
1511
@EnableApolloConfig(value = "application", order = 10)
1612
public class AppConfig {
17-
@Bean("normalBean")
18-
@RefreshScope
19-
public NormalBean normalBean(@Value("${batch:100}") int batch) {
20-
NormalBean bean = new NormalBean();
21-
bean.setBatch(batch);
22-
return bean;
23-
}
2413
}
Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,37 @@
11
package com.ctrip.framework.apollo.demo.spring.common.refresh;
22

3-
import com.ctrip.framework.apollo.Config;
4-
import com.ctrip.framework.apollo.ConfigChangeListener;
5-
import com.ctrip.framework.apollo.demo.spring.common.bean.NormalBean;
6-
import com.ctrip.framework.apollo.demo.spring.common.bean.RefreshScopeBean;
3+
import com.ctrip.framework.apollo.demo.spring.common.bean.AnnotatedBean;
74
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
8-
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
5+
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
96

107
import org.slf4j.Logger;
118
import org.slf4j.LoggerFactory;
129
import org.springframework.beans.factory.annotation.Autowired;
13-
import org.springframework.beans.factory.annotation.Qualifier;
1410
import org.springframework.cloud.context.scope.refresh.RefreshScope;
1511
import org.springframework.stereotype.Component;
1612

17-
import javax.annotation.PostConstruct;
18-
1913
/**
2014
* To refresh the config bean when config is changed
2115
*
2216
* @author Jason Song([email protected])
2317
*/
2418
@Component
25-
public class ApolloRefreshConfig implements ConfigChangeListener {
19+
public class ApolloRefreshConfig {
2620
private static final Logger logger = LoggerFactory.getLogger(ApolloRefreshConfig.class);
2721

28-
@ApolloConfig
29-
private Config config;
30-
@ApolloConfig("FX.apollo")
31-
private Config anotherConfig;
3222
@Autowired
3323
private RefreshScope refreshScope;
3424

3525
@Autowired
36-
private RefreshScopeBean refreshScopeBean;
37-
38-
@Autowired
39-
private NormalBean normalBean;
40-
41-
@PostConstruct
42-
private void init() {
43-
config.addChangeListener(this);
44-
anotherConfig.addChangeListener(this);
45-
}
46-
47-
@Override
48-
public void onChange(ConfigChangeEvent changeEvent) {
49-
//could also call refreshScope.refreshAll();
50-
logger.info("refreshScopeBean before refresh {}", refreshScopeBean.toString());
51-
//could also call refreshScope.refreshAll();
52-
refreshScope.refresh("refreshScopeBean");
53-
logger.info("refreshScopeBean after refresh {}", refreshScopeBean.toString());
54-
logger.info("normalBean with refresh scope before refresh {}", normalBean.toString());
55-
refreshScope.refresh("normalBean");
56-
logger.info("normalBean with refresh scope after refresh {}", normalBean.toString());
26+
private AnnotatedBean annotatedBean;
27+
28+
@ApolloConfigChangeListener({"application", "FX.apollo"})
29+
private void onChange(ConfigChangeEvent changeEvent) {
30+
if (changeEvent.isChanged("timeout") || changeEvent.isChanged("batch")) {
31+
logger.info("before refresh {}", annotatedBean.toString());
32+
//could also call refreshScope.refreshAll();
33+
refreshScope.refresh("annotatedBean");
34+
logger.info("after refresh {}", annotatedBean.toString());
35+
}
5736
}
5837
}

apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/spring/javaConfigDemo/config/RefreshScopeConfig.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.ctrip.framework.apollo.demo.spring.javaConfigDemo.config;
22

3-
import com.ctrip.framework.apollo.demo.spring.common.refresh.ApolloRefreshConfig;
4-
53
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
6-
import org.springframework.context.annotation.Bean;
74
import org.springframework.context.annotation.Configuration;
85
import org.springframework.context.annotation.Import;
96

apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/spring/springBootDemo/config/SampleRedisConfig.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.ctrip.framework.apollo.demo.spring.springBootDemo.config;
22

3-
import com.ctrip.framework.apollo.ConfigChangeListener;
4-
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
5-
63
import org.slf4j.Logger;
74
import org.slf4j.LoggerFactory;
85
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -25,8 +22,8 @@ public class SampleRedisConfig {
2522
private int commandTimeout;
2623

2724
@PostConstruct
28-
private void init() {
29-
logger.info("ConfigurationProperties sample - expireSeconds: {}, clusterNodes: {}, commandTimeout: {}",
25+
private void initialize() {
26+
logger.info("SampleRedisConfig initialized - expireSeconds: {}, clusterNodes: {}, commandTimeout: {}",
3027
expireSeconds, clusterNodes, commandTimeout);
3128
}
3229

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
package com.ctrip.framework.apollo.demo.spring.springBootDemo.refresh;
22

3-
import com.ctrip.framework.apollo.Config;
4-
import com.ctrip.framework.apollo.ConfigChangeListener;
53
import com.ctrip.framework.apollo.demo.spring.common.refresh.ApolloRefreshConfig;
64
import com.ctrip.framework.apollo.demo.spring.springBootDemo.config.SampleRedisConfig;
75
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
8-
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
6+
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
97

108
import org.slf4j.Logger;
119
import org.slf4j.LoggerFactory;
1210
import org.springframework.beans.factory.annotation.Autowired;
1311
import org.springframework.cloud.context.scope.refresh.RefreshScope;
1412
import org.springframework.stereotype.Component;
1513

16-
import javax.annotation.PostConstruct;
17-
1814
/**
1915
* @author Jason Song([email protected])
2016
*/
2117
@Component
22-
public class SpringBootApolloRefreshConfig implements ConfigChangeListener {
18+
public class SpringBootApolloRefreshConfig {
2319
private static final Logger logger = LoggerFactory.getLogger(SpringBootApolloRefreshConfig.class);
2420

2521
@Autowired
@@ -28,21 +24,13 @@ public class SpringBootApolloRefreshConfig implements ConfigChangeListener {
2824
@Autowired
2925
private SampleRedisConfig sampleRedisConfig;
3026

31-
@ApolloConfig
32-
private Config config;
33-
3427
@Autowired
3528
private RefreshScope refreshScope;
3629

37-
@PostConstruct
38-
private void init() {
39-
config.addChangeListener(this);
40-
}
41-
42-
@Override
30+
@ApolloConfigChangeListener
4331
public void onChange(ConfigChangeEvent changeEvent) {
44-
logger.info("sampleRedisConfig before refresh {}", sampleRedisConfig.toString());
32+
logger.info("before refresh {}", sampleRedisConfig.toString());
4533
refreshScope.refresh("sampleRedisConfig");
46-
logger.info("sampleRedisConfig after refresh {}", sampleRedisConfig.toString());
34+
logger.info("after refresh {}", sampleRedisConfig.toString());
4735
}
4836
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.ctrip.framework.apollo.demo.spring.xmlConfigDemo.bean;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
/**
7+
* @author Jason Song([email protected])
8+
*/
9+
public class XmlBean {
10+
private static final Logger logger = LoggerFactory.getLogger(XmlBean.class);
11+
12+
private int timeout;
13+
private int batch;
14+
15+
public void setTimeout(int timeout) {
16+
logger.info("updating timeout, old value: {}, new value: {}", this.timeout, timeout);
17+
this.timeout = timeout;
18+
}
19+
20+
public void setBatch(int batch) {
21+
logger.info("updating batch, old value: {}, new value: {}", this.batch, batch);
22+
this.batch = batch;
23+
}
24+
25+
public int getTimeout() {
26+
return timeout;
27+
}
28+
29+
public int getBatch() {
30+
return batch;
31+
}
32+
}

0 commit comments

Comments
 (0)