Spring的MapPropertySource刷新系统属性值(key-value配置在外部系统中如redis,数据库等)
来源:互联网 发布:python logger 编辑:程序博客网 时间:2024/05/17 23:54
我们经常会把一些属性值以key=value的形式放在properties文件中,有的时候,我们可能会将一些经常变动的属性值放在外部系统中,比如redis或者数据库中,如果在某个时刻修改了redis中的值,如何让使用了该值的系统及时刷新系统内存中的值呢?下面我将介绍,如何在Spring环境中动态刷新属性值。
1、假设我在redis服务器上set了一个key为env,value为dev的值
2、自定义MapPropertySource并继承Spring的MapPropertySource类,在自定义的刷新类中添加refresh方法,用于定时刷新内部属性值
package com.example.spring.refresh.config;import com.google.common.collect.Maps;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.AutoConfigureAfter;import org.springframework.core.env.MapPropertySource;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;import java.util.Map;/** * ClassName: RefreshPropertySource <br/> * Description: 将外部属性,在实例化时动态注入到Spring容器中 <br/> * Date: 2017/10/12 13:56 <br/> * * @author sxp(1378127237@qq.com) <br/> * @version 1.0 <br/> */@Component@AutoConfigureAfter(RedisTemplate.class)public class RefreshPropertySource extends MapPropertySource { @Autowired private StringRedisTemplate redisTemplate; public RefreshPropertySource(String name, Map<String, Object> source) { super(name, source); } public RefreshPropertySource() { super("RefreshPropertySource", Maps.newHashMap()); } @PostConstruct public void setup() { redisTemplate.opsForValue().set("env", "dev"); } public void refresh() { String env = redisTemplate.opsForValue().get("env"); this.source.put("env", env); }}
3、使用Spring的定时器机制,定时检测外部系统中属性值的变化
package com.example.spring.refresh.config;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cloud.context.refresh.ContextRefresher;import org.springframework.cloud.context.scope.refresh.RefreshScope;import org.springframework.core.env.ConfigurableEnvironment;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import org.springframework.web.client.RestTemplate;/** * ClassName: RefreshScheduler <br/> * Description: RefreshScheduler <br/> * Date: 2017/10/11 10:22 <br/> * * @author sxp(1378127237@qq.com) <br/> * @version 1.0 <br/> */@Componentpublic class RefreshScheduler { @Autowired private ConfigurableEnvironment environment; @Autowired private RefreshPropertySource refreshPropertySource; @PostConstruct public void setup() { environment.getPropertySources().addLast(refreshPropertySource); } @Scheduled(cron = "*/5 * * * * ?") public void scheduling() { /* 将自定的RefreshPropertySource添加到Spring的环境中定时刷新 */ environment.getPropertySources().addLast(refreshPropertySource); System.out.println(environment.getProperty("env")); }}
阅读全文
0 0
- Spring的MapPropertySource刷新系统属性值(key-value配置在外部系统中如redis,数据库等)
- spring整合redis,一种持久key-value的数据库
- redis在spring集成下key与value的使用方法
- Matlab中使用外部系统命令(如C++编译的exe等)
- Key/Value数据库redis
- Key-Value数据库 Redis
- Key-Value数据库--redis
- Redis是一种面向“key-value”类型数据的分布式NoSQL数据库系统,具有高性能、持久存储、适应高并发应用场景等优势。它虽然起步较晚,但发展却十分迅速。
- 获取android系统中图片的属性,如时间,地点等
- Key-value系统介绍
- redis是key-value存储的,放在内存中,并在磁盘持久化的数据结构存储系统 通过set key value来存储,通过get key来获取值 复制代码 set key value:设
- 将Salt Stack的运行结果存储到数据库等外部系统中
- 2 Springboot中使用redis,配置redis的key value生成策略
- Springboot中使用redis,配置redis的key value生成策略(解决编码问题)
- Dynamo - 亚马逊的 key value 系统 简介
- redis在Linux系统的配置
- Redis中key-value实现
- 在Ubuntu系统中安装Redis数据库服务器(附Redis常用的命令)
- 关于SecureRandom导致tomcat启动慢
- 关于小程序
- struts2实现文件下载
- java 集合类对比
- WebAssembly,Web的新时代
- Spring的MapPropertySource刷新系统属性值(key-value配置在外部系统中如redis,数据库等)
- 让你飞快构建Android开发框架----Android Studio Template
- 可重用模块化设计运用到menu小程序
- Pyhton3+openCV3初体验
- python+requests接口自动化测试框架实例详解教程
- [Android学习笔记] Service 学习
- Vue.js框架
- 浅谈nginx内存池(三)
- 1.2 Web 资源