- 浏览: 262506 次
- 性别:
- 来自: 苏州
-
文章分类
- 全部博客 (289)
- java (72)
- oracle (3)
- mysql (5)
- spring (28)
- hibernate (2)
- osgi (0)
- linux (2)
- ExtJs (1)
- jvm (0)
- mybatis (7)
- 分布式 (11)
- MINA (6)
- apache+tomcat (13)
- js+htm (7)
- android (44)
- http (1)
- hbase+hdoop (0)
- memcache (13)
- search (27)
- 部署及性能 (12)
- mongoDB (2)
- 多线程 (12)
- 安全管理验证 (9)
- struts (1)
- webservice (0)
- easyUI (1)
- spring security (16)
- pattern (6)
- 算法 (2)
最新评论
-
lzh8189146:
CommonsHttpSolrServer这个类,现在是不是没 ...
CommonsHttpSolrServer -
xiaochanzi:
我按照你的方法试了下,tomcat6可以发布,但是访问任何网页 ...
基于内嵌Tomcat的应用开发 -
phoneeye:
麻烦你,如果是抄来的文章,请给出来源。谢谢
ant 两则技巧 -
neverforget:
转载不注明出处
Spring Security3.1登陆验证 替换 usernamepasswordfilter -
liang1022:
若不使用eclipse ,如何在命令行下 运行服务端程序 ?
WebService CXF学习(入门篇2):HelloWorld
/**
* Performs the actual initialization work for the root application context.
* Called by {@link ContextLoaderListener} and {@link ContextLoaderServlet}.
*翻译:
这个类将会被ContextLoaderListener或者ContextLoaderServlet调用来实际执行spring容器(root application context)
的初始化工作。
* <p>Looks for a {@link #CONTEXT_CLASS_PARAM "contextClass"} parameter
* at the <code>web.xml</code> context-param level to specify the context
* class type, falling back to the default of
* {@link org.springframework.web.context.support.XmlWebApplicationContext}
* if not found. With the default ContextLoader implementation, any context class
* specified needs to implement the ConfigurableWebApplicationContext interface.
*
翻译:
在web.xml文件的context-param中寻找名为 "contextClass"的变量(CONTEXT_CLASS_PARAM )来指定context的类型,如果
在配置文件中找不到这个变量,那么将使用默认的类型,org.springframework.web.context.support.XmlWebApplicationContext。
如果使用默认的ContextLoader的实现类,任何一个context类都必须要实现ConfigurableWebApplicationContext接口。
* <p>Processes a {@link #CONFIG_LOCATION_PARAM "contextConfigLocation"}
* context-param and passes its value to the context instance, parsing it into
* potentially multiple file paths which can be separated by any number of
* commas and spaces, e.g. "WEB-INF/applicationContext1.xml,
* WEB-INF/applicationContext2.xml". Ant-style path patterns are supported as well,
* e.g. "WEB-INF/*Context.xml,WEB-INF/spring*.xml" or "WEB-INF/**/*Context.xml".
* If not explicitly specified, the context implementation is supposed to use a
* default location (with XmlWebApplicationContext: "/WEB-INF/applicationContext.xml").
*
翻译:
在处理web.xml里面的名为"contextConfigLocation"的context-param 变量CONFIG_LOCATION_PARAM 的时候,会将这个变量
的值传递到context实例中去。这个值可以是由逗号和空格分开的多个文件路径,例如可以是"WEB-INF/applicationContext1.xml,
WEB-INF/applicationContext2.xml"。除此之外,这个值还可以用ant风格的方式书写,例如"WEB-INF/*Context.xml,WEB-INF/spring*.xml" 或者 "WEB-INF/**/*Context.xml"。如果在web.xml中设置这个变量,那么spring会使用默认的路径XmlWebApplicationContext: "/WEB-INF/applicationContext.xml"
* <p>Note: In case of multiple config locations, later bean definitions will
* override ones defined in previously loaded files, at least when using one of
* Spring's default ApplicationContext implementations. This can be leveraged
* to deliberately override certain bean definitions via an extra XML file.
*
翻译:
注意:
如果使用了多个配置文件路径而且使用了Spring的ApplicationContext默认实现类的话,对于同一个bean,后来加载的
文件的内容会复写较前加载的文件的内容。所以,我们可以利用这个特性,通过额外的xml 配置文件(当然要较后加载)
来复写某个bean的配置信息。
* <p>Above and beyond loading the root application context, this class
* can optionally load or obtain and hook up a shared parent context to
* the root application context. See the
* {@link #loadParentContext(ServletContext)} method for more information.
*
翻译:
除了读取根容器信息(root application context)之外,这个类可以选择去读取,获得或者挂起根容器信息(root application context)
的共有的父容器信息。
* @author Juergen Hoeller
* @author Colin Sampaleanu
* @author Sam Brannen
* @author 译者:kaiwii
* @since 17.02.2003
* @see ContextLoaderListener
* @see ContextLoaderServlet
* @see ConfigurableWebApplicationContext
* @see org.springframework.web.context.support.XmlWebApplicationContext
*/
public class ContextLoader {
/**
* Config param for the root WebApplicationContext implementation class to
* use: "<code>contextClass</code>"
*/
public static final String CONTEXT_CLASS_PARAM = "contextClass";
翻译:
root WebApplicationContext实现类的配置参数,名为"contextClass"
/**
* Name of servlet context parameter (i.e., "<code>contextConfigLocation</code>")
* that can specify the config location for the root context, falling back
* to the implementation's default otherwise.
* @see org.springframework.web.context.support.XmlWebApplicationContext#DEFAULT_CONFIG_LOCATION
*/
public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";
/**
* Optional servlet context parameter (i.e., "<code>locatorFactorySelector</code>")
* used only when obtaining a parent context using the default implementation
* of {@link #loadParentContext(ServletContext servletContext)}.
* Specifies the 'selector' used in the
* {@link ContextSingletonBeanFactoryLocator#getInstance(String selector)}
* method call, which is used to obtain the BeanFactoryLocator instance from
* which the parent context is obtained.
* <p>The default is <code>classpath*:beanRefContext.xml</code>,
* matching the default applied for the
* {@link ContextSingletonBeanFactoryLocator#getInstance()} method.
* Supplying the "parentContextKey" parameter is sufficient in this case.
*/
public static final String LOCATOR_FACTORY_SELECTOR_PARAM = "locatorFactorySelector";
/**
* Optional servlet context parameter (i.e., "<code>parentContextKey</code>")
* used only when obtaining a parent context using the default implementation
* of {@link #loadParentContext(ServletContext servletContext)}.
* Specifies the 'factoryKey' used in the
* {@link BeanFactoryLocator#useBeanFactory(String factoryKey)} method call,
* obtaining the parent application context from the BeanFactoryLocator instance.
* <p>Supplying this "parentContextKey" parameter is sufficient when relying
* on the default <code>classpath*:beanRefContext.xml</code> selector for
* candidate factory references.
*/
public static final String LOCATOR_FACTORY_KEY_PARAM = "parentContextKey";
/**
* Name of the class path resource (relative to the ContextLoader class)
* that defines ContextLoader's default strategy names.
*/
private static final String DEFAULT_STRATEGIES_PATH = "ContextLoader.properties";
private static final Properties defaultStrategies;
static {
// Load default strategy implementations from properties file.
// This is currently strictly internal and not meant to be customized
// by application developers.
try {
ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class);
defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
}
catch (IOException ex) {
throw new IllegalStateException("Could not load 'ContextLoader.properties': " + ex.getMessage());
}
}
private static final Log logger = LogFactory.getLog(ContextLoader.class);
/**
* Map from (thread context) ClassLoader to WebApplicationContext.
* Often just holding one reference - if the ContextLoader class is
* deployed in the web app ClassLoader itself!
*/
private static final Map currentContextPerThread = CollectionFactory.createConcurrentMapIfPossible(1);
/**
* The root WebApplicationContext instance that this loader manages.
*/
private WebApplicationContext context;
/**
* Holds BeanFactoryReference when loading parent factory via
* ContextSingletonBeanFactoryLocator.
*/
private BeanFactoryReference parentContextRef;
/**
* Initialize Spring's web application context for the given servlet context,
* according to the "{@link #CONTEXT_CLASS_PARAM contextClass}" and
* "{@link #CONFIG_LOCATION_PARAM contextConfigLocation}" context-params.
* @param servletContext current servlet context
* @return the new WebApplicationContext
* @throws IllegalStateException if there is already a root application context present
* @throws BeansException if the context failed to initialize
* @see #CONTEXT_CLASS_PARAM
* @see #CONFIG_LOCATION_PARAM
*/
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!");
}
servletContext.log("Initializing Spring root WebApplicationContext");
if (logger.isInfoEnabled()) {
logger.info("Root WebApplicationContext: initialization started");
}
long startTime = System.currentTimeMillis();
try {
// Determine parent for root web application context, if any.
ApplicationContext parent = loadParentContext(servletContext);
// Store context in local instance variable, to guarantee that
// it is available on ServletContext shutdown.
this.context = createWebApplicationContext(servletContext, parent);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
currentContextPerThread.put(Thread.currentThread().getContextClassLoader(), this.context);
if (logger.isDebugEnabled()) {
logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
}
if (logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
}
return this.context;
}
catch (RuntimeException ex) {
logger.error("Context initialization failed", ex);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
throw ex;
}
catch (Error err) {
logger.error("Context initialization failed", err);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);
throw err;
}
}
/**
* Instantiate the root WebApplicationContext for this loader, either the
* default context class or a custom context class if specified.
* <p>This implementation expects custom contexts to implement the
* {@link ConfigurableWebApplicationContext} interface.
* Can be overridden in subclasses.
* <p>In addition, {@link #customizeContext} gets called prior to refreshing the
* context, allowing subclasses to perform custom modifications to the context.
* @param servletContext current servlet context
* @param parent the parent ApplicationContext to use, or <code>null</code> if none
* @return the root WebApplicationContext
* @throws BeansException if the context couldn't be initialized
* @see ConfigurableWebApplicationContext
*/
protected WebApplicationContext createWebApplicationContext(
ServletContext servletContext, ApplicationContext parent) throws BeansException {
Class contextClass = determineContextClass(servletContext);
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
"] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
}
ConfigurableWebApplicationContext wac =
(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
wac.setParent(parent);
wac.setServletContext(servletContext);
wac.setConfigLocation(servletContext.getInitParameter(CONFIG_LOCATION_PARAM));
customizeContext(servletContext, wac);
wac.refresh();
return wac;
}
/**
* Return the WebApplicationContext implementation class to use, either the
* default XmlWebApplicationContext or a custom context class if specified.
* @param servletContext current servlet context
* @return the WebApplicationContext implementation class to use
* @throws ApplicationContextException if the context class couldn't be loaded
* @see #CONTEXT_CLASS_PARAM
* @see org.springframework.web.context.support.XmlWebApplicationContext
*/
protected Class determineContextClass(ServletContext servletContext) throws ApplicationContextException {
String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);
if (contextClassName != null) {
try {
return ClassUtils.forName(contextClassName);
}
catch (ClassNotFoundException ex) {
throw new ApplicationContextException(
"Failed to load custom context class [" + contextClassName + "]", ex);
}
}
else {
contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());
try {
return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());
}
catch (ClassNotFoundException ex) {
throw new ApplicationContextException(
"Failed to load default context class [" + contextClassName + "]", ex);
}
}
}
/**
* Customize the {@link ConfigurableWebApplicationContext} created by this
* ContextLoader after config locations have been supplied to the context
* but before the context is <em>refreshed</em>.
* <p>The default implementation is empty but can be overridden in subclasses
* to customize the application context.
* @param servletContext the current servlet context
* @param applicationContext the newly created application context
* @see #createWebApplicationContext(ServletContext, ApplicationContext)
*/
protected void customizeContext(
ServletContext servletContext, ConfigurableWebApplicationContext applicationContext) {
}
/**
* Template method with default implementation (which may be overridden by a
* subclass), to load or obtain an ApplicationContext instance which will be
* used as the parent context of the root WebApplicationContext. If the
* return value from the method is null, no parent context is set.
* <p>The main reason to load a parent context here is to allow multiple root
* web application contexts to all be children of a shared EAR context, or
* alternately to also share the same parent context that is visible to
* EJBs. For pure web applications, there is usually no need to worry about
* having a parent context to the root web application context.
* <p>The default implementation uses
* {@link org.springframework.context.access.ContextSingletonBeanFactoryLocator},
* configured via {@link #LOCATOR_FACTORY_SELECTOR_PARAM} and
* {@link #LOCATOR_FACTORY_KEY_PARAM}, to load a parent context
* which will be shared by all other users of ContextsingletonBeanFactoryLocator
* which also use the same configuration parameters.
* @param servletContext current servlet context
* @return the parent application context, or <code>null</code> if none
* @throws BeansException if the context couldn't be initialized
* @see org.springframework.context.access.ContextSingletonBeanFactoryLocator
*/
protected ApplicationContext loadParentContext(ServletContext servletContext)
throws BeansException {
ApplicationContext parentContext = null;
String locatorFactorySelector = servletContext.getInitParameter(LOCATOR_FACTORY_SELECTOR_PARAM);
String parentContextKey = servletContext.getInitParameter(LOCATOR_FACTORY_KEY_PARAM);
if (parentContextKey != null) {
// locatorFactorySelector may be null, indicating the default "classpath*:beanRefContext.xml"
BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator.getInstance(locatorFactorySelector);
if (logger.isDebugEnabled()) {
logger.debug("Getting parent context definition: using parent context key of '" +
parentContextKey + "' with BeanFactoryLocator");
}
this.parentContextRef = locator.useBeanFactory(parentContextKey);
parentContext = (ApplicationContext) this.parentContextRef.getFactory();
}
return parentContext;
}
/**
* Close Spring's web application context for the given servlet context. If
* the default {@link #loadParentContext(ServletContext)} implementation,
* which uses ContextSingletonBeanFactoryLocator, has loaded any shared
* parent context, release one reference to that shared parent context.
* <p>If overriding {@link #loadParentContext(ServletContext)}, you may have
* to override this method as well.
* @param servletContext the ServletContext that the WebApplicationContext runs in
*/
public void closeWebApplicationContext(ServletContext servletContext) {
servletContext.log("Closing Spring root WebApplicationContext");
try {
if (this.context instanceof ConfigurableWebApplicationContext) {
((ConfigurableWebApplicationContext) this.context).close();
}
}
finally {
currentContextPerThread.remove(Thread.currentThread().getContextClassLoader());
servletContext.removeAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
if (this.parentContextRef != null) {
this.parentContextRef.release();
}
}
}
/**
* Obtain the Spring root web application context for the current thread
* (i.e. for the current thread's context ClassLoader, which needs to be
* the web application's ClassLoader).
* @return the current root web application context, or <code>null</code>
* if none found
* @see org.springframework.web.context.support.SpringBeanAutowiringSupport
*/
public static WebApplicationContext getCurrentWebApplicationContext() {
return (WebApplicationContext) currentContextPerThread.get(Thread.currentThread().getContextClassLoader());
}
}
发表评论
-
Java keytool 安全证书学习笔记
2012-08-02 14:16 803http://linliangyi2007.iteye.com ... -
java国际化
2012-07-16 14:08 424http://lavasoft.blog.51cto.com/ ... -
Java版远程控制V1.0
2012-06-17 21:37 770http://www.cnblogs.com/syxchina ... -
浅析Java中CountDownLatch用法
2012-05-16 20:57 805CountDownLatch如其所写,是一个 ... -
SMTP发送邮件
2012-04-18 09:41 778SMTP发送邮件 openkk 2011-06-0 ... -
smtp 返回代码 信息
2012-04-18 08:52 1457SMTP Server Response Cod ... -
JavaMail详解
2012-04-18 02:24 0JavaMail详解 博客分类: JAV ... -
安装Eclipse反编译插件
2012-04-17 09:34 808安装Eclipse反编译插件 博客分类: ... -
Java编程中“为了性能”尽量要做到的一些地方
2012-04-13 08:30 671最近的机器内存又爆满了,除了新增机器内存外,还应该好好r ... -
Dijkstra算法
2012-04-11 08:00 876Dijkstra算法 博客分类: 算 ... -
java 播放音乐
2012-04-11 08:00 1007java 播放音乐 博客分类: J2 ... -
Java中的native,transient,volatile和strictfp关键字
2012-04-06 08:49 737Java中的native,transient,v ... -
用ReentrantLock模拟宴会的热闹情景
2012-04-05 08:32 913用ReentrantLock模拟宴会的热闹情景 ... -
Hashmap 分析
2012-04-05 08:32 738Hashmap 博客分类: 算法 ... -
ExecutorService线程池
2012-04-05 08:32 769ExecutorService线程池 (2010 ... -
Java并发:juc Executor框架详解
2012-04-05 08:32 791Java并发:juc Executor ... -
java并发包,多线程,工具类,笔记
2012-04-11 08:00 913JDK 线程池 Executors.newCachedT ... -
利用 Spring 和 EHCache 做方法缓存处理〔转〕
2012-04-09 09:49 855利用 Spring 和 EHCache 做方法缓存处理〔 ... -
EhCache使用详细介绍
2012-04-09 09:49 1354EhCache使用详细介绍 Ehcache中不仅可 ... -
HashMap 分析
2012-04-01 08:21 1908http://www.blogjava.net ...
相关推荐
ContextLoader ContextLoaderListener ContextLoaderPlugIn ContextLoaderServlet ContextRefreshedEvent ContextSingletonBeanFactoryLocator ControlFlow ControlFlowFactory ControlFlowFactory.Jdk13...
1. **初始化流程**:从`org.springframework.context.support.ClassPathXmlApplicationContext`或`org.springframework.web.context.ContextLoader`开始,理解如何加载配置并创建Bean定义。 2. **依赖注入**:研究`...
在Spring MVC框架中,`ServletContextListener`扮演着重要的角色,它是Java Servlet API的一部分,用于监听ServletContext事件。在本文中,我们将深入探讨`ServletContextListener`如何与Spring MVC协作,以及如何...
`WebApplicationContext`是`ApplicationContext`的子接口,专为Web环境设计,提供了更多的功能,如对Servlet API的支持。可以通过`WebApplicationContextUtils`类从`ServletContext`中获取`WebApplicationContext`...
它包括了数据库连接池的管理、事务处理、SQL语句的执行以及异常转换等,使得开发者无需直接与低级别的JDBC API打交道。 4. **jms**:Java Message Service (JMS) 模块提供了与消息中间件交互的接口,支持发布/订阅...
### Spring框架源码详解 #### 一、Spring框架概述 ...总之,Spring框架通过一系列精心设计的机制和API,简化了Java应用的开发流程。通过深入了解这些机制,开发者可以更高效地构建高质量的应用程序。
Spring为测试提供了便利,`org.springframework.test`包下的`ContextLoader`、`TestContext`等类可以帮助开发者进行单元测试和集成测试,提高测试覆盖率。 8. **Spring Expression Language (SpEL)** SpEL是...
6. **测试类加载配置文件**:在单元测试中,可以通过 Spring 提供的 `ContextLoader` 或者 `ApplicationContext` 来加载配置文件并初始化上下文。 7. **根据 name 注入 service**:使用 `@Resource` 注解指定 name ...
在上传Excel文件时,需要在web.xml文件中配置Listener和ContextLoader,用于加载Spring的配置文件application.xml。在application.xml文件中,可以规定上传文件的格式、大小等多种属性限制。 在前端HTML文件中,...
MySQL的JDBC驱动是与Java应用程序交互的桥梁,它实现了JDBC API,使得Java程序能够通过标准的JDBC接口与MySQL数据库进行通信。在使用DBCP连接池时,需要将对应的MySQL JDBC驱动jar包添加到项目的类路径中。 3. **...