BeanFactory到WebApplicationContext的结构 以及bean和spring容器的关系
BeanFactory: Ioc 容器
ApplicationContext: Spring容器
WebApplicationContext需要ServletContext实例,也就是说它必须在拥有Web 容器的
前提下才能完成启动的工作。
Spring分别提供了用于启动WebApplicationContext的 Servlet和 Web容器监听器: org.springframework.web.context.ContextLoaderServlet;
org.springframework.web.context.ContextLoaderListener。
两者的内部都实现了启动 WebApplicationContext 实例的逻辑,我们只要根据 Web 容
器的具体情况选择两者之一,并在web.xml中完成配置就可以了。
1. 使用 ContextLoader Listener
<!确定配置文件的位置--〉
<context-param>
<param-name>contextConfigLocation</param-name>
<!--此处可以列出多个Spring 的 XML 配置文件→
<param-value>/WEB-INF/daoContext.xml/WEB-iNF/applicationContext.xml</param-value>
</context-param>
<!-- 应用启动时,自动加载listener,该 listener会读取上面确定的XML配置文件。
然后创建ApplicationContext实例--〉
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
ContextLoaderListener 通过 Web 容器上下文参数 contextConfigLocation 获取 Spring 配
置文件的位置。用户可以指定多个配置文件,用逗号、空格或冒号分隔均可。对于未带资
源类型前缀的配置文件路径, WebApplicationContext默认这些路径相对于Web的部署根路
径。当然,我们可以采用带资源类型前缀的路径配置,如classpath:spring/*.xml,classpath:spring/*/*.xml,
和上面的配置是等效的。
2. 使用 ContextLoaderServlet
<servlet>
<!--确定Servlet 的名-->
<servlet-name>context</servlet-name><!--确定 Servlet对应的类--〉
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<!--确定Servlet的启动级别--〉
<load-on-startup>l</load-on-startup></servlet>
采用这种方式时,应将context 的启动级别设成最小,即最优先启动。因为ApplicationContext是整个应用的核心。
注意:在两种启动方式中,推荐采用第一种。因为根据Servlet2.4规范, listener比Servlet优先启动;关键问题是有些容器并不支持Serlet2.4规范,即不支持listener。支持 listener的容器有:
ApacheTomcat4.x 及更高版本。
Jetty4.x及更高版本。
Resin 2.1.8 及更高版本。
Orion2.0.2及更高版本。
BEAWebLogic8.1 SP3
不支持 listener 的容器有:
BEAWebLogicupto 8.1 SP2 及更低版本。
IBMWebSphere 5.x 及更低版本。
OracleOC4J9.0.3 及更低版本。
使用Spring容器以后,程序各元素之间的关系图:
Bean配置信息:定义了Bean的实现和依赖关系。
Spring容器根据 Bean配置信息在容器内部建立Bean注册表。
3.使用ContextLoaderPlugIn
上面我们介绍了WebApplicationContext在Servlet容器中初始化的原理,一般的Web应用就可以轻松的使用了,但是,随着Struts的广泛应用,把Struts和Spring整个起来,是一个需要面对的问题,Spring本身也提供了Struts的相关类,主要使用的有org.springframework.web.struts.ActionSupport,我们只要把自己的Action继承自ActionSupport,就是可以调用ActionSupport中getWebApplicationContext()的方法取出WebApplicationContext,但这样一来在Action中,需要取得业务逻辑的地方都要getBean,看上去不够简洁,所以Spring又提供了另一个方法,用org.springframework.web.struts.ContextLoaderPlugIn,这是一个Struts的Plug,在Struts启动时加载,对于Action,可以像管理Bean一样来管理,在struts-config.xml中Action的配置变成类似下面的样子
<action attribute="aForm" name="aForm" path="/aAction" scope="request"  type="org.springframework.web.struts.DelegatingActionProxy">
  <forward name="forward" path="forward.jsp" />
</action>
注意type变成了org.springframework.web.struts.DelegatingActionProxy,之后我们需要建立action-servlet.xml这样的文件,action-servlet.xml符合Spring的spring-beans.dtd标准,在里面定义类似下面的
<bean name="/aAction" class="com.web.action.Aaction" singleton="false">
  <property name="businessService">
    <ref bean="businessService"/>
  </property>
</bean>
com.web.action.Aaction是Action的实现类,businessService是需要的业务逻辑,Spring会把businessService注入到Action中,在Action中只要写businessService的get和set方法就可以了,还有一点,action的bean是singleton="false",即每次新建一个实例,这也解决了Struts中Action的线程同步问题,具体过程是当用户做“/aAction”的HTTP请求(当然应该是“/aAction.do”),Struts会找到这个Action的对应类org.springframework.web.struts.DelegatingActionProxy,DelegatingActionProxy是个代理类,它会去找action-servlet.xml文件中“/aAction”对应的真正实现类,然后把它实例化,同时把需要的业务对象注入,然后执行Action的execute方法。
使用了ContextLoaderPlugIn,在struts-config.xml中变成类似这样配置
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
  <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml" />
