Spring的MapPropertySource刷新系统属性值(key-value配置在外部系统中如redis,数据库等)

我们经常会把一些属性值以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/>
 */
@Component
public 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"));
    }

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是Java使用注解操作Redis的示例: 首先,我们需要在pom.xml文件添加Redis的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 接下来,我们需要在Spring Boot的Application类上添加@EnableCaching注解来开启缓存: ```java @SpringBootApplication @EnableCaching public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 然后,我们需要在Redis配置文件application.properties添加Redis的连接信息: ```properties spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= ``` 接下来,我们就可以使用注解来操作Redis了。下面是一个使用注解操作Redis缓存的示例: ```java @Service public class UserService { @Autowired private UserRepository userRepository; @Cacheable(value = "userCache", key = "#id") public User getUserById(Long id) { System.out.println("从数据库获取用户信息"); return userRepository.findById(id).orElse(null); } @CachePut(value = "userCache", key = "#user.id") public User saveUser(User user) { System.out.println("保存用户信息到数据库"); return userRepository.save(user); } @CacheEvict(value = "userCache", key = "#id") public void deleteUserById(Long id) { System.out.println("从数据库删除用户信息"); userRepository.deleteById(id); } } ``` 上面的示例,我们使用了三个注解来操作Redis缓存: - @Cacheable:表示方法的结果可以被缓存,如果缓存有数据,则直接返回缓存数据,否则执行方法并将结果放入缓存。 - @CachePut:表示方法的结果需要被缓存,每次都会执行方法,并将结果放入缓存。 - @CacheEvict:表示方法会从缓存删除数据。 在这个示例,我们使用了value属性来指定缓存的名称,key属性来指定缓存的键,#id和#user.id是SpEL表达式,用于获取方法参数。 以上就是一个使用注解操作Redis缓存的示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值