zoukankan      html  css  js  c++  java
  • Springzz中使用监听器,用于容器一启动就加载准备数据(application范围内的数据,用于减轻服务器压力,不用每次都去查数据)

    java代码:

    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("------------> 已准备数据topPrivilegeList <------------");

    // 准备数据:allPrivilegeUrls
    Collection<String> allPrivilegeUrls = privilegeService.getAllPrivilegeUrls();
    sce.getServletContext().setAttribute("allPrivilegeUrls", allPrivilegeUrls);
    System.out.println("------------> 已准备数据allPrivilegeUrls <------------");
    }

    public void contextDestroyed(ServletContextEvent arg0) {

    }
    }

    web.xml中的配置如下:

    <!-- 配置Spring的用于初始化容器对象的监听器 -->
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext*.xml</param-value>
    </context-param>

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

    在jsp中获取数据:

    <s:iterator value="#application.topPrivilegeList">

  • 相关阅读:
    BeagleBone Black安装小米随身WiFi驱动方法
    java-ee,ssh整合博文收藏
    BeagleBoneBlack Linux开发相关链接收藏
    Debian7.7 wheezy 中源码安装emacs24
    GLOG使用注意事项
    centos、linux改变ll命令显示颜色
    centos中samba配置后始终连不上的绝招
    中兴电信光纤猫F612管理员密码获取方法
    STM32W108芯片的SWD在IAR7.30版本中不能用
    TRF7960天线参数试验
  • 原文地址:https://www.cnblogs.com/qiyc/p/5860657.html
Copyright © 2011-2022 走看看