<<  < 2017 - 10 >  >>
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31




做项目时碰到Controller不能使用aop进行拦截,从网上搜索得知:使用spring mvc 启动了两个context:applicationContext 和WebapplicationContext。

首先我们来了解applicationContext 和WebapplicationContext区别和联系吧

1. ApplicationContext和WebApplicationContext是继承关系

复制代码
/**
 * Interface to provide configuration for a web application. This is read-only while
 * the application is running, but may be reloaded if the implementation supports this.
 *
 * <p>This interface adds a {@code getServletContext()} method to the generic
 * ApplicationContext interface, and defines a well-known application attribute name
 * that the root context must be bound to in the bootstrap process.
 *
 * <p>Like generic application contexts, web application contexts are hierarchical.
 * There is a single root context per application, while each servlet in the application
 * (including a dispatcher servlet in the MVC work) has its own child context.
 *
 * <p>In addition to standard application context lifecycle capabilities,
 * WebApplicationContext implementations need to detect {@link ServletContextAware}
 * beans and invoke the {@code setServletContext} method accordingly.*/public interface WebApplicationContext extends ApplicationContext {
复制代码

2. ContextLoaderListener 创建基于web的应用 applicationContext 并将它放入到ServletContext. applicationContext加载或者卸载spring管理的beans。在structs和spring mvc的控制层都是这样使用的。

3. DispatcherServlet创建自己的WebApplicationContext并管理这个WebApplicationContext里面的 handlers/controllers/view-resolvers. 

4. 当ContextLoaderListener和DispatcherServlet一起使用时, ContextLoaderListener 先创建一个根applicationContext,然后DispatcherSerlvet创建一个子applicationContext并且绑定到根applicationContext。

首先来看看web.xml的定义:

一般的web应用,通过ContextLoaderListener监听,ContextLoaderListener中加载的context成功后,spring 将 applicationContext存放在ServletContext中key值为"org.springwork.web.context.WebApplicationContext.ROOT"的attribute中。

复制代码
<listener>  
<listener-class>org.springwork.web.context.ContextLoaderListener</listener-class>  
</listener>  
<context-param>  
<param-name>contextConfigLocation</param-name>  
<param-value>classpath*:conf/applicationContext*.xml</param-value>  
</context-param>  
复制代码

DispatcherServlet加载的context成功后,会将applicationContext存放在org.springwork.web.servlet.workServlet.CONTEXT. + (servletName)的attribute中。 

复制代码
<servlet><servlet-name>mvcServlet</servlet-name><servlet-class>org.springwork.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:conf/spring-dispatcher-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet>
复制代码

当然,如果没有指定*servlet.xml 配置,则默认使用DispatcherServlet的默认配置DispatcherServlet.properties

复制代码
# Default implementation classes for DispatcherServlet's strategy interfaces.
# Used as fallback when no matching beans are found in the DispatcherServlet context.
# Not meant to be customized by application developers.

org.springwork.web.servlet.LocaleResolver=org.springwork.web.servlet.i18n.AcceptHeaderLocaleResolver

org.springwork.web.servlet.ThemeResolver=org.springwork.web.servlet.theme.FixedThemeResolver

org.springwork.web.servlet.HandlerMapping=org.springwork.web.servlet.handler.BeanNameUrlHandlerMapping,\
    org.springwork.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping

org.springwork.web.servlet.HandlerAdapter=org.springwork.web.servlet.mvc.HttpRequestHandlerAdapter,\
    org.springwork.web.servlet.mvc.SimpleControllerHandlerAdapter,\
    org.springwork.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

org.springwork.web.servlet.HandlerExceptionResolver=org.springwork.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver,\
    org.springwork.web.servlet.mvc.annotation.ResponseStatusExceptionResolver,\
    org.springwork.web.servlet.mvc.support.DefaultHandlerExceptionResolver

org.springwork.web.servlet.RequestToViewNameTranslator=org.springwork.web.servlet.view.DefaultRequestToViewNameTranslator

org.springwork.web.servlet.ViewResolver=org.springwork.web.servlet.view.InternalResourceViewResolver

org.springwork.web.servlet.FlashMapManager=org.springwork.web.servlet.support.SessionFlashMapManager
复制代码

简单的来说:spring bean的管理在applicationContext中,ContextLoaderListener的作用:

1. 将applicationContext的生命周期和servletContext的生命周期联系到一起。

2. 自动管理applicationContext的创建,ContextLoaderListener 给我们提供了一个便利,不用显式的去创建applicationContext。

DispatcherServlet 相关bean的管理在WebApplicationContext,ServletContextListener创建WebApplicationContext,WebApplicationContext可以访问ServletContext/ServletContextAware这些bean,还可以访问getServletContext方法。

正式的官方文档:

A web application can define any number of DispatcherServlets. Each servlet will operate in its own namespace, loading its own application context with mappings, handlers, etc. Only the root application context as loaded by ContextLoaderListener, if any, will be shared.As of Spring 3.1, DispatcherServlet may now be injected with a web application context, rather than creating its own internally. This is useful in Servlet 3.0+ environments, which support programmatic registration of servlet instances. 

在一个web应用中使用多个DispatcherServlet,每个servlet通过自己的命名空间来获取自己的webapplicationContext,然后加载此applicationContext里面的hangdlerMapping,hangdlerAdapter等等。ContextLoaderListener加载根application,所有子applicationContext共享根applicationContext。

从版本3.1 后,spring 支持将DispatcherServlet注入到根applicationContext,而不用创建自己的webapplicationContext,这主要为支持servlet 3.0 以上版本环境的要求,因为servlet 3.0 以上版本支持使用编程的方式来注册servlet实例。

spring支持使用多层层次applicationContext,通常我们使用两层结构就够了。

*** id="***_0.5097253863306053" src="///E:/MyDownloads/weizhi/temp/5e74a829-226c-4184-8875-3409822b5002/128/index_files/WSAPE.png" border="0" scrolling="no" style="box-sizing: border-box; border-style: none; border-width: initial; width: 20px; height: 20px;">

接下来,通过深入源代码层来探究WebApplicationContext是如何创建的?

1. DispatcherServlet的初始化过程使用到applicationContext。

    我们知道DispatcherServlet直接继承自workServlet,而workServlet又继承了HttpServletBean和 ApplicationContextAware。

2. workServlet实现了ApplicationContextAware接口的setApplicationContext()方法,可知DispatcherServlet的applicationContext来自workServlet。

复制代码
    /**
     * Called by Spring via {@link ApplicationContextAware} to inject the current
     * application context. This method allows workServlets to be registered as
     * Spring beans inside an existing {@link WebApplicationContext} rather than
     * {@link #findWebApplicationContext() finding} a
     * {@link org.springwork.web.context.ContextLoaderListener bootstrapped} context.
     * <p>Primarily added to support use in embedded servlet containers.
     * @since 4.0
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        if (this.webApplicationContext == null && applicationContext instanceof WebApplicationContext) {
            this.webApplicationContext = (WebApplicationContext) applicationContext;
            this.webApplicationContextInjected = true;
        }
    }
复制代码

3. workServlet的setApplicationContext()方法中WebApplicationContext是如何实例化的呢?

   workServlet继承自HttpServletBean,HttpServletBean的初始化方法:

复制代码
/**
     * Map config parameters onto bean properties of this servlet, and
     * invoke subclass initialization.
     * @throws ServletException if bean properties are invalid (or required
     * properties are missing), or if subclass initialization fails.
     */
    @Override
    public final void init() throws ServletException {
        if (logger.isDebugEnabled()) {
            logger.debug("Initializing servlet '" + getServletName() + "'");
        }

        // Set bean properties from init parameters.try {
            PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
            BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
            ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
            bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
            initBeanWrapper(bw);
            bw.setPropertyValues(pvs, true);
        }
        catch (BeansException ex) {
            logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
            throw ex;
        }

        // Let subclasses do whatever initialization they like.
        initServletBean();

        if (logger.isDebugEnabled()) {
            logger.debug("Servlet '" + getServletName() + "' configured successfully");
        }
    }
