Commit ad241be2 by 张乐 Committed by GitHub

Merge pull request #570 from nobodyiam/spring-boot-demo

add spring boot configuration properties example
parents aefdea8c 7e10b67c
......@@ -13,8 +13,7 @@
<properties>
<java.version>1.7</java.version>
<github.path>${project.artifactId}</github.path>
<!-- apollo spring integration requires Spring 3.1.1+ -->
<spring-demo.version>3.1.1.RELEASE</spring-demo.version>
<spring-demo.version>4.3.6.RELEASE</spring-demo.version>
</properties>
<dependencyManagement>
<dependencies>
......@@ -72,6 +71,21 @@
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<!-- for spring boot demo -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- required for spring 3.1.0 -->
<dependency>
<groupId>cglib</groupId>
......
package com.ctrip.framework.apollo.demo.spring;
import com.ctrip.framework.apollo.demo.spring.config.SampleRedisConfig;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import java.util.Scanner;
/**
* @author Jason Song
*/
@SpringBootApplication
public class SpringBootSampleApplication {
@Bean
public SampleRedisConfig sampleRedisConfig() {
return new SampleRedisConfig();
}
public static void main(String[] args) {
new SpringApplicationBuilder(SpringBootSampleApplication.class).run(args);
onKeyExit();
}
private static void onKeyExit() {
System.out.println("Press Enter to exit...");
new Scanner(System.in).nextLine();
}
}
......@@ -51,6 +51,9 @@ public class AnnotatedBean {
logger.info("[someChangeHandler]Change - key: {}, oldValue: {}, newValue: {}, changeType: {}",
change.getPropertyName(), change.getOldValue(), change.getNewValue(),
change.getChangeType());
if (key.equals("timeout")) {
refreshTimeout();
}
}
}
......@@ -64,4 +67,10 @@ public class AnnotatedBean {
change.getChangeType());
}
}
private void refreshTimeout() {
//do some custom logic to update placeholder value
timeout = config.getIntProperty("timeout", timeout);
logger.info("Refreshing timeout to {}", timeout);
}
}
package com.ctrip.framework.apollo.demo.spring.config;
import com.ctrip.framework.apollo.demo.spring.bean.AnnotatedBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import javax.annotation.PostConstruct;
/**
* @author Jason Song
*/
@ConfigurationProperties(prefix = "redis.cache")
public class SampleRedisConfig {
private static final Logger logger = LoggerFactory.getLogger(SampleRedisConfig.class);
private int expireSeconds;
private String clusterNodes;
private int commandTimeout;
@PostConstruct
private void init() {
logger.info("ConfigurationProperties sample - expireSeconds: {}, clusterNodes: {}, commandTimeout: {}",
expireSeconds, clusterNodes, commandTimeout);
}
public void setExpireSeconds(int expireSeconds) {
this.expireSeconds = expireSeconds;
}
public void setClusterNodes(String clusterNodes) {
this.clusterNodes = clusterNodes;
}
public void setCommandTimeout(int commandTimeout) {
this.commandTimeout = commandTimeout;
}
}
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