【Spring笔记】五、Spring集成web环境

Spring集成web环境

ApplicationContext

问题

应用上下文对象(Spring容器)是通过new ClassPathXmlApplicationContext(“Spring配置文件”)方式获取的,但是每次从容器中获得取Bean时都要编写new ClassPathXmlApplicationContext,这样会导致配置文件加载多次,应用上下文对象ApplicationContext创建多次

解决方法

在Web项目中,可以使用ServletContextListener监听器监听Web应用的启动,可以在Web启动时,就加载Spring的配置文件,创建应用上下文对象ApplicationContext,再将其存到servlet最大的域对象ServletContext中,这样就可以在任意位置从域中获取Spring容器对象了

实现步骤

  1. 创建Web应用启动监听器,实现ServletContextListener监听器接口
public class ContextLoaderListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
//        将Spring的应用上下文对象存到servlet最大的域对象ServletContext中
        ServletContext servletContext = sce.getServletContext();
        servletContext.setAttribute("app",app);
        System.out.println("Spring容器创建完毕");
    }
  1. 在web.xml中配置监听器
    <listener>
        <listener-class>com.heima.listener.ContextLoaderListener</listener-class>
    </listener>  
  1. 创建Servlet程序(在web.xml中配置servlet),获取Spring应用上下文对象,获取bean对象
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
        UserService userService = app.getBean(UserService.class);
        userService.save();
    }

解耦合优化

在web.xml中配置ServletContext初始化参数,配置Spring配置文件的文件名

<!--  配置Spring配置文件的文件名  -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>applicationContext.xml</param-value>
    </context-param>

在Listener监听器中创建Spring应用上下文对象时,读取并赋予xml中Spring配置文件的地址

public class ContextLoaderListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();
//        读取web.xml中的全局参数
        String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext(contextConfigLocation);
//        将Spring的应用上下文对象存到servlet最大的域对象ServletContext中
        servletContext.setAttribute("app",app);
        System.out.println("Spring容器创建完毕");
    }

终!Spring提供获取应用上下文对象的工具类

上面的分析不用手动实现,Spring提供了一个监听器ContextLoaderListener(需导入spring-web依赖)就是对上述功能的封装,该监听器内部加载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中,提供了一个工具类WebApplicationContextUtils以获取Spring应用上下文对象

步骤:

  1. pom.xml中导入spring-web的jar包依赖
  2. web.xml中配置ContextLoaderListener监听器
  3. 使用WebApplicationContextUtils获取Spring应用上下文对象ApplicationContext

1、pom.xml中导入spring-web的jar包依赖

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>5.0.5.RELEASE</version>
</dependency>

2、web.xml中配置ContextLoaderListener监听器

<!--  配置监听器  -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>  

​ web.xml中配置Spring配置文件的文件名

<!--  配置Spring配置文件的文件名  -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

3、使用WebApplicationContextUtils获取Spring应用上下文对象ApplicationContext

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UserService userService = app.getBean(UserService.class);
        userService.save();
    }

如需更多知识点请前往我的Spring学习笔记专栏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值