心耘博客 | ZLPRIME-磊落平生志,破浪去乘风 心耘博客 | ZLPRIME-磊落平生志,破浪去乘风
  • 首页
  • 朝花夕拾
    • 光影记录
    • 生活随笔
  • 代码如诗
    • 100 Days of SwiftUI
    • 后端技术
    • 服务器配置
  • 影集
  • 关于我
  • 更多
    • 专题汇总
    • 友情链接
    • 留言板
  • 0
  • 0

Spring集成Web环境

心耘
3 年前

在使用spring框架以后,我们将对象创建通过控制反转IOC,交给了Spring容器。在进行Web开发时,很多地方都需要用到Spring的ApplicationContext上下文对象。应用上下文对象是通过new ClasspathXmlApplicationContext(spring配置文件) 方式获取的,但是每次从容器中获得Bean时都要编写new ClasspathXmlApplicationContext(spring配置文件) ,这样的弊端是配置文件加载多次,应用上下文对象创建多次。

所以,在Web项目中,可以使用ServletContextListener监听Web应用的启动。在Web应用启动时,就加载Spring的配置文件,创建应用上下文对象ApplicationContext,再将其存储到最大的域servletContext域中,这样就可以在任意位置从域中获得应用上下文ApplicationContext对象了。根据以上思路,可以进行以下开发:

首先创建一个监听Web应用启动的监听器:

/*
*   自定义一个ContextLoaderListener,让其在Web应用启动的时候,就加载Spring的配置文件。
*   然后,创建出ApplicationContext对象,并存储到ServletContext中
* */
public class ContextLoaderListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //为了解耦,将spring配置文件的路径,配置在web.xml中的全局参数中,通过ServletContext读取
        ServletContext servletContext = sce.getServletContext();
        String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
        ApplicationContext app = new ClassPathXmlApplicationContext(contextConfigLocation);

        //将Spring的应用上下文对象,存储到ServletContext域中
        servletContext.setAttribute("app",app);
        System.out.println("Spring容器创建完毕!");
    }
}

然后配置监听器:

    <!--全局初始化参数-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>applicationContext.xml</param-value>
    </context-param>
    
	<!--配置监听器-->    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

为了解耦,创建工具类从ServletContext获取在监听器中创建并存储的ApplicationContext对象

public class WebApplicationContextUtils {
    public static ApplicationContext getWebApplicationContext(ServletContext servletContext){
        return (ApplicationContext) servletContext.getAttribute("app");
    }
}

在Servlet中获取ApplicationContext上下文对象:

public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取ServletContext对象
        ServletContext servletContext = req.getServletContext();
        //通过工具类获取ApplicationContext
        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UserService userService = (UserService) app.getBean("userService");
        userService.save();
    }
}

Spring提供获取应用上下文的工具

Spring提供了一个监听器ContextLoaderListener就是对上述功能的封装,该监听器内部加载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中,并且提供了一个客户端工具WebApplicationContextUtils供使用者获得应用上下文对象。所以,我们完全可以不用进行以上的开发,直接引入依赖,并进行配置。其它的工作,Spring都替我们做好了。

  1. 在web.xml中配置ContextLoaderListener监听器(导入spring-web坐标)
  2. 使用WebApplicationContextUtils获得应用上下文对象ApplicationContext

首先,导入Spring集成Web的坐标

        <!--导入Spring集成Web的坐标-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.3.3</version>
        </dependency>

然后,配置ContextLoaderListener监听器

    <!--全局初始化参数-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    
    <!--配置监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

最后,直接通过spring-web提供的WebApplicationContextUtils工具获得ApplicationContext对象

public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = req.getServletContext();
        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UserService userService = (UserService) app.getBean("userService");
        userService.save();
    }
}
spring
0
MySQL导出表结构和数据
上一篇
SpringMVC的执行流程与组件
下一篇

评论 (0)

再想想
暂无评论

心耘

73
文章
167
评论
94
喜欢

标签

centos (13) java (22) jenkins (7) linux (12) lombok (1) maven (1) mysql (3) redis (2) rocky linux (3) spring (8) wordpress (5) 发布 (3) 实用技巧 (7) 教程 (6) 服务器配置 (3) 生活 (5) 部署 (2)

聚合文章

生活杂记(一)
家电选购小结-空调
十一带娃心得
通过War包升级Jenkins版本

专题推荐

7

Jenkins合集

12

服务器配置

猜你喜欢

Mac中安装并启动Jmeter

Mac中安装并启动Jmeter

2 年前
734 0 2
Dozer的基本使用

Dozer的基本使用

3 年前
1,713 0 0
RABC模型

RABC模型

3 年前
1,724 0 0
Mac M1芯片无法运行nacos 2.x版本

Mac M1芯片无法运行nacos 2.x版本

3 年前
2,446 0 3

简介

海边微风起,等风也等你

留言板

留言板

小伙伴们

2Broear 乙末博客 若志随笔 豆豆 诗意笔记 元のDiary
Copyright © 2017-2025 心耘博客 | ZLPRIME-磊落平生志,破浪去乘风. 皖ICP备17019582号
  • 首页
  • 朝花夕拾
    • 光影记录
    • 生活随笔
  • 代码如诗
    • 100 Days of SwiftUI
    • 后端技术
    • 服务器配置
  • 影集
  • 关于我
  • 更多
    • 专题汇总
    • 友情链接
    • 留言板

搜索

  • java
  • spring

心耘

73
文章
167
评论
94
喜欢