首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >对于某些不会变又经常需要用的数据的处理

对于某些不会变又经常需要用的数据的处理

作者头像
Java架构师必看
发布2021-05-17 10:28:34
发布2021-05-17 10:28:34
2900
举报
文章被收录于专栏:Java架构师必看Java架构师必看
代码语言:javascript
复制
import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.itcast.oa.domain.Privilege;
import cn.itcast.oa.service.PrivilegeService;

public class InitListener implements ServletContextListener {


	public void contextInitialized(ServletContextEvent sce) {
		// 获取容器与相关的Service对象
		ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
		PrivilegeService privilegeService = (PrivilegeService) ac.getBean("privilegeServiceImpl");

		// 准备数据:topPrivilegeList
		List<Privilege> topPrivilegeList = privilegeService.findTopList();
		System.out.println("size"+topPrivilegeList.size());
		sce.getServletContext().setAttribute("topPrivilegeList", topPrivilegeList);
		System.out.println("------------> 已准备数据 <------------");
	}

	public void contextDestroyed(ServletContextEvent arg0) {

	}
}

不会变化的数据:

将从数据库查到的信息缓存起来,只访问一次数据库,以后要用直接从内存拿来用:#application.topPrivilegeList

<!-- 用于做初始化工作的监听器,一定要配置到Spring的ContextLoaderListener之后, 因为要用到Spring的容器对象 --> <listener> <listener-class>cn.itcast.oa.util.InitListener</listener-class> </listener>

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档