第八节——使用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();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值