WebApplicationContext定义了一个常 ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,在上线文启动的时候。 ?...wac = (WebApplicationContext)servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE...通常情况下,在web.xml中配置自启动的Servlet或者定义Web容器监听器(ServletContextListener),借助二者中的任何一个,就可以完成Spring Web应用上下文的启动工作...注意: 所有的版本的Web容器都可以定义自启动的Servlet,但是只有Servlet2.3及以上版本的Web容器才支持Web容器监听器 ---- Spring分别提供了用于启动WebApplicationContext...当然也可以采用带资源类型前缀的路径配置,如 classpath:spring-context.xml . ---- WebApplicationContext中的日志文件的两种配置方式 由于WebApplicationContext
这一篇我们介绍一下WebApplicationContext接口的知识,看WebApplicationContext接口的名字我们应该可以得知,这个接口就是专门为Web应用准备的,它允许从Web根目录的路径中装载配置文件并完成初始化工作...我们可以从WebApplicationContext接口中获取到ServletContext的引用。...在spring中我们可以使用ContextLoaderListener监听器启动WebApplicationContext。 下面我们看一下监听器的具体配置,下面配置是在web.xml中的配置的。...在spring中我们知道IOC容器是分为父子容器的。子容器可以访问父容器的对象,但父容器不能访问子容器的对象。在容器中对象的id必须是唯一的,但子容器可以拥有一个和父容器id相同的对象。...这样做的好处是我们可以通过变成的方式,为一个已经存在的容器添加特殊的子容器,以为容器提供一些额个的功能。在spring中最典型的父子容器的应用就是springMVC。
Servlet上下文通过web.xml文件获取所配置的contextConfigLocation、contextClass等参数,定位Spring配置文件资源,调用Spring Web容器的刷新的refresh...servletContext.log("Closing Spring root WebApplicationContext"); try { //Spring Web...上下文中Spring Web根上下文属性 servletContext.removeAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE...接口 public interface WebApplicationContext extends ApplicationContext { //用于定义在Servlet上下文中存储Spring...上下文环境的Bean名称 String SERVLET_CONTEXT_BEAN_NAME = "servletContext"; //web.xml文件中的配置Spring初始化参数的属性
如果无法找到适合的配置文件,Spring将无法初始化。 Spring根据指定的文件创建WebApplicationContext对象,并将其保存在Web应用的ServletContext中。...首先第一段注释是对这个类的描述: 这个启动监听器是用开启和关闭Spring的root的,这里他用了root而不是容器。...在Spring3.1中,ContextLoaderListener支持通过ContextLoaderListener(WebApplicationContext)这个构造方法向应用上下文中注入root(...也就是Spring的容器),这样可以以编程的方式来配置Servlet 3.0+的环境。...root WebApplicationContext"); if (logger.isInfoEnabled()) { logger.info("Root WebApplicationContext
spring的启动是建筑在servlet容器之上的,所有web工程的初始位置就是web.xml,它配置了servlet的上下文(context)和监听器(Listener) web.xml 的配置,这个servlet主要用于前端控制,这是springMVC的基础--> servlet> servlet-name>service_dispatcher...root WebApplicationContext"); if (logger.isInfoEnabled()) { logger.info("Root WebApplicationContext...加载对应的spring配置文件中的Bean。 将WebApplicationContext放入ServletContext(Java Web的全局变量)中。...每个Servlet拥有自己的上下文,也会共享parent的上下文。 下期讲解refresh()做了什么,以及Spring Boot的启动原理,敬请观看,谢谢。。。
WebApplicationContext是实现ApplicationContext接口的子类。是专门为WEB应用准备的。 作用: 1.它允许从相对于Web根目录的路径中加载配置文件完成初始化工作。...从WebApplicationContext中可以获取ServletContext引用,整个Web应用上下文对象将作为属性放置在ServletContext中, 以便Web应用环境可以访问Spring...2.WebApplicationContext还为Bean提供了三个新的作用域,request、session和globalsession。
: ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE=WebApplicationContext.class.getName() + ".ROOT" 根上下文的名称...root WebApplicationContext"); if (logger.isInfoEnabled()) { logger.info("Root WebApplicationContext...构建Root Context时序图: protected WebApplicationContext initWebApplicationContext() { //先从web容器的ServletContext...(getServletContext()); //创建Spring MVC的上下文,并将根上下文作为起双亲上下文 wac = createWebApplicationContext...(); //将Spring MVC的Context放置到ServletContext中 getServletContext().setAttribute(
准备工作(根据自己的要求调整) Log4j 1.2.17 Spring 4.2.5.RELEASE Maven 3 Tomcat 9 Eclipse neon版本 注意 默认情况下,Spring(...Spring-core)使用JCL(commons-logging)记录日志,JCL在运行时可以发现其他classpath下的日志框架 集成log4j,你需要: 将log4j.jar添加到classpath...:33:07 INFO WelcomeController:20 - 访问首页 在指定的目录E:\eclipse-log下生成了日志文件spring-mvc-log4j.log,打开看到内容: 2016...-10-27 00:44:50.489 INFO ContextLoader:305 - Root WebApplicationContext: initialization started 2016...ContextLoader:345 - Root WebApplicationContext: initialization completed in 1536 ms 2016-10-27 00:44:
WebApplicationContext 实例会在应用启动之后由Spring实例化并维护,而平常在学习的时候也往往不会自己去实例化 WebApplicationContext 对象,因为将因为部署到web...web项目的开发关键点在于让web容器初始化之后提醒Spring ApplicationContext 初始化,例如 tomcat 的 ServletContext 会维护一个 WebApplicationContext...> servlet-mapping> WebApplicationContext 的初始化调用链路:ContextLoaderListener.contextInitialized...所以呢,通过 web.xml 配置Spring MVC默认的上下文是: XmlWebApplicationContext 指定 WebApplicationContext 如果在 web.xml 中配置...参考 关于web.xml配置启动,Spring 的加载流程网络上资料很多,所以有可能会有很多重复的,选择一遍排版不错,写得相对完整的,编写时间比较新的:Spring MVC 启动过程源码分析。
[java][Servlet]Servlet 简介-Servlet 到 Spring MVC 的简化之路-Servlet/Tomcat/ Spring 之间的关系 温习一下这个传统的方式,jsp+JavaBeans...Servlet 到 Spring MVC 的简化之路 背景 Servlet和JSP是开发Java Web应用程序的两种基本技术,Spring MVC是Spring框架中用于开发Web应用的一个模块。...演进5:Spring Web模块 - Spring MVC 学过Servlet的朋友应该知道,当要使用Servlet完成的复杂的功能时,需要编写多个Servlet类,并且在web.xml进行注册,这对于完成复杂的...Servlet/Tomcat/ Spring 之间的关系 0.基础知识 在idea中打开servlet的源码: ?...Spring 任何Spring Web的entry point,都是servlet。
熟悉 Spring MVC 的启动过程,有助于我们理解相关文件配置的原理,深入理解 Spring MVC 的设计原理和执行过程。...>classpath:spring-mvc.xml servlet> servlet-mapping> servlet-name...(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) !...root WebApplicationContext"); if (logger.isInfoEnabled()) { logger.info("Root WebApplicationContext...至此,根 IoC 容器以及相关Servlet的子 IoC 容器已经配置完成,子容器中管理的 Bean 一般只被该Servlet使用,因此,其中管理的 Bean 一般是“局部”的,如 Spring MVC
上图中显示了2个WebApplicationContext实例,为了进行区分,分别称之为:Servlet WebApplicationContext(子容器)、Root WebApplicationContext...通过spring mvc中提供的DispatchServlet来加载配置,通常情况下,配置文件的名称为spring-servlet.xml。...启动的时候控制台应该会打印出“Root WebApplicationContext: initialization started” 我们拿着这个日志就能定位到代码了 public WebApplicationContext...; } servletContext.log("Initializing Spring root WebApplicationContext"); Log logger = LogFactory.getLog...这个是把包的扫描配置spring-servlet.xml中这个是可行的。
XXXAware XXXAware在Spring中该类接口用于通过Spring自动向XXXAware实现类中实现的setXXX(XXX xxx)方法中注入XXX对象,方便在实现类中调用XXX对象。...XXXCapable 实现XXXCapable接口后说明该实现类具有提供XXX的能力,当Spring需要XXX时会通过该类的getXXX方法来获取XXX对象。...获取Spring都得rootContext Spring默认将rootContext设置于ServletContext的属性中,属性名为ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE...= WebApplicationContext.class.getName() + ".ROOT"; 设置WebApplicationContext并根据情况调用onRefresh方法 设置WebApplicationContext...=org.springframework.web.servlet.support.SessionFlashMapManager 这里的默认设置并不是最优配置,也不是Spring推荐配置,只是为了防止没有配置
Spring MVC启动过程 接下来以一个常见的简单web.xml配置进行Spring MVC启动过程的分析,web.xml配置内容如下: Web...>classpath:spring-mvc.xml servlet> servlet-mapping> servlet-name...(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) !...root WebApplicationContext"); if (logger.isInfoEnabled()) { logger.info("Root WebApplicationContext...,键名为WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,可以通过以下两种方法获取 WebApplicationContext
SpringMVC 而在web项目中,我们一般都是使用的spring mvc,Spring Framework本身没有Web功能,Spring MVC使用WebApplicationContext类扩展...-- servlet定义:前端处理器,接受的HTTP请求和转发请求的类 --> servlet> servlet-name>courtservlet-name>...同时Spring MVC也对应的持有一个独立的Context,它是ROOT Context的子上下文。 对于这样的Context结构在Spring MVC中是如何实现的呢?...=WebApplicationContext.class.getName() + ".ROOT" 根上下文的名称 //PS : 默认情况下,配置文件的位置和名称是:DEFAULT_CONFIG_LOCATION...root WebApplicationContext"); if (logger.isInfoEnabled()) { logger.info("Root WebApplicationContext
SpringBoot单元测试spring单元测试之前在spring项目中使用单元测试时是使用注解@RunWith(SpringJUnit4ClassRunner.class)来进行的java 代码解读复制代码...来进行权限,这就给我们的测试带来了麻烦,可以使用spring-security-test依赖来进行测试xml 代码解读复制代码 org.springframework.security...,并提供一个mock servlet环境,使用该模式内嵌的servlet容器不会启动 MOCK(false), // 加载EmbeddedWebApplicationContext,并提供一个真实的...servlet环境,内嵌servlet容器启动,并监听一个随机端口 RANDOM_PORT(true), // 加载EmbeddedWebApplicationContext,并提供一个真实的servlet...环境,内嵌servlet容器启动,并监听一个定义好的接口 DEFINED_PORT(true), // 使用SpringApplication加载一个ApplicationContext,但不提供
WebApplicationContext的引用,但是并没有放进ServletContext容器哦 if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE...//放进ServletContext上下文,避免再次被初始化,也让我们能更加方便的获取到容器 servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE...,看到控制台log日志: Root WebApplicationContext: initialization completed in 75383 ms 就证明Spring根容器就初始化完成了。...配置多个DispatcherServletServlet 从本文中我们看到Spring的容器存在父子容器的。因此我们可以很容器的配置多个web子容器,然后父容器都是Root容器,这是被允许的。...,它是Spring4.0之后有的,只用于嵌入式的Servlet环境。
在《深入理解Spring系列之一:开篇》的示例代码中使用如下方式去加载Spring的配置文件并初始化容器。...ServletContext定义了一些方法方便Servlet和Servlet容器进行通讯,在一个web应用中所有的Servlet都公用一个ServletContext,Spring在和web应用结合使用的时候...,因为在一个应用中Spring容器只能有一个,所以需要校验,至于这个默认的名称为什么是WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE...这里配置了Spring默认的WebApplicationContext接口实现类XmlWebApplicationContext.java。...使用Spring的WebApplicationContextUtils工具类获取这个WebApplicationContext方式如下。
前言: 这是关于Spring的第三篇文章, 打算后续还会写入AOP 和Spring 事务管理相关的文章, 这么好的两个周末 都在看code了, 确实是有所收获, 现在就来记录一下....解决问题的方法很简单, 在web 启动的时候将applicationContext转到到servletContext中, 因为在web 应用中的所有servlet都共享一个servletContext对象...然而Spring容器底层已经为我们想到了这一点, 在spring-web-xxx-release.jar包中有一个 已经实现了ServletContextListener的类, 下面我们就来看一下这个类...servletContext中装载的applicationContext对象了: 那么这里又有一个问题, 装载时的key 是 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE...其实Spring为我们提供了一个工具类WebApplicationContextUtils, 接着我们先看下如何使用, 然后再去看下这个工具类的源码: WebApplicationContext applicationContext
在分析SpringMVC框架具体组件之前,我们先了解Spring WebApplicationContext。...web.xml中其他配置的servlet,为其初始化自己servlet的上下文信息servletContext,并加载其设置的配置信息和参数信息到该上下文中,将WebApplicationContext...所以最后的关系是ServletContext包含了WebApplicationContext,WebApplicationContext包含了其他的Servlet上下文环境。...WebApplicationContext扩展了ApplicationContext.在 WebApplicationContext中定义了一个常量 ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE...(ServletContext sc) { return getWebApplicationContext(sc, WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE