feat: auto init device

This commit is contained in:
xtaodada 2024-05-12 23:19:32 +08:00
parent 79d16918be
commit f3445238b3
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
3 changed files with 48 additions and 5 deletions

View File

@ -16,13 +16,8 @@ import java.util.ArrayList;
public class DeviceDaoImpl implements DeviceDao { public class DeviceDaoImpl implements DeviceDao {
private static final DeviceMapper mapper = ConnectionFactory.getMapper(DeviceMapper.class); private static final DeviceMapper mapper = ConnectionFactory.getMapper(DeviceMapper.class);
@Override @Override
public ArrayList<Device> findAllDevice() { public ArrayList<Device> findAllDevice() {
return (ArrayList<Device>) mapper.selectAll(); return (ArrayList<Device>) mapper.selectAll();
} }
} }

View File

@ -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) {
}
}

View File

@ -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());
}
}