parent
cd5f43941e
commit
7395d75b25
@ -0,0 +1,94 @@
|
||||
// 定义当前类所在的包路径
|
||||
package com.ServletContextListener;
|
||||
|
||||
// 导入MyBatis-Plus实体包装类
|
||||
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
||||
// 导入字典实体类
|
||||
import com.entity.DictionaryEntity;
|
||||
// 导入字典服务接口
|
||||
import com.service.DictionaryService;
|
||||
// 导入自定义线程方法类
|
||||
import com.thread.MyThreadMethod;
|
||||
// 导入日志记录器接口
|
||||
import org.slf4j.Logger;
|
||||
// 导入日志工厂类
|
||||
import org.slf4j.LoggerFactory;
|
||||
// 导入Spring应用上下文接口
|
||||
import org.springframework.context.ApplicationContext;
|
||||
// 导入Web应用上下文工具类
|
||||
import org.springframework.web.context.support.WebApplicationContextUtils;
|
||||
|
||||
// 导入Servlet监听器接口
|
||||
import javax.servlet.ServletContextListener;
|
||||
// 导入Servlet上下文事件类
|
||||
import javax.servlet.ServletContextEvent;
|
||||
// 导入Web监听器注解
|
||||
import javax.servlet.annotation.WebListener;
|
||||
// 导入HashMap实现类
|
||||
import java.util.HashMap;
|
||||
// 导入List集合接口
|
||||
import java.util.List;
|
||||
// 导入Map集合接口
|
||||
import java.util.Map;
|
||||
|
||||
// 字典初始化监听器类
|
||||
// 使用@WebListener注解声明为Servlet监听器
|
||||
// 实现ServletContextListener接口
|
||||
@WebListener
|
||||
public class DictionaryServletContextListener implements ServletContextListener {
|
||||
|
||||
// 声明日志记录器
|
||||
private static final Logger logger = LoggerFactory.getLogger(DictionaryServletContextListener.class);
|
||||
// 声明自定义线程方法对象
|
||||
private MyThreadMethod myThreadMethod;
|
||||
|
||||
// 上下文销毁方法实现
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent sce) {
|
||||
// 记录服务器停止日志
|
||||
logger.info("----------服务器停止----------");
|
||||
}
|
||||
|
||||
// 上下文初始化方法实现
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent sce) {
|
||||
// 获取Spring应用上下文
|
||||
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
|
||||
|
||||
// 记录字典表初始化开始日志
|
||||
logger.info("----------字典表初始化开始----------");
|
||||
// 从Spring容器获取字典服务实例
|
||||
DictionaryService dictionaryService = (DictionaryService)appContext.getBean("dictionaryService");
|
||||
// 查询所有字典数据
|
||||
List<DictionaryEntity> dictionaryEntities = dictionaryService.selectList(new EntityWrapper<DictionaryEntity>());
|
||||
// 创建字典映射表
|
||||
Map<String, Map<Integer,String>> map = new HashMap<>();
|
||||
// 遍历字典数据并构建映射关系
|
||||
for(DictionaryEntity d :dictionaryEntities){
|
||||
// 获取当前字典代码对应的映射表
|
||||
Map<Integer, String> m = map.get(d.getDicCode());
|
||||
// 如果映射表为空则新建
|
||||
if(m ==null || m.isEmpty()){
|
||||
m = new HashMap<>();
|
||||
}
|
||||
// 添加字典项到映射表
|
||||
m.put(d.getCodeIndex(),d.getIndexName());
|
||||
// 更新字典映射表
|
||||
map.put(d.getDicCode(),m);
|
||||
}
|
||||
// 将字典映射表存入Servlet上下文
|
||||
sce.getServletContext().setAttribute("dictionaryMap", map);
|
||||
// 记录字典表初始化完成日志
|
||||
logger.info("----------字典表初始化完成----------");
|
||||
|
||||
// 记录线程执行开始日志
|
||||
logger.info("----------线程执行开始----------");
|
||||
// 初始化并启动自定义线程
|
||||
if (myThreadMethod == null) {
|
||||
myThreadMethod = new MyThreadMethod();
|
||||
myThreadMethod.start(); // 在Servlet上下文初始化时启动线程
|
||||
}
|
||||
// 记录线程执行结束日志
|
||||
logger.info("----------线程执行结束----------");
|
||||
}
|
||||
}
|
Loading…
Reference in new issue