在程序中。有些数据我们希望在程序启动的时候就准备好,而且仅仅准备一次,放在application作用域中,这时候。我们一般会用Listener来准备这些数据。

可是,用Listener准备application作用域的数据。在获取容器的时候会有一些注意事项。

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();
sce.getServletContext().setAttribute("topPrivilegeList", topPrivilegeList);
System.out.println("------------> 已准备数据 <------------");
// 准备数据:allPrivilegeUrls
Collection<String> allPrivilegeUrls = privilegeService.getAllPrivilegeUrls();
sce.getServletContext().setAttribute("allPrivilegeUrls", allPrivilegeUrls);
System.out.println("------------> 已准备数据allPrivilegeUrls <------------");
} public void contextDestroyed(ServletContextEvent arg0) { }
  1. 该Listener配置在web.xml里,默认通过反射生成实例,来得到这个对象实例来运行
  2. 并没有从Spring容器里面获取,Tomcat没有找Spring容器,所以此处无法使用注解
  3. 假设使用注解,会生成两个对象,一个Tomcat产生的对象,一个Spring容器注入的对象
  4. Tomcat会使用自己产生的对象,而Spring管理的对象没人使用

使用Listener准备application作用域数据的更多相关文章

  1. 通过Application传递数据代码

    使用Application传递数据步骤如下:创建新class,取名MyApp,继承android.app.Application父类,并在MyApp中定义需要保存的属性     在整个Android程 ...

  2. Android--通过Application传递数据

    在整个Android程序中,有时需要保存某些全局的数据(如:用户信息),方便在程序的任何地方调用.在Activity之间数据传递中有一种比较使用的方式,就是全局对象,使用过J2EE的都应该知道Java ...

  3. Application作用域实现:当用户重复登录时,挤掉原来的用户

    Application作用域实现:当用户重复登录时,挤掉原来的用户 一.实现思想 1.application(ServletContext)是保存在服务器端的作用域,我们在application中保存 ...

  4. JSP中page、request、session、application作用域的使用

    几乎所有的Web开发语言都支持Session功能,Servlet也不例外. Servlet/JSP中的Session功能是通过作用域(scope)这个概念来实现的. 作用域分为四种,分别为: page ...

  5. 通过Application传递数据

    1:通过Application传递数据 假如有一个Activity A, 跳转到 Activity B ,并需要推荐一些数据,通常的作法是Intent.putExtra() 让Intent携带,或者有 ...

  6. 对request,session,application作用域形象理解

    看到一篇比较有意思的文章,分享一下.原网址:http://blog.csdn.net/rushkid02/article/details/8063792 几乎所有的Web开发语言都支持Session功 ...

  7. Application中数据传递及内存泄漏问题

    原文地址:http://android.tgbus.com/Android/tutorial/201107/359474.shtml Application的使用 Application和Actovo ...

  8. Application共享数据

    1.Application与Session的区别 Application对象:实现程序级别的数据共享. Session对象:实现会话级别的数据共享. 当需要整个程序级别的共享信息时,可以使用Appli ...

  9. Android 通过Application 传递数据

    </pre><pre> package com.example.ApplicationTest; import android.app.Application; /** * C ...

随机推荐

  1. 子元素的margin-top影响父元素原因和解决办法

    这个问题会出现在所有浏览器当中,原因是css2.1盒子模型中规定, In this specification, the expression collapsing margins means tha ...

  2. [r]Seven habits of effective text editing

    Seven habits of effective text editing(via) Bram Moolenaar November 2000 If you spend a lot of time ...

  3. js动态加载脚本

    最近公司的前端地图产品需要做一下模块划分,希望用户用到哪一块的功能再加载哪一块的模块,这样可以提高用户体验. 所以到处查资料研究js动态脚本的加载,不过真让人伤心啊!,网上几乎都是同一篇文章,4种方法 ...

  4. 练习2 A - ASCII码排序

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description 输入三 ...

  5. 一个关于导出excel模板的实例

    1 首先jsp页面 点击模板下载,会自动下载模板excel,效果如下 让我们看源码: 1 jsp页面 <div class="tab-pane" id="profi ...

  6. iOS触摸事件处理--备用

    主要是记录下iOS的界面触摸事件处理机制,然后用一个实例来说明下应用场景. 一.处理机制 界面响应消息机制分两块,(1)首先在视图的层次结构里找到能响应消息的那个视图.(2)然后在找到的视图里处理消息 ...

  7. Altium Designer 从导入DXF文件,并转换成板框

    大多数人都知道,PADS中导入DXF文件,然后转换成板框,是很方便的.AD也同样可以做到. PADS导入DXF见:http://www.cnblogs.com/craftor/archive/2012 ...

  8. libc.so.6 误删后修复

    libc.so.6 误删后修复  libc.so.6 被删除了(libc.so.6只是个链接,真实的lib 文件是 libc-2.15.so) su, sudo,ls, cp, mv 等等一系列命令都 ...

  9. Android 安全测试

    文章Android Security Tools对1~4的使用有介绍,下面主要分析其源码实现. 1.Manifest Explorer 2.Package Play Main.java public ...

  10. Contains Duplicate II ——LeetCode

    Given an array of integers and an integer k, find out whether there there are two distinct indices i ...