</plug-in>
而在web.xml中不再需要ContextLoaderListener或是ContextLoaderServlet。
说到这里不知道大家会不会有这样的问题,如果使用ContextLoaderPlugIn,如果我们有些程序是脱离Struts的Action环境,我们怎么处理,比如我们要自定义标记库,在标记库中,我们需要调用Spring管理的业务层逻辑对象,这时候我们就很麻烦,因为只有在action中动态注入业务逻辑,其他我们似乎不能取得Spring的WebApplicationContext。
别急,我们还是来看一下ContextLoaderPlugIn的源码(源码不再贴出),我们可以发现,原来ContextLoaderPlugIn仍然是把WebApplicationContext放在ServletContext中,只是这个KEY不太一样了,这个KEY值为ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX+ModuleConfig.getPrefix()(具体请查看源代码),这下好了,我们知道了WebApplicationContext放在哪里,只要我们在Web应用中能够取到ServletContext也就能取到WebApplicationContext了:)
BeanFactory到WebApplicationContext的结构 以及bean和spring容器的关系的更多相关文章
- 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean
		7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ... 
- 【Spring学习笔记-3.1】让bean获取spring容器上下文(applicationContext.xml)
		*.hl_mark_KMSmartTagPinkImg{background-color:#ffaaff;}*.hl_mark_KMSmartTagBlueImg{background-color:# ... 
- spring4.1.8扩展实战之六:注册bean到spring容器(BeanDefinitionRegistryPostProcessor接口)
		本章是<spring4.1.8扩展实战>系列的第六篇,目标是学习如何通过自己写代码的方式,向spring容器中注册bean: 原文地址:https://blog.csdn.net/boli ... 
- spring boot: spring Aware的目的是为了让Bean获得Spring容器的服务
		Spring Aware的目的是为了让Bean获得Spring容器的服务 //获取容器中的bean名称import org.springframework.beans.factory.BeanName ... 
- 基于ImportBeanDefinitionRegistrar和FactoryBean动态注入Bean到Spring容器中
		基于ImportBeanDefinitionRegistrar和FactoryBean动态注入Bean到Spring容器中 一.背景 二.实现方案 1.基于@ComponentScan注解实现 2.基 ... 
- [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间
		本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ... 
- bean获取Spring容器
		Person.java public class Person implements ApplicationContextAware{ private String name; private int ... 
- 手动注入bean到spring容器
		ApplicationContext applicationContext = SpringContextUtils.getApplicationContext(); //将applicationCo ... 
- Spring MVC 了解WebApplicationContext中特殊的bean类型
		Spring MVC 了解WebApplicationContext中特殊的bean类型 Spring的DispatcherServlet使用了特殊的bean来处理请求.渲染视图等,这些特定的bean ... 
随机推荐
- HTML_2
			html图像 <img>标签可以在网页上插入一张图片,它是独立使用的标签,通过‘src’属性定义图片的地址(可为绝对路径也可为相对路径),通过‘alt’属性定义图片加载时显示的文字,以及对 ... 
- xshell5 上传下载命令
			借助XShell,使用linux命令sz可以很方便的将服务器上的文件下载到本地,使用rz命令则是把本地文件上传到服务器. yum -y install lrzsz 其中,对于sz和rz的理解与记忆我用 ... 
- VC-基础:vs2010快捷键
			F12: 转到所调用过程或变量的定义 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL + SHIFT + O打开项目 CTRL + S ... 
- Forbidden  You don't have permission to access /phpStudyTest/application/index/controller/Index.php on this server.
			发生情况:将thinkPHP从官网上下了 http://thinkphp.cn 然后安装了phpstudy和PHPstorm,并将thinkPHP解压到www路径下 在用PHPstorm打开 thi ... 
- python-判断alter是否存在
			from selenium import webdriver import time from selenium.webdriver.support.ui import WebDriverWait f ... 
- The expected type was 'System.Int64' but the actual value was null.”
			System.InvalidOperationException:“An exception occurred while reading a database value for property ... 
- 性能优化之MySQL优化(慕课)
			MySQL数据库优化 1-1MySQL优化简介 数据库优化的目的 避免出现页面访问错误 由于数据库连接timeout产生5XX错误 由于慢查询造成页面无法加载 由于阻塞造成数据无法提交 增加数据库的稳 ... 
- percona-toolkit工具使用介绍
			percona-toolkit工具使用介绍 1. pt-heartbeat 1.1 pt-heartbeat 原理 1.2 pt-heartbeat 主要参数介绍 1.3 pt-heartbeat 实 ... 
- mysql锁机制(转载)
			锁是计算机协调多个进程或线程并发访问某一资源的机制 .在数据库中,除传统的 计算资源(如CPU.RAM.I/O等)的争用以外,数据也是一种供许多用户共享的资源.如何保证数据并发访问的一致性.有效性是所 ... 
- JavaScript取出字符串中括号里的内容
			/** * 取出中括号内的内容 * @param text * @returns {string} */ export function getBracketStr(text) { let resul ... 