复制代码

workServlet 实现了initServletBean(); 

复制代码
    /**
     * Overridden method of {@link HttpServletBean}, invoked after any bean properties
     * have been set. Creates this servlet's WebApplicationContext.
     */
    @Override
    protected final void initServletBean() throws ServletException {
        getServletContext().log("Initializing Spring workServlet '" + getServletName() + "'");
        if (this.logger.isInfoEnabled()) {
            this.logger.info("workServlet '" + getServletName() + "': initialization started");
        }
        long startTime = System.currentTimeMillis();

        try {
            this.webApplicationContext = initWebApplicationContext();
            initworkServlet();
        }
        catch (ServletException ex) {
            this.logger.error("Context initialization failed", ex);
            throw ex;
        }
        catch (RuntimeException ex) {
            this.logger.error("Context initialization failed", ex);
            throw ex;
        }

        if (this.logger.isInfoEnabled()) {
            long elapsedTime = System.currentTimeMillis() - startTime;
            this.logger.info("workServlet '" + getServletName() + "': initialization completed in " +
                    elapsedTime + " ms");
        }
    }
复制代码

最终追踪到workServlet 的initWebApplicationContext()方法

复制代码
/**
     * Initialize and publish the WebApplicationContext for this servlet.
     * <p>Delegates to {@link #createWebApplicationContext} for actual creation
     * of the context. Can be overridden in subclasses.
     * @return the WebApplicationContext instance
     * @see #workServlet(WebApplicationContext)
     * @see #setContextClass
     * @see #setContextConfigLocation
     */protected WebApplicationContext initWebApplicationContext() {
        WebApplicationContext rootContext =
                WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        WebApplicationContext wac = null;

        if (this.webApplicationContext != null) {
            // A context instance was injected at construction time -> use it
            wac = this.webApplicationContext;
            if (wac instanceof ConfigurableWebApplicationContext) {
                ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
                if (!cwac.isActive()) {
                    // The context has not yet been refreshed -> provide services such as
                    // setting the parent context, setting the application context id, etcif (cwac.getParent() == null) {
                        // The context instance was injected without an explicit parent -> set
                        // the root application context (if any; may be null) as the parent                        cwac.setParent(rootContext);
                    }
                    configureAndRefreshWebApplicationContext(cwac);
                }
            }
        }
        if (wac == null) {
            // No context instance was injected at construction time -> see if one
            // has been registered in the servlet context. If one exists, it is assumed
            // that the parent context (if any) has already been set and that the
            // user has performed any initialization such as setting the context id
            wac = findWebApplicationContext();
        }
        if (wac == null) {
            // No context instance is defined for this servlet -> create a local one
            wac = createWebApplicationContext(rootContext);
        }

        if (!this.refreshEventReceived) {
            // Either the context is not a ConfigurableApplicationContext with refresh
            // support or the context injected at construction time had already been
            // refreshed -> trigger initial onRefresh manually here.            onRefresh(wac);
        }

        if (this.publishContext) {
            // Publish the context as a servlet context attribute.
            String attrName = getServletContextAttributeName();
            getServletContext().setAttribute(attrName, wac);
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() +
                        "' as ServletContext attribute with name [" + attrName + "]");
            }
        }

        return wac;
    }
