Spring、SpringMVC和SpringBoot框架中那些容器

简介: Spring、SpringMVC和SpringBoot框架中那些容器

【1】SpringMVC和Tomcat

项目背景是SpringMVC部署在外部的Tomcat8.5.382中。这里我们配置DispatcherServlet的为DispatcherServlet


① ServletContext


如下图所示,这里获取到的全局上下文ServletContext是ApplicationContextFacade,是一个ApplicationContext的外观对象(也就是说这里应用了设计模式中的外观模式)。


ApplicationContextFacade有三个属性:classCache、objectCache和源目标对象context(ApplicationContext)。


源对象context(ApplicationContext) 就是我们平时使用的实际Tomcat应用上下文,其又拥有StandardContext的引用。我们servletContext.setAttribute("testAttr","testAttrValue");设置的属性就在attributes下。

92cdaba1f2c74fdea0cd5778dc43c8da.png


① attributes概览

这里可以看到我们的SpringMVC容器以

org.springframework.web.servlet.FrameworkServlet.CONTEXT.DispatcherServlet为键存储在ApplicationContext中的attributes里面。


② attributes中的org.apache.catalina.jsp_classpath

也就是我们常说的classpath,这里我们以分号换行分隔展示:

# 本地# 本地tomcat路径下的
/D:/softintall/apache-tomcat-8.5.38/lib/;
/D:/softintall/apache-tomcat-8.5.38/lib/annotations-api.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/catalina-ant.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/catalina-ha.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/catalina-storeconfig.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/catalina-tribes.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/catalina.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/ecj-4.6.3.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/el-api.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/jasper-el.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/jasper.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/jaspic-api.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/jsp-api.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/servlet-api.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/tomcat-api.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/tomcat-coyote.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/tomcat-dbcp.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/tomcat-i18n-es.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/tomcat-i18n-fr.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/tomcat-i18n-ja.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/tomcat-i18n-ru.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/tomcat-jdbc.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/tomcat-jni.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/tomcat-util-scan.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/tomcat-util.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/tomcat-websocket.jar;
/D:/softintall/apache-tomcat-8.5.38/lib/websocket-api.jar;
/D:/softintall/apache-tomcat-8.5.38/bin/bootstrap.jar;
/D:/softintall/apache-tomcat-8.5.38/bin/tomcat-juli.jar;
# idea路径下的
/C:/Users/12746/.IntelliJIdea2018.2/system/captureAgent/debugger-agent.jar;
# jre路径下的
/C:/Program Files/Java/jdk1.8.0_101/jre/lib/ext/access-bridge-64.jar;
/C:/Program Files/Java/jdk1.8.0_101/jre/lib/ext/cldrdata.jar;
/C:/Program Files/Java/jdk1.8.0_101/jre/lib/ext/dnsns.jar;
/C:/Program Files/Java/jdk1.8.0_101/jre/lib/ext/jaccess.jar;
/C:/Program Files/Java/jdk1.8.0_101/jre/lib/ext/jfxrt.jar;
/C:/Program Files/Java/jdk1.8.0_101/jre/lib/ext/localedata.jar;
/C:/Program Files/Java/jdk1.8.0_101/jre/lib/ext/nashorn.jar;
/C:/Program Files/Java/jdk1.8.0_101/jre/lib/ext/sunec.jar;
/C:/Program Files/Java/jdk1.8.0_101/jre/lib/ext/sunjce_provider.jar;
/C:/Program Files/Java/jdk1.8.0_101/jre/lib/ext/sunmscapi.jar;
/C:/Program Files/Java/jdk1.8.0_101/jre/lib/ext/sunpkcs11.jar;
/C:/Program Files/Java/jdk1.8.0_101/jre/lib/ext/zipfs.jar


② SpringMVC容器XmlWebApplicationContext

这里项目创建的SpringMVC容器是XmlWebApplicationContext,上面我们提到我们的SpringMVC容器以org.springframework.web.servlet.FrameworkServlet.CONTEXT.DispatcherServlet为键存储在ApplicationContext中的attributes里面,值是WebApplicationContext for namespace 'DispatcherServlet-servlet', started on Thu Oct 07 11:00:54 CST 2021

org.springframework.web.servlet.FrameworkServlet.CONTEXT.DispatcherServlet=
WebApplicationContext for namespace 'DispatcherServlet-servlet', started on Thu Oct 07 11:00:54 CST 2021

fb16a4f979434f558d3dd1166bd61599.png


① XmlWebApplicationContext对象概览

  • 其拥有servletContext引用;
  • 其拥有servletConfig引用;

namespace为DispatcherServlet-servlet;

