当前位置: 首页 > news >正文

Spring与Web环境的集成

1. ApplicationContext应用上下文获取方式

应用上下文对象是通过new ClasspathXmlApplicationContext(spring配置文件) 方式获取的,但是每次从容器中获得Bean时都要编写new ClasspathXmlApplicationContext(spring配置文件) ,这样的弊端是配置文件加载多次,应用上下文对象创建多次

在Web项目中,可以使用ServletContextListener监听Web应用的启动,我们可以在Web应用启动时,就加载Spring的配置文件,创建应用上下文对象ApplicationContext,在将其存储到最大的域servletContext域中,这样就可以在任意位置从域中获得应用上下文ApplicationContext对象了。
web.xml配置全局初始化参数

<!--全局初始化参数--><context-param><param-name>contextConfigLocation</param-name><param-value>applicationContext.xml</param-value></context-param><listener><listener-class>com.zhxd.listener.ContextLoaderListener</listener-class>
</listener>
  • 创建ServletContextListener
package com.zhxd.listener;import org.springframework.context.support.ClassPathXmlApplicationContext;import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;public class ContextLoaderListener implements ServletContextListener {public void contextInitialized(ServletContextEvent servletContextEvent) {ServletContext servletContext = servletContextEvent.getServletContext();//ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");//隐藏Spring配置文件String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext(contextConfigLocation);servletContext.setAttribute("app",app);System.out.println("容器创建完毕...");}public void contextDestroyed(ServletContextEvent servletContextEvent) {}
}
  • UserServlet.java
public class UserServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {ServletContext servletContext = req.getServletContext();ApplicationContext app = (ApplicationContext)servletContext.getAttribute("app");UserService userService = app.getBean(UserService.class);userService.save();}
}

优化上述程序,把上下文变量名字(app)隐藏:

  • 定义获取上下文对象工具类
package com.zhxd.listener;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import javax.servlet.ServletContext;
import javax.servlet.ServletContextListener;public class WebApplicationContextUtils {public static ApplicationContext getApp(ServletContext servletContext) {return (ApplicationContext) servletContext.getAttribute("app");}
}
  • 修改UserServlet.java
public class UserServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {ServletContext servletContext = req.getServletContext();ApplicationContext app = WebApplicationContextUtils.getApp(servletContext);UserService userService = app.getBean(UserService.class);userService.save();}
}

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

上面的分析不用手动实现,Spring提供了一个监听器ContextLoaderListener就是对上述功能的封装,该监听器内部加载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中,提供了一个客户端工具WebApplicationContextUtils供使用者获得应用上下文对象。

所以我们需要做的只有两件事:
① 在web.xml中配置ContextLoaderListener监听器(导入spring-web坐标)

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

② 使用WebApplicationContextUtils获得应用上下文对象ApplicationContext
配置web.xml

 <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>
package com.zhxd.web;import com.zhxd.service.UserService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;public class UserServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {ServletContext servletContext = req.getServletContext();WebApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);UserService userService = app.getBean(UserService.class);userService.save();}
}
http://www.lryc.cn/news/334380.html

相关文章:

  • 二叉树的遍历——bfs广度优先搜索
  • 飞鸟写作可靠吗 #职场发展#经验分享#经验分享
  • Java 实现自定义注解
  • 代码随想录Day48
  • Web 后台项目,权限如何定义、设置、使用:菜单权限、按钮权限 ts element-ui-Plus
  • ADB 操作命令及其详细用法
  • 类的函数成员(三):拷贝构造函数
  • C#操作MySQL从入门到精通(8)——对查询数据进行高级过滤
  • Centos 7 安装通过yum安装google浏览器
  • 题目:学习使用按位与 。
  • 逐步分解,一文教会你如何用 jenkins+docker 实现主从模式
  • WebSocket 对于手游的意义
  • 安卓APP的技术质量:如何提高
  • 二分查找 -- 力扣(LeetCode)第704题
  • Windows下如何确定虚函数在虚函数表中的位置
  • C++设计模式:观察者模式(三)
  • CentOS运行Py脚本报错illegal instruction故障处理
  • 软件设计师——1.备考提纲
  • [开源] 基于GRU的时间序列预测模型python代码
  • SQL SERVER 备份
  • 提示词专场:从调整提示改善与LLMs的沟通,到利用LLMs优化提示效果
  • 测开面经(pytest测试案例,接口断言,多并发断言)
  • Golang 开发实战day09 - package Scope
  • 24考研-东南大学916经验贴
  • 【AI面试】YOLO 如何通过 k-means 得到 anchor boxes的?Yolo、SSD 和 faster rcnn 的正负样本定义
  • MySQL高级篇(B-Tree、Btree)
  • Zookeeper脑裂解决方案
  • 常用日常脚本
  • Longan Pi 3H 开发板体验
  • SpringCloud Alibaba Sentinel 创建流控规则