Spring-ContextLoaderListener

来源:互联网 发布:淘宝女装店铺素材 编辑:程序博客网 时间:2024/05/22 16:57
/*
 * Copyright 2002-2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.context;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * Bootstrap listener to start up and shut down Spring's root {@link WebApplicationContext}.

 * Simply delegates to {@link ContextLoader} as well as to {@link ContextCleanupListener}.

引动程序监听器,它监听启动和关闭Spring的Root(即Web应用的环境/上下文)的事件。

这个监听动作委托给ContextLoader,ContextCleanupListerner的监听也是这样实现的。

 *
 * <p>This listener should be registered after
 * {@link org.springframework.web.util.Log4jConfigListener}

 * in <code>web.xml</code>, if the latter is used.

这个监听器,应该放到Log4jConfigListener后面,如果在web.xml中配置了这个监听器。

 *
 * <p>As of Spring 3.1, {@code ContextLoaderListener} supports injecting the root web
 * application context via the {@link #ContextLoaderListener(WebApplicationContext)}
 * constructor, allowing for programmatic configuration in Servlet 3.0+ environments. See
 * {@link org.springframework.web.WebApplicationInitializer} for usage examples.
 *
 * @author Juergen Hoeller
 * @author Chris Beams
 * @since 17.02.2003
 * @see org.springframework.web.WebApplicationInitializer
 * @see org.springframework.web.util.Log4jConfigListener
 */
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {

    private ContextLoader contextLoader;


    /**
     * Create a new {@code ContextLoaderListener} that will create a web application
     * context based on the "contextClass" and "contextConfigLocation" servlet
     * context-params. See {@link ContextLoader} superclass documentation for details on

     * default values for each.

 创建一个新的ContextLoaderListener(环境载入/加载监听器),这个监听器会创建一个web应用环境/上下文,

这个上下文基于contextClass和contextConfigLocation这两个Servlet环境/上下文参数。具体参见该类的父类ContextLoader中

的对每个的默认值的详细的描述。

     * <p>This constructor is typically used when declaring {@code ContextLoaderListener}
     * as a {@code <listener>} within {@code web.xml}, where a no-arg constructor is

     * required.

当在web.xml中,如果ContextLoaderListener以Listener(用<listener>标签声明)在web.xml进行了声明,在web.xml

中,就会需要一个无参数的构造器,这个构造器就会被用到。


     * <p>The created application context will be registered into the ServletContext under
     * the attribute name {@link WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE}
     * and the Spring application context will be closed when the {@link #contextDestroyed}
     * lifecycle method is invoked on this listener.
     * @see ContextLoader
     * @see #ContextLoaderListener(WebApplicationContext)
     * @see #contextInitialized(ServletContextEvent)
     * @see #contextDestroyed(ServletContextEvent)
     */
    public ContextLoaderListener() {
    }

    /**
     * Create a new {@code ContextLoaderListener} with the given application context. This
     * constructor is useful in Servlet 3.0+ environments where instance-based
     * registration of listeners is possible through the {@link ServletContext#addListener}

     * API.

使用指定的application context创建一个ContextLoaderListener。这个构造器在Servlet3.0+的环境中非常有用,

这个Servlet3.0+环境中,通过ServletContext的addListerner方法,基于实例的监听器注册是可能的。

     * <p>The context may or may not yet be {@linkplain

     * ConfigurableApplicationContext#refresh() refreshed}.

这个环境/上下文可能,也可能没有被刷新(可以参考ConfigurableApplicationContext的refresh方法)。

If it (a) is an implementation

     * of {@link ConfigurableWebApplicationContext} and (b) has <strong>not</strong>

     * already been refreshed (the recommended approach), then the following will occur:

如果,它实现了ConfigurableWebApplicationContext接口,并且没有刷新(推荐的方法),然后,下面的事情会发生:

     * <ul>
     * <li>If the given context has not already been assigned an {@linkplain

     * ConfigurableApplicationContext#setId id}, one will be assigned to it</li>

如果,这个给定的context环境/上下文)还没有设置一个id,那么一个Id给它(context)设置。

     * <li>{@code ServletContext} and {@code ServletConfig} objects will be delegated to

     * the application context</li>

ServletContext和ServletConfig这些对象(的功能)会被委托给这个application环境(上下文)


     * <li>{@link #customizeContext} will be called</li>

  • ContextLoader.customizeContext(javax.servlet.ServletContext, org.springframework.web.context.ConfigurableWebApplicationContext) 会被调用。

     * <li>Any {@link ApplicationContextInitializer}s specified through the

     * "contextInitializerClasses" init-param will be applied.</li>

任何ApplicationContextInitializerS指定的,通过contextInitializerClasses的init-param会被应用上。

     * <li>{@link ConfigurableApplicationContext#refresh refresh()} will be called</li>

    ConfigurableApplication的refresh方法会被调用。

     * </ul>
     * If the context has already been refreshed or does not implement
     * {@code ConfigurableWebApplicationContext}, none of the above will occur under the
     * assumption that the user has performed these actions (or not) per his or her

     * specific needs.

如果,这个context(环境/上下文)已经被刷新过,或者没有实现ConfigurableWebApplicationContext接口的话,

以上的事情都不会发生,在一个假设下,这个假设是永恒已经执行了这些动作(或者没有)根据用户的特定的需求。

     * <p>See {@link org.springframework.web.WebApplicationInitializer} for usage examples.

请看WebApplicationInitializer的使用例子。

     * <p>In any case, the given application context will be registered into the
     * ServletContext under the attribute name {@link
     * WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE} and the Spring
     * application context will be closed when the {@link #contextDestroyed} lifecycle

     * method is invoked on this listener.

(In any case, the given application context will be registered into the ServletContext under the attribute nameWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE and the Spring application context will be closed when thecontextDestroyed(javax.servlet.ServletContextEvent) lifecycle method is invoked on this listener.)

在任何情况下,这个传入的application的环境/上下文会被注册到ServletContext中,在这个WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE上,并且,Spring Application的环境/上下文会关闭

当这个监听器的contextDestroyed的方法被调用时。

     * @param context the application context to manage
     * @see #contextInitialized(ServletContextEvent)
     * @see #contextDestroyed(ServletContextEvent)
     */
    public ContextLoaderListener(WebApplicationContext context) {
        super(context);
    }

    /**

     * Initialize the root web application context.

      初始化root web appliation 环境/上下文。

     */
    public void contextInitialized(ServletContextEvent event) {
        this.contextLoader = createContextLoader();
        if (this.contextLoader == null) {
            this.contextLoader = this;
        }
        this.contextLoader.initWebApplicationContext(event.getServletContext());
    }

    /**

     * Create the ContextLoader to use. Can be overridden in subclasses.

       创建一个会用到的ContextLoader,在子类中可以被覆盖。

     * @return the new ContextLoader
     * @deprecated in favor of simply subclassing ContextLoaderListener itself
     * (which extends ContextLoader, as of Spring 3.0)
     */
    @Deprecated
    protected ContextLoader createContextLoader() {
        return null;
    }

    /**

     * Return the ContextLoader used by this listener.

      返回这个listerner用到的这个ContextLoader。

     * @return the current ContextLoader
     * @deprecated in favor of simply subclassing ContextLoaderListener itself
     * (which extends ContextLoader, as of Spring 3.0)
     */
    @Deprecated
    public ContextLoader getContextLoader() {
        return this.contextLoader;
    }


    /**

     * Close the root web application context.

     关闭root web application 环境/上下文。

     */
    public void contextDestroyed(ServletContextEvent event) {
        if (this.contextLoader != null) {
            this.contextLoader.closeWebApplicationContext(event.getServletContext());
        }
        ContextCleanupListener.cleanupAttributes(event.getServletContext());
    }

}


0 0