From f3445238b3d3f9a6a7d1fa358ebac1257c7acbc7 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Sun, 12 May 2024 23:19:32 +0800 Subject: [PATCH] feat: auto init device --- .../cqwu/repair/dao/impl/DeviceDaoImpl.java | 5 --- .../listener/MyServletContextListener.java | 34 +++++++++++++++++++ .../java/cn/edu/cqwu/repair/util/AppInit.java | 14 ++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 src/main/java/cn/edu/cqwu/repair/listener/MyServletContextListener.java create mode 100644 src/main/java/cn/edu/cqwu/repair/util/AppInit.java diff --git a/src/main/java/cn/edu/cqwu/repair/dao/impl/DeviceDaoImpl.java b/src/main/java/cn/edu/cqwu/repair/dao/impl/DeviceDaoImpl.java index 72501fc..acd6c76 100644 --- a/src/main/java/cn/edu/cqwu/repair/dao/impl/DeviceDaoImpl.java +++ b/src/main/java/cn/edu/cqwu/repair/dao/impl/DeviceDaoImpl.java @@ -16,13 +16,8 @@ import java.util.ArrayList; public class DeviceDaoImpl implements DeviceDao { private static final DeviceMapper mapper = ConnectionFactory.getMapper(DeviceMapper.class); - @Override public ArrayList findAllDevice() { - - return (ArrayList) mapper.selectAll(); } - - } diff --git a/src/main/java/cn/edu/cqwu/repair/listener/MyServletContextListener.java b/src/main/java/cn/edu/cqwu/repair/listener/MyServletContextListener.java new file mode 100644 index 0000000..0c92750 --- /dev/null +++ b/src/main/java/cn/edu/cqwu/repair/listener/MyServletContextListener.java @@ -0,0 +1,34 @@ +package cn.edu.cqwu.repair.listener; + +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; +import javax.servlet.annotation.WebListener; + +import cn.edu.cqwu.repair.dao.DeviceDao; +import cn.edu.cqwu.repair.util.AppInit; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.WebApplicationContextUtils; + + +/** + * @author xtaod + */ +@WebListener +public class MyServletContextListener extends AppInit implements ServletContextListener { + + @Override + public void contextInitialized(ServletContextEvent sce) { + ServletContext application = sce.getServletContext(); + WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(application); + if (context != null) { + DeviceDao deviceDao = context.getBean(DeviceDao.class); + initDevice(application, deviceDao); + } + } + + @Override + public void contextDestroyed(ServletContextEvent sce) { + + } +} diff --git a/src/main/java/cn/edu/cqwu/repair/util/AppInit.java b/src/main/java/cn/edu/cqwu/repair/util/AppInit.java new file mode 100644 index 0000000..d3492f9 --- /dev/null +++ b/src/main/java/cn/edu/cqwu/repair/util/AppInit.java @@ -0,0 +1,14 @@ +package cn.edu.cqwu.repair.util; + +import cn.edu.cqwu.repair.dao.DeviceDao; + +import javax.servlet.ServletContext; + +/** + * @author xtaod + */ +public class AppInit { + public static void initDevice(ServletContext servletContext, DeviceDao deviceDao) { + servletContext.setAttribute("device", deviceDao.findAllDevice()); + } +}