2016-12-03 redis设置初启动并执行缓存以及执行方法后再执行缓存

本文介绍了如何在Web容器启动时设置Redis缓存,并讲解了在没有配置清除方法的情况下,仅通过连接Redis服务器执行flushall命令才能清空缓存。提到了使用Spring的ApplicationListener作为监听器实现。
摘要由CSDN通过智能技术生成

tip:重启web服务器是不能够清空redis缓存的 如果没有在web容器的监听中配置清除的方法,那么只有连接上redis的服务器并且执行flushall方法才能够清除缓存

1.设置在WEB容器在启动的时候就加载缓存,如何设置

思路:在Spring容器启动或者刷新的时候进行设置,所以必须用到ApplicationListener去监听ApplicationContext

自定义的ApplicationListener如下(观察者模式)

@Component("ContextRefreshedListener") 
public class ContextRefreshedListener implements ApplicationListener<ContextRefreshedEvent> {
 
	/** 
	* 当一个ApplicationContext被初始化或刷新触发 
	*/ 
	@Override 
	public void onApplicationEvent(ContextRefreshedEvent event) {
    	if (event.getApplicationContext().getDisplayName().equals("Root WebApplicationContext")) {
    		//System.out.println("spring容器初始化完毕================================================");
    		//多数据源缓存加载
    		SysSvrHelper.loadxml();
    		//应用系统、数据源映射缓存加载
    		AppUtils.loadCache();
    		//缓存系统参数
    		
SysConfigUtils.loadCache();

} }}


监听的自定义事件封装类为

@SuppressWarnings("serial")
public class ContextRefreshedEvent extends ApplicationContextEvent {

	/**
	 * Create a new ContextRefreshedEvent.
	 * @param source the {@code ApplicationContext} that has been initialized
	 * or refreshed (must not be {@code null})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值