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数据。
时间: 2024-11-02 11:55:20