Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >9-Spring集成web环境

9-Spring集成web环境

作者头像
Ywrby
发布于 2022-10-27 05:25:22
发布于 2022-10-27 05:25:22
31500
代码可运行
举报
文章被收录于专栏:YwrbyYwrby
运行总次数:0
代码可运行

ApplicationContext应用上下问的获取方式

下面是之前一直采用的应用上下问的获取方法

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService service= (UserService) context.getBean("userService");
        service.save();
    }
}

通过new ClassPathXmlApplicationContext(“applicationContext.xml”)来获取应用上下文,不过这种方式获取的弊端就是所有web层的服务使用前都需要利用new ClassPathXmlApplicationContext(“applicationContext.xml”);加载配置文件,导致配置文件需要重复被加载多次,应用上下文的对象也需要创建多次

在Web项目中,要解决这个问题,可以利用ServletContextListener监听web应用的启动,一旦web应用启动,就加载Spring配置文件,并创建ApplicationContext应用上下文对象,然后将其存储入最大的域servletContext中,其他web层方法就可以在需要时直接从域中获取应用上下文对象

配置文件web.xml 配置监听器和Servlet

这里将Spring配置文件的文件名作为全局参数进行配置,避免了文件名加载配置文件导致的耦合

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!--全局初始化参数 将Spring配置文件作为参数存储以解耦合-->
    <context-param>
        <param-name>applicationContext</param-name>
        <param-value>applicationContext.xml</param-value>
    </context-param>

    <!--配置监听器-->
    <listener>
        <listener-class>cn.ywrby.listener.ContextLoaderListener</listener-class>
    </listener>

    <!--配置Servlet-->
    <servlet>
        <servlet-name>UserServlet</servlet-name>
        <servlet-class>cn.ywrby.web.UserServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>UserServlet</servlet-name>
        <url-pattern>/userServlet</url-pattern>
    </servlet-mapping>
    

</web-app>
创建监听器
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/**
 * 创建监听器,监听服务器启动
 */