复制代码

我们来分析整个流程吧:

1. 获取根applicationContext。

复制代码
        WebApplicationContext rootContext =
                WebApplicationContextUtils.getWebApplicationContext(getServletContext());

    /**
     * Find the root {@link WebApplicationContext} for this web app, typically
     * loaded via {@link org.springwork.web.context.ContextLoaderListener}.
     * <p>Will rethrow an exception that happened on root context startup,
     * to differentiate between a failed context startup and no context at all.
     * @param sc ServletContext to find the web application context for
     * @return the root WebApplicationContext for this web app, or {@code null} if none
     * @see org.springwork.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
     */public static WebApplicationContext getWebApplicationContext(ServletContext sc) {
        return getWebApplicationContext(sc, WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    }
    /**
     * Find a custom {@link WebApplicationContext} for this web app.
     * @param sc ServletContext to find the web application context for
     * @param attrName the name of the ServletContext attribute to look for
     * @return the desired WebApplicationContext for this web app, or {@code null} if none
     */public static WebApplicationContext getWebApplicationContext(ServletContext sc, String attrName) {
        Assert.notNull(sc, "ServletContext must not be null");
        Object attr = sc.getAttribute(attrName);
        if (attr == null) {
            return null;
        }
        if (attr instanceof RuntimeException) {
            throw (RuntimeException) attr;
        }
        if (attr instanceof Error) {
            throw (Error) attr;
        }
        if (attr instanceof Exception) {
            throw new IllegalStateException((Exception) attr);
        }
        if (!(attr instanceof WebApplicationContext)) {
            throw new IllegalStateException("Context attribute is not of type WebApplicationContext: " + attr);
        }
        return (WebApplicationContext) attr;
    }
复制代码

2. 判断webapplicationContext是否存在?存在的话就重利用该webapplicationContext

复制代码
    protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) {
        if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
            // The application context id is still set to its original default value
            // -> assign a more useful id based on available informationif (this.contextId != null) {
                wac.setId(this.contextId);
            }
            else {
                // Generate default id...
                wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
                        ObjectUtils.getDisplayString(getServletContext().getContextPath()) + "/" + getServletName());
            }
        }

        wac.setServletContext(getServletContext());
        wac.setServletConfig(getServletConfig());
        wac.setNamespace(getNamespace());
        wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener()));

        // The wac environment's #initPropertySources will be called in any case when the context
        // is refreshed; do it eagerly here to ensure servlet property sources are in place for
        // use in any post-processing or initialization that occurs below prior to #refresh
        ConfigurableEnvironment env = wac.getEnvironment();
        if (env instanceof ConfigurableWebEnvironment) {
            ((ConfigurableWebEnvironment) env).initPropertySources(getServletContext(), getServletConfig());
        }

        postProcessWebApplicationContext(wac);
        applyInitializers(wac);
        wac.refresh();
    }
