欢迎您的访问
专注于分享最有价值的互联网技术干货

在项目启动时 使用监听器加载所有字典表数据

几个T的资料等你来白嫖
双倍快乐
资源帮找!!!

1.首先在web.xml配置

<listener>
        <listener-class>DataDictionaryListener</listener-class>
 </listener>

2.书写DataDictionaryListener类

public class DataDictionaryListener extends ContextLoader implements ServletContextListener{

    private ArrayList<String> codeArrayList = new ArrayList<String>();//数据字典Code列表

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //Spring上下文获取及Bean获取
        ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
        DataDictionaryService dataDictionaryService = (DataDictionaryService) applicationContext.getBean("dataDictionaryService");
        ServletContext servletContext = sce.getServletContext();

        //查询获取数据库所有数据字典Code列表
        try {
            codeArrayList = dataDictionaryService.getDataDictionaryCodeList();
        }catch (Exception e){
            System.out.println("============项目启动获取数据字典Code列表出错==================");
            e.printStackTrace();
        }

        //循环CodeList初始化数据字典数据之内存中
        try {
            for (String code : codeArrayList) {
                servletContext.setAttribute(code, dataDictionaryService.getDataDictionaryByStr(code));
            }
        }catch (Exception ex){
            System.out.println("============项目启动存储数据字典列表至内存出错==================");
            ex.printStackTrace();
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        closeWebApplicationContext(sce.getServletContext());
    }
}

 3.从内存中取出每个code对应的list集合dataDictionaryService方法

WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
                ServletContext servletContext = webApplicationContext.getServletContext();
                arrayList = (ArrayList<DataDictionary>) servletContext.getAttribute(code);

  获取到该code对应的arrayList数据。

 

赞(0) 打赏
版权归原创作者所有,任何形式转载请联系我们:大白菜博客 » 在项目启动时 使用监听器加载所有字典表数据

评论 抢沙发

8 + 9 =
  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