zlprime zlprime
  • 首页
  • 朝花夕拾
    • 摄影
    • 生活
    • 随笔
  • 编程学习
    • Java
    • Vue
    • WordPress
  • 其它
    • 游记地图
    • 闲言碎语
    • 友情链接
    • 留言板
首页 › 编程学习 › Java › Spring集成Web环境

Spring集成Web环境

Stone
1年前Java
1,152 0 0

在使用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 ssm
0
本文系作者 @Stone 原创发布在 zlprime。未经许可,禁止转载。
MySQL导出表结构和数据
上一篇
SpringMVC的执行流程与组件
下一篇
评论 (0)
再想想
聚合文章
03Java中的变量
3月前
01从BeanFactory入门Spring框架
3月前
Jenkins修改插件的下载源
3月前
02Java中的数据类型
3月前
相关文章
03Java中的变量
01从BeanFactory入门Spring框架
02Java中的数据类型
01一个简单的Java程序
简介

海边微风起,等风也等你

留言板
留言板
小伙伴们
2Broear Libra の 向往 云帆沧海 若志随笔 豆豆 Chuanbo 诗意笔记
Copyright © 2017-2023 zlprime. 皖ICP备17019582号
  • 首页
  • 朝花夕拾
    • 摄影
    • 生活
    • 随笔
  • 编程学习
    • Java
    • Vue
    • WordPress
  • 其它
    • 游记地图
    • 闲言碎语
    • 友情链接
    • 留言板
热门搜索
  • java
  • spring
Stone
75 文章
145 评论
84 喜欢
  • 0
  • 0
  • Top