其他分享
首页 > 其他分享> > 第八节——使用ContextListener监听器简化开发

第八节——使用ContextListener监听器简化开发

作者:互联网

一、手工使用ContextListener监听器简化开发

  1. 目的:由于每个servlet都需要applicationContext.xml来获取bean对象,所以使用监听器当项目启动就加载文件,并将app放入最大的域中供所有servlet使用
  2. 在main-java-demo1-listener目录下创建ContextLoaderListener.class
public class ContextLoaderListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        //将app对象存储到最大的域ServletContext中
        ServletContext servletContext = sce.getServletContext();
        servletContext.setAttribute("app",app);
    }
}
  1. 编辑main-webapp-WEB-INF目录下的web.xml,将监听器配置进去
<listener>
	<listener-class>demo1.listener.ContextLoaderListener</listener-class>
</listener>
  1. 编辑web目录下的UserServlet.class,从域中获取app
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        ServletContext servletContext = req.getServletContext();
        ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
        UserServiceImpl userService = (UserServiceImpl) app.getBean("userService");
        userService.show();
    }
}
  1. 开启tomcat,访问http://localhost:8080/demo02_war/userServlet,运行成功

二、通过web.xml指定springconfig文件,不使用ClassPathXmlApplicationContext

  1. 编辑web.xml,添加全局初始化参数
<context-param>
    <param-name>contextConfig</param-name>
    <param-value>applicationContext.xml</param-value>
  </context-param>
  1. 编辑listener目录下的ContextLoaderListener.class,从域中获取全局初始化参数,并生成app
public class ContextLoaderListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
//        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        //将app对象存储到最大的域ServletContext中
        ServletContext servletContext = sce.getServletContext();
        String contextConfig = servletContext.getInitParameter("contextConfig");//此值与web.xml中param-name一致
        ApplicationContext app = new ClassPathXmlApplicationContext(contextConfig);
        servletContext.setAttribute("app",app);
    }
}

三、配置Spring自带的监听器简化开发

  1. 导入监听器坐标
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-web</artifactId>
	<version>5.3.10</version>
</dependency>
  1. 从web.xml中配置ContextLoaderListener监听器
<context-param>
  <param-name>contextConfig</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
  1. 将listener目录删除
  2. 编辑web目录下的UserServlet,使用webapplicationContextUtils获取app对象
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ServletContext servletContext = req.getServletContext();
        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UserServiceImpl userService = (UserServiceImpl) app.getBean("userService");
        userService.show();
    }
}

标签:xml,web,servletContext,ContextListener,app,第八节,ContextLoaderListener,监听器
来源: https://blog.csdn.net/qq_37589805/article/details/120593451