复制代码

不存在的话,根据配置的属性名去查询:

复制代码
    /**
     * Retrieve a {@code WebApplicationContext} from the {@code ServletContext}
     * attribute with the {@link #setContextAttribute configured name}. The
     * {@code WebApplicationContext} must have already been loaded and stored in the
     * {@code ServletContext} before this servlet gets initialized (or invoked).
     * <p>Subclasses may override this method to provide a different
     * {@code WebApplicationContext} retrieval strategy.
     * @return the WebApplicationContext for this servlet, or {@code null} if not found
     * @see #getContextAttribute()
     */protected WebApplicationContext findWebApplicationContext() {
        String attrName = getContextAttribute();
        if (attrName == null) {
            return null;
        }
        WebApplicationContext wac =
                WebApplicationContextUtils.getWebApplicationContext(getServletContext(), attrName);
        if (wac == null) {
            throw new IllegalStateException("No WebApplicationContext found: initializer not registered?");
        }
        return wac;
    }
复制代码

3. 如果还查询不到WebapplicationContext,那么就创建一个新的WebapplicationContext,并绑定到root WebapplicationContext上:

复制代码
/**
     * Instantiate the WebApplicationContext for this servlet, either a default
     * {@link org.springwork.web.context.support.XmlWebApplicationContext}
     * or a {@link #setContextClass custom context class}, if set.
     * <p>This implementation expects custom contexts to implement the
     * {@link org.springwork.web.context.ConfigurableWebApplicationContext}
     * interface. Can be overridden in subclasses.
     * <p>Do not forget to register this servlet instance as application listener on the
     * created context (for triggering its {@link #onRefresh callback}, and to call
     * {@link org.springwork.context.ConfigurableApplicationContext#refresh()}
     * before returning the context instance.
     * @param parent the parent ApplicationContext to use, or {@code null} if none
     * @return the WebApplicationContext for this servlet
     * @see org.springwork.web.context.support.XmlWebApplicationContext
     */protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) {
        Class<?> contextClass = getContextClass();
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Servlet with name '" + getServletName() +
                    "' will try to create custom WebApplicationContext context of class '" +
                    contextClass.getName() + "'" + ", using parent context [" + parent + "]");
        }
        if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
            throw new ApplicationContextException(
                    "Fatal initialization error in servlet with name '" + getServletName() +
                    "': custom WebApplicationContext class [" + contextClass.getName() +
                    "] is not of type ConfigurableWebApplicationContext");
        }
        ConfigurableWebApplicationContext wac =
                (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);

        wac.setEnvironment(getEnvironment());
        wac.setParent(parent);
        wac.setConfigLocation(getContextConfigLocation());

        configureAndRefreshWebApplicationContext(wac);

        return wac;
    }
