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">

  • 相关阅读:
    c++ 函数中的部分代码执行一次
    如何限制对象只能建立在堆上或者栈上
    FFMPEG Qt视频播放器
    C/C++中带可变参数的函数
    柔性数组
    压缩图片网站
    vscode存盘时格式化
    两个i标签之间有缝隙
    node 中process进程argv,argv0,execArgv,execPath
    chalk插件 使终端输出的字带颜色
  • 原文地址:https://www.cnblogs.com/qiyc/p/5860657.html
Copyright © 2011-2022 走看看