configLocations定义了配置文件路径,这里为classpath:springMVC.xml;

displayName=WebApplicationContext for namespace 'DispatcherServlet-servlet';

这里parent=null;


下图中mvc-attr是org.springframework.web.servlet.FrameworkServlet.CONTEXT.+servletName

c8931770fcfe4f6d8079301caaa9eba0.png


【2】SSM项目部署在外部Tomcat

这里我们配置DispatcherServlet的为springmvc。


① ServletContext

这里attributes中多了spring容器:

org.springframework.web.context.WebApplicationContext.ROOT=
Root WebApplicationContext: startup date [Fri Oct 08 20:49:42 CST 2021]; root of context hierarchy

Spring容器对象概览

  • id=org.springframework.web.context.WebApplicationContext:;
  • displayName=Root WebApplicationContext
  • parent=null;
  • configLocations=/WEB-INF/classes/spring/applicationContext-*.xml

springmvc容器以下面键值对储存在attributes中

org.springframework.web.servlet.FrameworkServlet.CONTEXT.springmvc=
WebApplicationContext for namespace 'springmvc-servlet': startup date [Fri Oct 08 20:49:48 CST 2021]; parent: Root WebApplicationContext


如下图所示,SpringMVC容器的parent属性指向了Spring容器。

也就是说,此时Tomcat的ServletContextattributes中存放了Spring容器SpringMVC容器。其中SpringMVC容器的parent指向Spring容器


【3】SpringBoot下内置Tomcat

① ServletContext

这里需要注意的是此时attributes有两个key对应同一个容器对象:

org.springframework.web.context.WebApplicationContext.ROOT=
org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@a668c39, started on Fri Oct 08 21:35:45 CST 2021
org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcherServlet=
org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@a668c39, started on Fri Oct 08 21:35:45 CST 2021

也就是说当前SpringBoot内置Tomcat环境下,只有ServletContext和AnnotationConfigServletWebServerApplicationContext。此时不存在Spring容器、SpringMVC容器,只有SpringBoot的容器(也可以理解为应用容器)

关于SpringMVC容器存储在ServletContext中的键

// 返回ServletContext 中 储存 servlet's WebApplicationContext的属性名
public String getServletContextAttributeName() {
  return SERVLET_CONTEXT_PREFIX + getServletName();
}
// SERVLET_CONTEXT_PREFIX  如下
public static final String SERVLET_CONTEXT_PREFIX = FrameworkServlet.class.getName() + ".CONTEXT.";
getServletName()就是你指定的DispatcherServlet的名字,默认为dispatcherServlet


