application共享变量
2016-05-30 21:52
337 查看
在程序中有时候需要将一些数据提前加载,比方说一些访问量多的数据,可以将其加载到公共的地方application中,以方便访问,也可以减少和数据库交互的次数,增加效率。为此我们可以将其放到listener中,只需要加载一次就好,如下:
1、类
public class InitListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
ApplicationContext ac=WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
PrivilegeService privilegeService =(PrivilegeService)ac.getBean("privilegeServiceImpl");
//准备数据:topPrivilegeList
List<Privilege> topPrivilegeList=privilegeService.findTopList();
sce.getServletContext().setAttribute("topPrivilegeList",topPrivilegeList);
Collection<String> allPrivilegeUrls=privilegeService.getAllPrivilegeUrls();
sce.getServletContext().setAttribute("allPrivilegeUrls", allPrivilegeUrls);
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
}
2、web.xml配置此Listener
<!-- 用于做初始化工作的监听器,一定要配置到spring的ContextLoaderListener后面,因为要用到spring的容器对象 -->
<listener>
<listener-class>cn.itcast.oa.util.InitListener</listener-class>
</listener>
1、类
public class InitListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
ApplicationContext ac=WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
PrivilegeService privilegeService =(PrivilegeService)ac.getBean("privilegeServiceImpl");
//准备数据:topPrivilegeList
List<Privilege> topPrivilegeList=privilegeService.findTopList();
sce.getServletContext().setAttribute("topPrivilegeList",topPrivilegeList);
Collection<String> allPrivilegeUrls=privilegeService.getAllPrivilegeUrls();
sce.getServletContext().setAttribute("allPrivilegeUrls", allPrivilegeUrls);
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
}
2、web.xml配置此Listener
<!-- 用于做初始化工作的监听器,一定要配置到spring的ContextLoaderListener后面,因为要用到spring的容器对象 -->
<listener>
<listener-class>cn.itcast.oa.util.InitListener</listener-class>
</listener>
相关文章推荐
- Genymotion加速下载虚拟镜像速度慢失败Connection timeout
- android studio创建项目编译出错问题的解决方法
- 利用代码扫描工具提升Android代码健壮性
- 【MyBatis框架点滴】——MyBatis开发DAO的两种方法:原始DAO开发方法和Mapper代理方法
- iOS - 设置底部输入框随键盘的移动而移动
- android studio添加使用SlidingMenu
- 快速理解android事件传递拦截机制概念
- 高仿手机质量怎么样?能用吗?
- 正确使用iOS常量(const)、enum以及宏(#define)
- Gson解析使用指南
- 开机启动
- 监听SD卡状态
- 短信防火墙
- ip拨号器
- Android五大布局
- 孙孙啊i之项目实战(五) 封装日志
- Android-项目结构介绍
- JNI 错误: 无法访问android.app.Activity
- Objective-C 编程全解-第02章 Objective-C程序设计
- 微信硬件平台从入门到精通(服务器端)