Spring源码分析-启动(二)

一、web项目的启动顺序

1、启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<context-param>和<listener>两个结点。

2、紧急着,容创建一个ServletContext(servlet上下文),这个web项目的所有部分都将共享这个上下文。

3、容器将<context-param>转换为键值对,并交给servletContext。

4、容器创建<listener>中的类实例,创建监听器。

5、容器创建<filter>中的类实例,创建过滤器。

最终得出的结论是:ServletContext-> listener ->filter -> servlet

 

二、加载spring容器相关的bean

ContextLoaderListener

Tomcat在启动web应用的时候,会通过监听器的方式,通知ServletContextListener,web容器开始启动web应用了,ServletContextListener可以自定义初始化逻辑。ContextLoaderListener就是ServletContextListener接口的一个实现类,主要负责加载spring主容器相关的bean,默认加载WEB-INF/applicationContext.xml文件的配置信息。

 

ContextLoaderListener通过实现ServletContextListener接口,将spring容器融入web容器当中。这个可以分两个角度来理解:

  • web项目自身:接收web容器启动web应用的通知,开始自身配置的解析加载,创建bean实例,通过一个WebApplicationContext来维护spring项目的主容器相关的bean,以及其他一些组件。
  • web容器:web容器使用ServletContext来维护每一个web应用,ContextLoaderListener将spring容器,即WebApplicationContext,作为ServletContext的一个attribute,key为,保存在ServletContext中,从而web容器和spring项目可以通过ServletContext来交互。

ContextLoaderListener只是作为一个中间层来建立spring容器和web容器的关联关系,而实际完成以上两个角度的工作是通过ContextLoader来进行的,即在ContextLoader中定义以上逻辑,ContextLoaderListener的实现如下:

 

/*
 * Copyright 2002-2018 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
 *
 *      https://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}.
 *
 * <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 #setContextInitializers
 * @see org.springframework.web.WebApplicationInitializer
 */
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {

   /**
    * 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.
    * <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.
    * <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 i
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`afterRefresh` 是 Spring Boot 启动过程中的一个重要方法,它是在 Spring 应用上下文完成刷新之后被调用的。具体来说,`afterRefresh` 方法是在 `AbstractApplicationContext` 类的 `finishRefresh` 方法中被调用的,代码如下: ```java protected void finishRefresh() { // ... // Initialize lifecycle processor for this context. initLifecycleProcessor(); // Propagate refresh to lifecycle processor first. getLifecycleProcessor().onRefresh(); // Publish the final event. publishEvent(new ContextRefreshedEvent(this)); // Participate in LiveBeansView MBean, if active. if (!isRunning()) { stop(); } // ... // Finally, invoke the afterRefresh callbacks. invokeAfterRefreshCallbacks(); } ``` 在 `afterRefresh` 方法中,Spring Boot 会调用 `ApplicationContext` 中所有实现了 `org.springframework.context.support.ApplicationContextAware` 接口的 Bean 的 `setApplicationContext` 方法,将应用上下文传入这些 Bean 中。这样,这些 Bean 就能够获取到应用上下文,并在需要时使用它。 另外,Spring Boot 还会回调所有实现了 `org.springframework.boot.context.event.ApplicationContextInitializedListener` 接口的 Bean 的 `afterApplicationContextInitialized` 方法,这些 Bean 可以在应用上下文初始化之后进行一些自定义操作。 总之,`afterRefresh` 方法是 Spring Boot 启动过程中非常重要的一个方法,它标志着应用上下文已经完成了初始化,可以进入正常的运行状态了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值