您的位置:首页 > 编程语言 > Java开发

configServer提交后手动刷新

2016-08-23 00:00 246 查看
摘要: 对于使用spring @Value("$xxx.xxx")的数据在提交configServer后刷新bean

1.在使用到@value('param')的类中添加@RefreshScope

@RestController
@RefreshScope
public class HelloController {

private Logger log = LoggerFactory.getLogger(HelloController.class);

@Value("${test.name}")
private String name;

@RequestMapping(value="/getName", method = RequestMethod.GET)
public String hello() {
log.info("call getName parameter:{}");
return "{hello: '" + name + "'}";
}

/**
* rest 服务用来测试
* --@requestParam url?xxx=name
* --requestBody 认定为json传输解析  url?{xxx=name}
* @param name
* @return
*/
@RequestMapping(value = "/hello", method = RequestMethod.POST)
public String hello(String name) {
log.info("call hello parameter:{}", name);
return "{hello: '" + name + "'}";
}

}

2.在pom.xml文件中添加spring-boot-starter-actuator

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>

3.在当前应用中通过post的方式refersh

测试 postman http://localhost:8080/refresh spring会刷新作用域
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息