复制代码

4. 将子applicationContext发布到servlet context上。

复制代码
        if (this.publishContext) {
            // Publish the context as a servlet context attribute.
            String attrName = getServletContextAttributeName();
            getServletContext().setAttribute(attrName, wac);
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() +
                        "' as ServletContext attribute with name [" + attrName + "]");
            }
        }

/**
* Return the ServletContext attribute name for this servlet's WebApplicationContext.
* <p>The default implementation returns
* {@code SERVLET_CONTEXT_PREFIX + servlet name}.
* @see #SERVLET_CONTEXT_PREFIX
* @see #getServletName
*/
public String getServletContextAttributeName() {
return SERVLET_CONTEXT_PREFIX + getServletName();
}

 

/**
* Prefix for the ServletContext attribute for the WebApplicationContext.
* The completion is the servlet name.
*/
public static final String SERVLET_CONTEXT_PREFIX = workServlet.class.getName() + ".CONTEXT.";

 
复制代码

最后,ContextLoaderListener启动时如何产生applicationContext呢?

见参考我的这篇文章:http://www.cnblogs.com/davidwang456/archive/2013/03/12/2956125.html

小结:

我们就以workServlet的官方说明来结束吧。没有比它更合适的!希望你喜欢。

复制代码
public abstract class workServlet
extends HttpServletBean
implements ApplicationContextAwareBase servlet for Spring's web work. Provides integration with a Spring application context, in a JavaBean-based overall solution. 
This class offers the following ality: 

Manages a WebApplicationContext instance per servlet. The servlet's configuration is determined by beans in the servlet's namespace. 
Publishes events on request processing, whether or not a request is successfully handled. 
Subclasses must implement doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) to handle requests. Because this extends HttpServletBean rather than HttpServlet directly, bean properties are automatically mapped onto it. Subclasses can override initworkServlet() for custom initialization. 

Detects a "contextClass" parameter at the servlet init-param level, falling back to the default context class, XmlWebApplicationContext, if not found. Note that, with the default workServlet, a custom context class needs to implement the ConfigurableWebApplicationContext SPI. 

Accepts an optional "contextInitializerClasses" servlet init-param that specifies one or more ApplicationContextInitializer classes. The managed web application context will be delegated to these initializers, allowing for additional programmatic configuration, e.g. adding property sources or activating profiles against the context's environment. See also ContextLoader which supports a "contextInitializerClasses" context-param with identical semantics for the "root" web application context. 

Passes a "contextConfigLocation" servlet init-param to the context instance, parsing it into potentially multiple file paths which can be separated by any number of commas and spaces, like "test-servlet.xml, myServlet.xml". If not explicitly specified, the context implementation is supposed to build a default location from the namespace of the servlet. 

Note: In case of multiple config locations, later bean definitions will override ones defined in earlier loaded files, at least when using Spring's default ApplicationContext implementation. This can be leveraged to deliberately override certain bean definitions via an extra XML file. 

The default namespace is "'servlet-name'-servlet", e.g. "test-servlet" for a servlet-name "test" (leading to a "/WEB-INF/test-servlet.xml" default location with XmlWebApplicationContext). The namespace can also be set explicitly via the "namespace" servlet init-param. 

As of Spring 3.1, workServlet may now be injected with a web application context, rather than creating its own internally. This is useful in Servlet 3.0+ environments, which support programmatic registration of servlet instances. See workServlet(WebApplicationContext) Javadoc for details.
复制代码

 

参考资料:

http://starscream.iteye.com/blog/1107036

http://www.softwarevol.com/en/tutorial/Spring-ContextLoaderListener-And-DispatcherServlet-Concepts


发表评论:
天涯博客欢迎您!