Commit 2650b3c0 by Jason Song Committed by GitHub

Merge pull request #781 from nobodyiam/more-configuration-properties-sample

add more ConfigurationProperties sample
parents 395dcb87 6d2d4596
package com.ctrip.framework.apollo.demo.spring.springBootDemo.config; package com.ctrip.framework.apollo.demo.spring.springBootDemo.config;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
...@@ -9,22 +13,38 @@ import org.springframework.stereotype.Component; ...@@ -9,22 +13,38 @@ import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
/** /**
* You may set up data like the following in Apollo:
* <pre>
* redis.cache.expireSeconds = 100
* redis.cache.clusterNodes = 1,2
* redis.cache.commandTimeout = 50
* redis.cache.someMap.key1 = a
* redis.cache.someMap.key2 = b
* redis.cache.someList[0] = c
* redis.cache.someList[1] = d
* </pre>
*
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
@ConfigurationProperties(prefix = "redis.cache") @ConfigurationProperties(prefix = "redis.cache")
@Component("sampleRedisConfig") @Component("sampleRedisConfig")
@RefreshScope @RefreshScope
public class SampleRedisConfig { public class SampleRedisConfig {
private static final Logger logger = LoggerFactory.getLogger(SampleRedisConfig.class); private static final Logger logger = LoggerFactory.getLogger(SampleRedisConfig.class);
private int expireSeconds; private int expireSeconds;
private String clusterNodes; private String clusterNodes;
private int commandTimeout; private int commandTimeout;
private Map<String, String> someMap = Maps.newLinkedHashMap();
private List<String> someList = Lists.newLinkedList();
@PostConstruct @PostConstruct
private void initialize() { private void initialize() {
logger.info("SampleRedisConfig initialized - expireSeconds: {}, clusterNodes: {}, commandTimeout: {}", logger.info(
expireSeconds, clusterNodes, commandTimeout); "SampleRedisConfig initialized - expireSeconds: {}, clusterNodes: {}, commandTimeout: {}, someMap: {}, someList: {}",
expireSeconds, clusterNodes, commandTimeout, someMap, someList);
} }
public void setExpireSeconds(int expireSeconds) { public void setExpireSeconds(int expireSeconds) {
...@@ -39,9 +59,18 @@ public class SampleRedisConfig { ...@@ -39,9 +59,18 @@ public class SampleRedisConfig {
this.commandTimeout = commandTimeout; this.commandTimeout = commandTimeout;
} }
public Map<String, String> getSomeMap() {
return someMap;
}
public List<String> getSomeList() {
return someList;
}
@Override @Override
public String toString() { public String toString() {
return String.format("[SampleRedisConfig] expireSeconds: %d, clusterNodes: %s, commandTimeout: %d", return String.format(
expireSeconds, clusterNodes, commandTimeout); "[SampleRedisConfig] expireSeconds: %d, clusterNodes: %s, commandTimeout: %d, someMap: %s, someList: %s",
expireSeconds, clusterNodes, commandTimeout, someMap, someList);
} }
} }
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