- 浏览: 3472761 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
wanglf1207:
EJB的确是个不错的产品,只是因为用起来有点门槛,招来太多人吐 ...
weblogic-ejb-jar.xml的元素解析 -
qwfys200:
总结的不错。
Spring Web Flow 2.0 入门 -
u011577913:
u011577913 写道也能给我发一份翻译文档? 邮件437 ...
Hazelcast 参考文档-4 -
u011577913:
也能给我发一份翻译文档?
Hazelcast 参考文档-4 -
songzj001:
DbUnit入门实战
(1)
org.springframework.web.context.ContextLoaderListener
这个类被定义为监听器,并读取在参数contextConfigLocation中定义的xml
文件,如果不设置contextConfigLocation的初始参数则默认会读取
WEB-INF路径下的
application.xml文件,如果需要自定义了另外的xml
则可以在contextConfigLocation下定义,ContextLoaderListener会读取这些XML文件并产生
WebApplicationContext对象,然后将这个对象放置在ServletContext的属性里,这样我们只要可以得到Servlet就可
以得到WebApplicationContext对象,并利用这个对象访问spring 容器管理的bean。
(2)解析ContextLoadListener的源代码
// 实现了servlet的ServletContextListener接口
public class ContextLoaderListener
implements ServletContextListener
{
private ContextLoader contextLoader;
// 这个是最重要的,利用contextLoader 来完成所有的工作
public void contextInitialized(ServletContextEvent event)
{
this.contextLoader = createContextLoader();
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
protected ContextLoader createContextLoader()
{
return new ContextLoader();
}
public ContextLoader getContextLoader()
{
return this.contextLoader;
}
public void contextDestroyed(ServletContextEvent event)
{
if (this.contextLoader != null)
this.contextLoader.closeWebApplicationContext(event.getServletContext());
}
}
下面看一下ContextLoader .initWebApplicationContext方法
public WebApplicationContext initWebApplicationContext(ServletContext servletContext)
throws IllegalStateException, BeansException
{
if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
throw new IllegalStateException("Cannot initialize context because
there is already a root application context present - check whether you
have multiple ContextLoader* definitions in your web.xml!");
}
long startTime = System.currentTimeMillis();
if (this.logger.isInfoEnabled())
this.logger.info("Root WebApplicationContext: initialization started");
servletContext.log("Loading Spring root WebApplicationContext");
try
{
ApplicationContext parent = loadParentContext(servletContext);
// 这里得到WebApplicationContext对象
,下面具体看一下这个方法是如何实现的
this.context = createWebApplicationContext(servletContext, parent);
// 将这个对象设置到
servletContext的属性里
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
if (this.logger.isInfoEnabled()) {
this.logger.info("Using context class [" +
this.context.getClass().getName() + "] for root WebApplicationContext");
}
if (this.logger.isDebugEnabled()) {
this.logger.debug("Published root WebApplicationContext [" +
this.context + "] as ServletContext attribute with name [" +
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
}
if (this.logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
this.logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
}
return this.context;
}
protected WebApplicationContext createWebApplicationContext(ServletContext servletContext, ApplicationContext parent)
throws BeansException
{
// 返回在web.xml 中参数contextClass自定义类对应的对象这个类,这个类实现了XmlWebApplicationContext
//
XmlWebApplicationContext继承了AbstractRefreshableWebApplicationContext类中定义的
方法protected String[] getConfigLocations()
,这个方法默认可以加载contextConfigLocation中定义的xml
文件,如果你重写了这个方法还可以在任意地方加载你想要的xml文件。
Class contextClass = determineContextClass(servletContext);
if (!(ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass))) {
throw new ApplicationContextException("Custom context class [" +
contextClass.getName() + "] is not of type
ConfigurableWebApplicationContext");
}
// 得到 WebApplicationContext
ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext)BeanUtils.instantiateClass(contextClass);
wac.setParent(parent);
wac.setServletContext(servletContext);
String configLocation = servletContext.getInitParameter("contextConfigLocation");
if (configLocation != null) {
wac.setConfigLocations(StringUtils.tokenizeToStringArray(configLocation, ",; \t\n"));
}
wac.refresh();
return wac;
}
发表评论
-
说明SOA监管(SOA Governance)实例(收录备查)
2012-12-19 11:35 1716SOA 已经不是单纯技术问 ... -
Injecting Spring Beans into Java Servlets
2012-11-01 10:21 1909If you are working in a Java ... -
用 HttpServletResponseWrapper 实现 Etag 过滤器
2012-07-09 16:58 3709原文出处:http://blog.chenlb.com/200 ... -
Eclipse Indigo - Cannot install Android ADT Plugin
2012-02-29 01:17 3837When I try to install the And ... -
Eclipse Indigo - Cannot install Android ADT Plugin
2012-02-29 01:13 1940When I try to install the And ... -
[转]mybatis下的分页,支持所有的数据库
2011-07-21 13:21 14788大 家都知道,mybatis的自带分页方法只是逻 ... -
Java framework for text- & console-based forms?
2011-07-21 01:06 1672charva jcurses JNA , ... -
JNA(Java Native Access)学习入门
2011-07-21 01:04 22524Java Native Access 项目 在 ... -
使用IntrospectorCleanupListener 解决quartz引起的内存泄漏
2011-04-20 11:59 13289"在服务器运行过程中,Spring不停的运行的计划任 ... -
DBCP代码研读以及就数据库连接失效的解决
2011-03-31 11:03 3728问题 网上很多评论说DBCP有很多BUG,但是都没有指明是什 ... -
Servlet3.0新功能: 异步处理
2010-12-06 15:22 3127J2EE 6和Glassfish 3V正式发 ... -
Servlet3.0引入的新特性
2010-12-06 15:20 3028Servlet3.0规范的新特性主要是为了3个目的: ... -
100個節點上運行群集亞馬遜EC2上Hazelcast
2010-12-03 23:59 3293本文的目的,適是给妳湮示的細節集群的100個節點。此湮示記錄, ... -
Spring Properties Reloaded
2010-12-02 14:54 4343Spring Properties Reloaded Som ... -
为spring2.5中的jpetstore增加perf4j监控
2010-09-02 13:51 2609perf4j是一款类似于log4j的性能检测工具. 它 ... -
语义网的学习资源大汇集(备忘)
2010-06-23 22:48 1692网上资源 http:/ ... -
使用 JOLAP 实现复杂分析查询
2010-06-06 13:42 1924Shashank Tiwari 在本文中对 ... -
HTML5 Canvas for Internet Explorer
2010-06-04 21:16 1830Canvascape http://www.benjoff ... -
大型网站架构演变和知识体系
2010-06-01 23:47 1913架构演变第一步:物 ... -
Ejb Con Spring
2010-05-20 17:46 2177Contenido [ocultar] 1 ...
相关推荐
Spring的监听器ContextLoaderListener的作用
web.xml中ContextLoaderListener的运行
java解决org.springframework.web.context.ContextLoaderListener
NULL 博文链接:https://angie.iteye.com/blog/2334955
二、 使用 Spring 的器监听器 ContextLoaderListener o1. maven依赖pom.xml o2. 注册监听器 ContextLoaderListener o3. 指定 Spring 配置文件的位置 o4. 获取Spring容器对象 在 Web 项目中使用 Spring 框架,首先...
ssh整合时 被虐的经验之谈。内容虽然比较少,也是前人的工作经验。
Web服务器实例化ApplicationContext容器时,通常会使用ContextLoaderListener来实现,此种方式只需要在web.xml中添加如下代码: <context-param> <param-name>contextConfigLocation</param-name> <param-value> ...
org.springframework.web.context.ContextLoaderListener <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener>
<listener-class>org.springframework.web.context.ContextLoaderListener <servlet-name>test <servlet-class>org.springframework.web.servlet.DispatcherServlet <load-on-startup>1 <servlet-name>...
<listener-class>org.springframework.web.context.ContextLoaderListener <!-- Listener log4jConfigLocation --> <listener-class>org.springframework.web.util.Log4jConfigListener </listener>
name标签与welcome-file-list标签中加入一下代码 struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* org.springframework.web.context.ContextLoaderListener ...
严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener at ...
<listener-class>org.springframework.web.context.ContextLoaderListener <!-- 利用spring监听 编码设置 --> <filter-name>SpringCharacterEncodingFilter <filter-class>org.springframework.web....
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:49) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3843) at org....
下面我们使用ContextLoaderListener作为载入器作一个详细的分析,这个Servlet的监听器是根上下文被载入的地方,也是整个 Spring web应用加载上下文的第一个地方;从加载过程我们可以看到,首先从Servlet事件中得到...
web.xml需要定义ContextLoaderListener,实例化容器配置 5.将事务管理交个Spring,采用AOP方式,删除原有Struts事务拦截器 -------------改造步骤----------------- 例如用户注册功能 1) 引入Hibernate开发包 2) ...
org.springframework.web.context.ContextLoaderListener 4.配置spring的配置文件(可以查看WEB-INF里面的applicationContext.xml文件) 注意:如果是默认,一定要放在WEB-INF中并文件名为...
proxool.0.9.1基础上做了修改。 改jar名称为:proxool-0.9.1.1,主要修改为以下3点: ... <listener-class>org.springframework.web.context.ContextLoaderListener 4.修正了proxool在多线程环境下的的错误。
org.springframework.web.context.ContextLoaderListener <filter-name>encodingFilter org.springframework.web.filter.CharacterEncodingFilter <param-name>encoding ...
上面, 我们在 web.xml 文件中告诉 ContextLoaderListener, 我们还有另外两个配置文件 /WEB-INF/database.xml 和 /WEB-INF/applicationContext.xml. applicationContext.xml: 1 2 <!DOCTYPE beans PUBLIC "-//...