public class ContextLoaderListener implements ServletContextListener {
    /**
     * 在服务器启动时加载配置文件创建应用上下文对象
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //获取ServletContext域
        ServletContext servletContext=sce.getServletContext();
        //从ServletContext域中获取全局初始化参数(获得Spring配置文件名)
        String context_name=servletContext.getInitParameter("applicationContext");
        //加载Spring配置文件并创建Spring应用上下文
        ApplicationContext context=new ClassPathXmlApplicationContext(context_name);
        //将Spring应用上下文存储到最大的域servletContext中
        servletContext.setAttribute("app",context);

    }
}
修改Servlet,从ServletContext域中获取Spring应用上下文
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //利用req从ServletContext域中获取存储的应用上下文对象
        ApplicationContext context= (ApplicationContext) req.getServletContext().getAttribute("app");
        //利用应用上下文获取Spring容器中的service层对象
        UserService service= (UserService) context.getBean("userService");
        service.save();
    }
}

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

上文提到的获取应用上下文的方式较为繁琐,并且每个web项目几乎都需要进行配置上下文的获取,所以Spring已经对应用上下文的获取进行了封装,我们只需要使用其提供的工具即可

Spring提供了一个监听器ContextLoaderListener就是对上述监听器的封装,该监听器实现了内部加载配置文件,创建应用上下文对象,并将对象存储在ServletContext域中,,同时提供了一个工具类WebApplicationContextUtils用来进行应用上下文的获取

使用步骤

0. 在pom.xml中导入spring-web坐标
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.3.3</version>
</dependency>
1. 在web.xml中配置ContextLoaderListener监听器

注意,这里的初始化参数名称必须是contextConfigLocation不能进行修改

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<!--全局初始化参数 将Spring配置文件作为参数存储以解耦合-->
<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>
2. 使用WebApplicationContextUtils获取应用上下文对象ApplicationContext
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //(利用req获取ServletContext域)
        ServletContext servletContext=req.getServletContext();
        //利用WebApplicationContextUtils获取应用上下文
        WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(servletContext);
        //利用应用上下文获取Spring容器中的service层对象
        UserService service= (UserService) context.getBean("userService");
        service.save();
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-03-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Spring整合javaweb的基本步骤简单记录
Spring整合javaweb Spring与javaweb整合使用 Spring来控制事务(dao---jdbctemplate) 所有组件@Autowired 管理数据库 Spring整合javaweb 1.导入相关坐标 2.写配置 1.将所有组件加入容器中 1.@Controller: servlet层 目前不能标注在servlet层 2.@Service:业务逻辑层 3.@Repository:dao层 4.@Component:其他组件 Tips: 可以写一个WebUtils类,里面封装一
大忽悠爱学习
2021/11/15
4120
ServletContext与Web应用以及Spring容器启动
Servlet容器在启动时会加载Web应用,并为每个Web应用创建唯一的ServletContext对象。
良月柒
2019/03/20
8990
ServletContext与Web应用以及Spring容器启动
面试之Spring的启动原理
为什么突然说一下Spring启动原理呢,因为之前面试的时候,回答的那可谓是坑坑洼洼,前前后后,补补贴贴。。。
Java技术债务
2022/08/09
2640
面试之Spring的启动原理
Spring集成web环境
1. ApplicationContext应用上下文获取方式 应用上下文对象是通过new ClasspathXmlApplicationContext(spring配置文件) 方式获取的,但是每次从容器中获得Bean时都要编写new ClasspathXmlApplicationContext(spring配置文件) ,这样的弊端是配置文件加载多次,应用上下文对象创建多次。 在Web项目中,可以使用ServletContextListener监听Web应用的启动,我们可以在Web应用启动时,就加载Sprin
兮动人
2021/11/16
7950
Spring入门
2.创建UserDao接口和UserDao接口的实现类UserDaoImpl 3.创建spring的xml配置文件,然后在其中给实现类标注id
大忽悠爱学习
2022/05/05
8980
Spring入门
Spring mvc ContextLoaderListener 原理解析
对于熟悉Spring MVC功能,首先应从web.xml 开始,在web.xml 文件中我们需要配置一个监听器 ContextLoaderListener,如下。 <!-- 加载spring上下文信息,最主要的功能是解析applicationContext.xml --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
java404
2018/05/18
4620
Spring学习笔记(4)一SpringMVC启动原理和WebApplicationContext
在分析SpringMVC框架具体组件之前,我们先了解Spring WebApplicationContext。
黄规速
2022/04/14
5720
Spring学习笔记(4)一SpringMVC启动原理和WebApplicationContext
Spring--框架学习
为了后期方便各州测试,在/src下新建test测试目录,在其中新建java文件夹,里面写测试代码
花猪
2022/02/22
1.3K0
Spring--框架学习
Spring框架:第九章:Spring整合Web
Spring整合Web 在web工程中添加Spring的jar包。 Spring的核心包 spring-beans-4.0.0.RELEASE.jar spring-context-4.0.0.RELEASE.jar spring-core-4.0.0.RELEASE.jar spring-expression-4.0.0.RELEASE.jar aop包 spring-aop-4.0.0.RELEASE.jar spring-aspects-4.0.0.RELEASE.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar JDBC-ORM包 spring-jdbc-4.0.0.RELEASE.jar spring-orm-4.0.0.RELEASE.jar spring-tx-4.0.0.RELEASE.jar Spring的web整合包 spring-web-4.0.0.RELEASE.jar 测试包 spring-test-4.0.0.RELEASE.jar
Java廖志伟
2022/09/28
2700
Spring框架:第九章:Spring整合Web
Spring笔记(4)
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/115198.html原文链接:https://javaforall.cn
全栈程序员站长
2021/12/23
1990
Spring MVC快速体验
Spring MVC 快速体验的五个步骤: 1. 配置web.xml 2. 创建Spring配置文件applicationContext.xml 3. 创建model 4. 创建controller 5. 创建视图文件userlist.jsp 6. 创建Servlet配置文件Spring-servlet.xml
栋先生
2018/09/29
5120
Spring MVC快速体验
Spring的一些零碎知识点整理
要想在Web工程中配置Spring,首先需要在工程加入spring-web包,我这里使用的是maven的web工程,pom.xml配置文件配置的依赖如下:
端碗吹水
2020/09/23
2630
Spring的一些零碎知识点整理
Spring框架:第九章:Spring整合Web
Spring整合Web 在web工程中添加Spring的jar包。 Spring的核心包 spring-beans-4.0.0.RELEASE.jar spring-context-4.0.0.RELEASE.jar spring-core-4.0.0.RELEASE.jar spring-expression-4.0.0.RELEASE.jar aop包 spring-aop-4.0.0.RELEASE.jar spring-aspects-4.0.0.RELEASE.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar JDBC-ORM包 spring-jdbc-4.0.0.RELEASE.jar spring-orm-4.0.0.RELEASE.jar spring-tx-4.0.0.RELEASE.jar Spring的web整合包 spring-web-4.0.0.RELEASE.jar 测试包 spring-test-4.0.0.RELEASE.jar
马克社区
2022/09/21
1950
监听器获取spring配置文件创建的对象
文章目录 1. 监听器获取spring配置文件创建的对象 1.1. 前提 1.2. 准备 1.3. 实现 1.4. 参考文章 监听器获取spring配置文件创建的对象 前提 我们在使用监听器的时候,会用到spring配置文件创建的对象,那么我们不能像其他的类中直接使用@Resource或者@AutoWired自动注入对象,那么我们如何获取对象呢 比如我们在缓存数据的时候,就是在容器启动的时候读取数据库中的信息缓存在ServletContext中,那么我们肯定需要调用Service中的对象来获取数据库中的
爱撒谎的男孩
2019/12/31
1K0
1 SpringMVC
Spring MVC 随着Spring3.0 的发布已经成为一个最优秀Spring 的MVC框架,特点有 MVC 设计模型、请求驱动类型的 轻量级 web框架
收心
2022/01/17
2460
1 SpringMVC
Spirng集成web环境SpringMVC学习笔记
在Web项目中,Spring提供了一个监听器ContextLoaderListener启动时加载Spring的配置文件,创建应用上下文对象ApplicationContext,在将其存储到最大的域servletContext域中,提供了一个客户端工具WebApplicationContextUtils供使用者获得应用上下文对象。
咕咕星
2020/12/29
6220
动力节点Spring框架学习笔记-王鹤(五)Spring 与 Web
https://www.bilibili.com/video/BV1nz4y1d7uy
动力节点
2022/02/10
4680
Java面试系列24-spring(3)-配置文件相关问题
12.解释一下Dependency Injection(DI)和IOC(inversion of control)? 依赖注入DI是一个程序设计模式和架构模型, 一些时候也称作控制反转,尽管在技术上来
Java帮帮
2018/03/19
5980
Spring中很重要的ContextLoderListener类,你理解了吗?
由于ContextLoderListener实现了ServletContextListener,所以最先执行初始化方法contextInitianized进行初始化
用户5224393
2019/08/13
9990
谈谈ContextLoaderListener
每一个整合spring框架的项目中,总是不可避免地要在web.xml中加入这样一段配置。
一觉睡到小时候
2019/07/04
1.8K0
相关推荐
Spring整合javaweb的基本步骤简单记录
更多 >
LV.1
这个人很懒,什么都没有留下~
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验