目录
相关文章
|
1天前
|
消息中间件 存储 Java
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ——ActiveMQ安装
本教程介绍ActiveMQ的安装与基本使用。首先从官网下载apache-activemq-5.15.3版本,解压后即可完成安装,非常便捷。启动时进入解压目录下的bin文件夹,根据系统选择win32或win64,运行activemq.bat启动服务。通过浏览器访问`http://127.0.0.1:8161/admin/`可进入管理界面,默认用户名密码为admin/admin。ActiveMQ支持两种消息模式:点对点(Queue)和发布/订阅(Topic)。前者确保每条消息仅被一个消费者消费,后者允许多个消费者同时接收相同消息。
12 0
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ——ActiveMQ安装
|
1天前
|
NoSQL Java 关系型数据库
微服务——SpringBoot使用归纳——Spring Boot 中集成Redis——Redis 介绍
本文介绍在 Spring Boot 中集成 Redis 的方法。Redis 是一种支持多种数据结构的非关系型数据库(NoSQL),具备高并发、高性能和灵活扩展的特点,适用于缓存、实时数据分析等场景。其数据以键值对形式存储,支持字符串、哈希、列表、集合等类型。通过将 Redis 与 Mysql 集群结合使用,可实现数据同步,提升系统稳定性。例如,在网站架构中优先从 Redis 获取数据,故障时回退至 Mysql,确保服务不中断。
14 0
微服务——SpringBoot使用归纳——Spring Boot 中集成Redis——Redis 介绍
|
1天前
|
安全 Java Apache
微服务——SpringBoot使用归纳——Spring Boot中集成 Shiro——Shiro 身份和权限认证
本文介绍了 Apache Shiro 的身份认证与权限认证机制。在身份认证部分,分析了 Shiro 的认证流程,包括应用程序调用 `Subject.login(token)` 方法、SecurityManager 接管认证以及通过 Realm 进行具体的安全验证。权限认证部分阐述了权限(permission)、角色(role)和用户(user)三者的关系,其中用户可拥有多个角色,角色则对应不同的权限组合,例如普通用户仅能查看或添加信息,而管理员可执行所有操作。
20 0
|
1天前
|
安全 Java 数据安全/隐私保护
微服务——SpringBoot使用归纳——Spring Boot中集成 Shiro——Shiro 三大核心组件
本课程介绍如何在Spring Boot中集成Shiro框架,主要讲解Shiro的认证与授权功能。Shiro是一个简单易用的Java安全框架,用于认证、授权、加密和会话管理等。其核心组件包括Subject(认证主体)、SecurityManager(安全管理员)和Realm(域)。Subject负责身份认证,包含Principals(身份)和Credentials(凭证);SecurityManager是架构核心,协调内部组件运作;Realm则是连接Shiro与应用数据的桥梁,用于访问用户账户及权限信息。通过学习,您将掌握Shiro的基本原理及其在项目中的应用。
16 0
|
1天前
|
消息中间件 Java 微服务
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ——发布/订阅消息的生产和消费
本文详细讲解了Spring Boot中ActiveMQ的发布/订阅消息机制,包括消息生产和消费的具体实现方式。生产端通过`sendMessage`方法发送订阅消息,消费端则需配置`application.yml`或自定义工厂以支持topic消息监听。为解决点对点与发布/订阅消息兼容问题,可通过设置`containerFactory`实现两者共存。最后,文章还提供了测试方法及总结,帮助读者掌握ActiveMQ在异步消息处理中的应用。
16 0
|
1天前
|
消息中间件 网络协议 Java
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ——ActiveMQ集成
本文介绍了在 Spring Boot 中集成 ActiveMQ 的详细步骤。首先通过引入 `spring-boot-starter-activemq` 依赖并配置 `application.yml` 文件实现基本设置。接着,创建 Queue 和 Topic 消息类型,分别使用 `ActiveMQQueue` 和 `ActiveMQTopic` 类完成配置。随后,利用 `JmsMessagingTemplate` 实现消息发送功能,并通过 Controller 和监听器实现点对点消息的生产和消费。最后,通过浏览器访问测试接口验证消息传递的成功性。
12 0
|
1天前
|
消息中间件 Java API
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ—— JMS 和 ActiveMQ 介绍
本文介绍如何在Spring Boot中集成ActiveMQ,首先阐述了JMS(Java消息服务)的概念及其作为与具体平台无关的API在异步通信中的作用。接着说明了JMS的主要对象模型,如连接工厂、会话、生产者和消费者等,并指出JMS支持点对点和发布/订阅两种消息类型。随后重点讲解了ActiveMQ,作为Apache开源的消息总线,它完全支持JMS规范,适用于异步消息处理。最后,文章探讨了在Spring Boot中使用队列(Queue)和主题(Topic)这两种消息通信形式的方法。
14 0
|
1天前
|
NoSQL Java API
微服务——SpringBoot使用归纳——Spring Boot 中集成Redis——Spring Boot 集成 Redis
本文介绍了在Spring Boot中集成Redis的方法,包括依赖导入、Redis配置及常用API的使用。通过导入`spring-boot-starter-data-redis`依赖和配置`application.yml`文件,可轻松实现Redis集成。文中详细讲解了StringRedisTemplate的使用,适用于字符串操作,并结合FastJSON将实体类转换为JSON存储。还展示了Redis的string、hash和list类型的操作示例。最后总结了Redis在缓存和高并发场景中的应用价值,并提供课程源代码下载链接。
14 0
|
1天前
|
NoSQL Java Redis
微服务——SpringBoot使用归纳——Spring Boot 中集成Redis——Redis 安装
本教程介绍在 VMware 虚拟机(CentOS 7)或阿里云服务器中安装 Redis 的过程,包括安装 gcc 编译环境、下载 Redis(官网或 wget)、解压安装、修改配置文件(如 bind、daemonize、requirepass 等设置)、启动 Redis 服务及测试客户端连接。通过 set 和 get 命令验证安装是否成功。适用于初学者快速上手 Redis 部署。
10 0
|
1天前
|
Java 微服务 Spring
微服务——SpringBoot使用归纳——Spring Boot中使用拦截器——拦截器使用实例
本文主要讲解了Spring Boot中拦截器的使用实例,包括判断用户是否登录和取消特定拦截操作两大场景。通过token验证实现登录状态检查,未登录则拦截请求;定义自定义注解@UnInterception实现灵活取消拦截功能。最后总结了拦截器的创建、配置及对静态资源的影响,并提供两种配置方式供选择,帮助读者掌握拦截器的实际应用。
12 0

热门文章

最新文章