Spring父容器与子容器
在使用spring+springMVC的web工程中,我们一般会在web.xml中做如下配置:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
</context-param> <!-- spring context listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- spring mvc dispatcher servlet -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
这样就会产生两个spring容器,一个父容器,一个子容器。
父容器Root WebApplicationContext由ContextLoaderListener加载
子容器WebApplicationContext for namespace 'spring-servlet'由DispatcherServlet加载('spring-servlet'子容器的contextConfigLocation,web.xml中配置的)
从容器里面getBean的时候,先从本容器取,如果取不到再从父容器取
原码跟踪得到如下结果:
ContextLoaderListener:
beanFactory : org.springframework.beans.factory.support.DefaultListableBeanFactory@501af69e: defining beans [commonService,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,propertyConfigurer,messageSource,exceptionHandler,compositeFilter,dataSource,sqlSessionFactory,transactionManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,org.mybatis.spring.mapper.MapperScannerConfigurer#0,tokenClient,formTokenManager,formTokenAspect,formTokenPointCut,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,responseAspect,responsePointCut,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,multiViewResolver,excelView]; root of factory hierarchy
applicationContext:(对象地址:6be04fe1,父容器:null)
(org.springframework.web.context.support.XmlWebApplicationContext@6be04fe1) Root WebApplicationContext: startup date [Thu Feb 16 09:42:54 CST 2017]; root of context hierarchy
contextId : org.springframework.web.context.WebApplicationContext:
beanFactory.registerResolvableDependency(ApplicationContext.class, this);将父容器(根容器)注册到父容器的beanFactory中
DispatcherServlet:
ServletContext : org.apache.catalina.core.ApplicationContextFacade@78d9af50
applicationContext : ( 对象地址:8323ee8,父容器:ContextLoaderListener加载的 applicationContext,即 Root WebApplicationContext)
(org.springframework.web.context.support.XmlWebApplicationContext@8323ee8) WebApplicationContext for namespace 'spring-servlet': startup date [Thu Jan 01 08:00:00 CST 1970]; parent: Root WebApplicationContext
contextId : org.springframework.web.context.WebApplicationContext:/spring
beanFactory.registerResolvableDependency(ApplicationContext.class, this); 将子容器注册到子容器的beanFactory中
父容器的ListableBeanFactory中注册的bean:(由ContextLoadListener初始化的容器)
(java.lang.String[]) [commonService
initService
pageService
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
propertyConfigurer
messageSource
exceptionHandler
compositeFilter
dataSource
sqlSessionFactory
transactionManager
org.springframework.aop.config.internalAutoProxyCreator
org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0
org.springframework.transaction.interceptor.TransactionInterceptor#0
org.springframework.transaction.config.internalTransactionAdvisor
org.mybatis.spring.mapper.MapperScannerConfigurer#0
tokenClient
formTokenManager
formTokenAspect
formTokenPointCut
org.springframework.aop.aspectj.AspectJPointcutAdvisor#0
responseAspect
responsePointCut
org.springframework.aop.aspectj.AspectJPointcutAdvisor#1
multiViewResolver
excelView
org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor
org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor
ICommonDao
IPageDao]
子容器的ListableBeanFactory中注册的bean:(由DispatcherServlet初始化的容器)
(java.lang.String[]) [baseTestController
captchaController
errorPageTestController
excelExportController
exceptionHandlerTestController
paramProcessController
tokenTestController
validatorTestController
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.aop.config.internalAutoProxyCreator
mvcContentNegotiationManager
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0
org.springframework.format.support.FormattingConversionServiceFactoryBean#0
org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean#0
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0
mvcUriComponentsContributor
org.springframework.web.servlet.handler.MappedInterceptor#0
org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0
org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0
org.springframework.web.servlet.view.InternalResourceViewResolver#0]
补充:bean中如果依赖注入了ApplicationContext,那么这个bean中注入的容器为持有这个bean的容器
web工程,web.xml配置如上
application.xml加载除了Controller之外的bean
<!-- 加载@Component, @Service, @Repository -->
<context:component-scan base-package="com.cn.kvn.usage">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
spring-servlet.xml加载Controller bean
<!-- 加载@Controller -->
<context:component-scan base-package="com.cn.kvn.usage.controller" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
那么在Controller bean中注入ApplicationContext拿到的是子容器,而在Service bean中注入ApplicationContext拿到的是父容器
@Controller
@RequestMapping("/base")
public class BaseTestController { @Resource
ApplicationContext applicationContext; ......
} @Service
public class CommonService implements ICommonService{ @Resource
ApplicationContext applicationContext; .......
}
断点跟踪,在Display窗口中打出的信息如下:
this
(org.springframework.web.context.support.XmlWebApplicationContext) Root WebApplicationContext: startup date [Thu Feb 16 13:50:53 CST 2017]; root of context hierarchy
org.springframework.util.ObjectUtils.getIdentityHexString(this)
(java.lang.String) // 父容器的地址
this
(org.springframework.web.context.support.XmlWebApplicationContext) WebApplicationContext for namespace 'spring-servlet': startup date [Thu Feb 16 13:53:47 CST 2017]; parent: Root WebApplicationContext
org.springframework.util.ObjectUtils.getIdentityHexString(this)
(java.lang.String) 3be4f169 // 子容器的地址
applicationContext
(org.springframework.web.context.support.XmlWebApplicationContext) WebApplicationContext for namespace 'spring-servlet': startup date [Thu Feb 16 13:53:47 CST 2017]; parent: Root WebApplicationContext
org.springframework.util.ObjectUtils.getIdentityHexString(applicationContext)
(java.lang.String) 3be4f169 // Controller 中注入的ApplicationContext
applicationContext
(org.springframework.web.context.support.XmlWebApplicationContext) Root WebApplicationContext: startup date [Thu Feb 16 13:50:53 CST 2017]; root of context hierarchy
org.springframework.util.ObjectUtils.getIdentityHexString(applicationContext)
(java.lang.String) // Service 中注入的ApplicationContext
Spring父容器与子容器的更多相关文章
- 查看Spring MVC 父容器和子容器的对象的实例
话不多说,直接上案例 package com.oukele.web; import org.springframework.beans.factory.annotation.Autowired; im ...
- 结合源码浅谈Spring容器与其子容器Spring MVC 冲突问题
容器是整个Spring 框架的核心思想,用来管理Bean的整个生命周期. 一个项目中引入Spring和SpringMVC这两个框架,Spring是父容器,SpringMVC是其子容器,子容器可以看见父 ...
- 父容器根据子容器高度自适应:设置父容器 height:100%;overflow:hidden;
父容器根据子容器高度自适应:设置父容器 height:100%;overflow:hidden;
- Spring MVC 根容器和子容器
整合 spring mvc 根容器和子容器 public class TestWebInitializer extends AbstractAnnotationConfigDispatcherServ ...
- Spring - 父容器与子容器
一.Spring容器(父容器) 1.Mapper代理对象 2.Service对象 二.Springmvc(前端控制器)(子容器) Controller对象 1.标准的配置是这样的:Con ...
- .NET同一个页面父容器与子容器通信方案
主界面: 关键主页面代码: <div id="EditDiv"> <iframe src="javascript:void(0)" id=&q ...
- 【CSS】div父容器根据子容器大小自适应
Div即父容器不根据内容自动调节高度,我们看下面的代码: <div id="main"> <div id="content"></ ...
- html 父容器和子容器通信
通过拿到document对象下的window对象后执行对应的方法.
- SpringMVC——DispatcherServlet的IoC容器(Web应用的IoC容器的子容器)创建过程
在上一篇<Spring--Web应用中的IoC容器创建(WebApplicationContext根应用上下文的创建过程)>中说到了Web应用中的IoC容器创建过程.这一篇主要讲Sprin ...
随机推荐
- _mysql.c:29:20: error: Python.h: No such file or directory
在Centos系统中安装 pip install MySQL-python 提示: _mysql.c:29:20: error: Python.h: No such file or directory ...
- 【C】——pthread_mutex_lock
函数名 pthread_mutex_lock, pthread_mutex_trylock, pthread_mutex_unlock - lock and unlock a mutex SYNOPS ...
- ElasticSearch 深度分页解决方案 {"index":{"number_of_replicas":0}}
常见深度分页方式 from+size es 默认采用的分页方式是 from+ size 的形式,在深度分页的情况下,这种使用方式效率是非常低的,比如 from = 5000, size=10, es ...
- 关于源码输出,浏览器不解析Html标签
有时候根据需要我们需要看到浏览器上源码效果如: 但是我如果在html中输入 <a href = 'http://www.baidu.com'>百度</a>那么问题来了,总是显示 ...
- 为什么c++中,有时可以用类名直接访问非静态成员函数?
正规的C++语言标准目前(截止到C++14)应该还不支持这种调用方法.目前微软似乎在它的VC++中推行一种叫做C++/CLI的标准,有可能会支持这种调用,如果一定要用这种调用方法的话,还应该用VS20 ...
- vncserve安装配置 (转)
使用服务器时,利用远程桌面是非常方便的,否则需要跑到服务器机房操作非常的费事,或者需要远程操作机器是也可以使用,一般的操作系统都会带有远程桌面功能,但是不如第三方的的软件好用,对于Linux系统常用的 ...
- 第三百八十四节,Django+Xadmin打造上线标准的在线教育平台—路由映射与静态文件配置以及会员注册
第三百八十四节,Django+Xadmin打造上线标准的在线教育平台—路由映射与静态文件配置以及会员注册 基于类的路由映射 from django.conf.urls import url, incl ...
- 第三百二十二节,web爬虫,requests请求
第三百二十二节,web爬虫,requests请求 requests请求,就是用yhthon的requests模块模拟浏览器请求,返回html源码 模拟浏览器请求有两种,一种是不需要用户登录或者验证的请 ...
- (转)Live555单线程原理
1. 概述 在live555-Server库中,使用单线程实现了多用户请求视频数据,这似乎多线程才能实现的功能,并且用户请求视频数据各个流程衔接的都十分完美,其执行效率非常高. live555是如何实 ...
- EF5+MVC4系列(11)在主视图中用Html.RenderPartial调用分部视图(ViewDate传值);在主视图中按钮用ajax调用子action并在子action中使用return PartialView返回分布视图(return view ,return PartialView区别)
一:主视图中使用Html.RenderPartial来调用子视图(注意,这里是直接调用子视图,而没有去调用子Action ) 在没有使用母版页的主视图中(也就是设置了layout为null的视图中), ...