還搞不清Spring 與 Spring MVC 容器之間的關係?

新書Java併發編程系統與模型已上線,歡迎拜讀。

在使用Spring MVC的時候,標準的配置是以下這樣的:javascript

1.ContextLoaderListener配置:java

<!-- Spring讀取Spring的配置文件 -->
    <context-param>
        <!-- 名稱 -->
        <param-name>contextConfigLocation</param-name>
        <!-- 文件的位置 -->
        <param-value>classpath:application-context.xml</param-value>
    </context-param>
<context-param>
    <param-name>webAppRootKey</param-name>
    <param-value>meipian</param-value>
</context-param>
    <!-- Spring 的監聽器配置 -->
    <listener>
        <!-- 在Spring-web包下 context -->
        <listener-class>org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>複製代碼

2.DispatcherServlet的配置:web

<servlet>
        <servlet-name>meipian</servlet-name>
        <!-- 配置SpringMVC核心控制器 -->
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
    </servlet>
        <servlet-mapping>
        <servlet-name>meipian</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>複製代碼

ContextLoaderListener監聽器的做用就是啓動Web容器時,自動裝配ApplicationContext的配置信息。由於它實現了ServletContextListener這個接口,在web.xml配置這個監聽器,啓動容器時,就會默認執行它實現的方法。
Spring MVC的使用的容器是WebApplicationContext。那麼他和ContextLoaderListener所初始化的ApplicationContext有什麼關係呢?spring

能夠從DispatcherServlet的初始化過程提及:DispatcherServlet攔截了全部的請求,因此訪問任何一個接口都會初始化DispatcherServlet對象。編程

初始化DispatcherServlet只是作了一個很簡單的事:服務器

public DispatcherServlet() {
        super();
        setDispatchOptionsRequest(true);
    }複製代碼
其父類FrameworkServlet初始化什麼也沒有:複製代碼
public FrameworkServlet() {
    }複製代碼

而後Tomcat會調用DispatcherServlet的init方法,在 Servlet 的生命期中,僅執行一次 init() 方法。它是在服務器裝入 Servlet 時執行的。 能夠配置服務器,以在啓動服務器或客戶機首次訪問 Servlet 時裝入 Servlet。 不管有多少客戶機訪問 Servlet,都不會重複執行 init() 。別問我爲何會調用init方法,這是servlet的規範。DispatcherServlet的init方法是在父類FrameworkServlet的父類HttpServlet中實現的:併發

@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");
        }
    }複製代碼

會調用DispatcherServlet的initServletBean()方法就是初始化WebApplicationContext的方法。這個方法在其子類FrameServlet方法中實現:mvc

@Override
    protected final void initServletBean() throws ServletException {
        getServletContext().log("Initializing Spring FrameworkServlet '" + getServletName() + "'");
        if (this.logger.isInfoEnabled()) {
            this.logger.info("FrameworkServlet '" + getServletName() + "': initialization started");
        }
        long startTime = System.currentTimeMillis();

        try {
            this.webApplicationContext = initWebApplicationContext(); //初始化WebApplicationContext對象
            initFrameworkServlet();
        }
        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("FrameworkServlet '" + getServletName() + "': initialization completed in " +
                    elapsedTime + " ms");
        }
    }複製代碼

initWebApplicationContext方法以下所示:app

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, etc
                    if (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;
    }複製代碼
會默認走到createWebApplicationContext方法:複製代碼
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;
    }複製代碼

在沒有 contexloaderListener的狀況下,parent是空的。
在有 contexloaderListener的狀況下,發現parent不是空的。ide

並且在Spring MVC的配置中,若是你將Service的配置與mvc的配置寫在一塊兒,有沒有contexloaderListener無所謂的。

這就說明了contexloaderListener做用是什麼了。

相關文章
相關標籤/搜索