• 【详细学习SpringBoot核心源码之SpringApplication构造器&Run方法源码详细流程-4】


    一.知识回顾

    【0.SpringBoot专栏的相关文章都在这里哟,后续更多的文章内容可以点击查看】
    【1.SpringBoot初识之Spring注解发展流程以及常用的Spring和SpringBoot注解】
    【2.SpringBoot自动装配之SPI机制&SPI案例实操学习&SPI机制核心源码学习】
    【3.详细学习SpringBoot自动装配原理分析之核心流程初解析-1】
    【4.详细学习SpringBoot自动装配原理之自定义手写Starter案例实操实战-2】
    【5.IDEA中集成SpringBoot源码环境详细步骤讲解】
    【6.初识SpringBoot核心源码之SpringApplication构造器以及run方法主线流程-3】

    二.SpringApplication构造器

    上一篇文章和大家一起学习了SpringBoot启动的主线流程,接下来这篇文章我们先来学习一下SpringApplication构造器和run方法()核心流程

    2.1 一览SpringBoot核心启动流程

    通过上篇文章,我们一起查看了源码后我们知道了SpringBoot的核心流程大致分为俩个分支,接下来我们通过一个思维导图来大致的回顾梳理一下整理流程。如下图所示
    在这里插入图片描述

    2.2 接下来我们就来先学习SpringApplication构造方法中是如何帮我们完成这4个核心操作的。

    核心源码流程如下图所示:

    /**
     * Create a new {@link SpringApplication} instance. The application context will load
     * beans from the specified primary sources (see {@link SpringApplication class-level}
     * documentation for details. The instance can be customized before calling
     * {@link #run(String...)}.
     * @param resourceLoader the resource loader to use
     * @param primarySources the primary bean sources
     * @see #run(Class, String[])
     * @see #setSources(Set)
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
    	// 传递的resourceLoader为null
    	this.resourceLoader = resourceLoader;
    	Assert.notNull(primarySources, "PrimarySources must not be null");
    	// 记录主方法的配置类名称
    	this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
    	// 记录当前项目的类型
    	this.webApplicationType = WebApplicationType.deduceFromClasspath();
    	// 加载配置在spring.factories文件中的ApplicationContextInitializer对应的类型并实例化
    	// 并将加载的数据存储在了 initializers 成员变量中。
    	setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
    	// 初始化监听器 并将加载的监听器实例对象存储在了listeners成员变量中
    	setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
    	// 反推main方法所在的Class对象 并记录在了mainApplicationClass对象中
    	this.mainApplicationClass = deduceMainApplicationClass();
    }
    
    • 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
    2.2.1 webApplicationType

    先来看webApplicationType是如何来推导出当前启动的项目的类型。通过代码可以看到是通过deduceFromClassPath()方法根据ClassPath来推导出来的。

    // 记录当前项目的类型
    this.webApplicationType = WebApplicationType.deduceFromClasspath();
    
    • 1
    • 2

    查看WebApplicationType.deduceFromClasspath()方法:

    /**
     * 根据classPath推导出Web项目的类型
     * Servlet项目或者Reactive项目
     * @return
     */
    static WebApplicationType deduceFromClasspath() {
    	if (ClassUtils.isPresent(WEBFLUX_INDICATOR_CLASS, null)
    			&& !ClassUtils.isPresent(WEBMVC_INDICATOR_CLASS, null)
    			&& !ClassUtils.isPresent(JERSEY_INDICATOR_CLASS, null)) {
    		return WebApplicationType.REACTIVE;
    	}
    	for (String className : SERVLET_INDICATOR_CLASSES) {
    		if (!ClassUtils.isPresent(className, null)) {
    			return WebApplicationType.NONE;
    		}
    	}
    	return WebApplicationType.SERVLET;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在看整体的实现逻辑之前,我们先分别看两个内容,第一就是在上面的代码中使用到了相关的静态变量。这些静态变量其实就是一些绑定的Java类的全类路径。

    private static final String[] SERVLET_INDICATOR_CLASSES = { "javax.servlet.Servlet",
    		"org.springframework.web.context.ConfigurableWebApplicationContext" };
    
    private static final String WEBMVC_INDICATOR_CLASS = "org.springframework.web.servlet.DispatcherServlet";
    
    private static final String WEBFLUX_INDICATOR_CLASS = "org.springframework.web.reactive.DispatcherHandler";
    
    private static final String JERSEY_INDICATOR_CLASS = "org.glassfish.jersey.servlet.ServletContainer";
    
    private static final String SERVLET_APPLICATION_CONTEXT_CLASS = "org.springframework.web.context.WebApplicationContext";
    
    private static final String REACTIVE_APPLICATION_CONTEXT_CLASS = "org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext";
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    第二个就是 ClassUtils.isPresent()方法,该方法的逻辑也非常简单,就是通过反射的方式获取对应的类型的Class对象,如果存在返回true,否则返回false。
    在这里插入图片描述

    有了上面两个知识的补充,接下来我们看这个过程就更加明白啦。

    /**
     * 根据classPath推导出Web项目的类型
     * Servlet项目或者Reactive项目
     * @return
     */
    static WebApplicationType deduceFromClasspath() {
    	//如果存在WEBFLUX_INDICATOR_CLASS类型  并且
    	//不存在WEBMVC_INDICATOR_CLASS 和 JERSEY_INDICATOR_CLASS这俩个类型
    	if (ClassUtils.isPresent(WEBFLUX_INDICATOR_CLASS, null)
    			&& !ClassUtils.isPresent(WEBMVC_INDICATOR_CLASS, null)
    			&& !ClassUtils.isPresent(JERSEY_INDICATOR_CLASS, null)) {
    		//满足上面约束直接返回REACTIVE类型
    		return WebApplicationType.REACTIVE;
    	}
    	for (String className : SERVLET_INDICATOR_CLASSES) {
    		if (!ClassUtils.isPresent(className, null)) {
    			//通过循环遍历找要进行加载的类全名路径,如果不满足这个条件就返回NONE类型
    			return WebApplicationType.NONE;
    		}
    	}
    	//满足上面约束直接返回SERVLET类型
    	return WebApplicationType.SERVLET;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    2.2.2.setInitializers

    接下来我们再来看下如何实现加载初始化器的。

    // 加载配置在spring.factories文件中的ApplicationContextInitializer对应的类型并实例化
    // 并将加载的数据存储在了 initializers 成员变量中。
    setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
    
    • 1
    • 2
    • 3

    所有的初始化器都实现了 ApplicationContextInitializer接口,也就是根据这个类型来加载相关的实现类。

    public interface ApplicationContextInitializer<C extends ConfigurableApplicationContext> {
        void initialize(C var1);
    }
    
    • 1
    • 2
    • 3

    然后加载的关键方法是 getSpringFactoriesInstances()方法。该方法会加载 spring.factories文件中的key为 org.springframework.context.ApplicationContextInitializer 的值。

    /**
     * 扩展点的加载
     * @param type
     * @param 
     * @return
     */
    private <T> Collection<T> getSpringFactoriesInstances(Class<T> type) {
    	return getSpringFactoriesInstances(type, new Class<?>[] {});
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    具体的加载方法为 getSpringFacotiesInstance()方法,我们进入getSpringFactoriesInstances方法中查看源码

    /**
     * 初始化Initializer
     * SpringFactoriesLoader.loadFactoryNames(type, classLoader)
     *    根据对应的类型加载 spring.factories 文件中的配置信息
     * @param type
     * @param parameterTypes
     * @param args
     * @param 
     * @return
     */
    private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) {
    	// 获取当前上下文类加载器
    	ClassLoader classLoader = getClassLoader();
    	// 获取到的扩展类名存入set集合中防止重复
    	Set<String> names = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
    	// 创建扩展点实例
    	List<T> instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);
    	AnnotationAwareOrderComparator.sort(instances);
    	return instances;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    先进入 SpringFactoriesLoader.loadFactoryNames(type, classLoader)中具体查看加载文件的过程.

    public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
            //获取对应的名称
            String factoryTypeName = factoryType.getName();
            //加载spring.factory文件中所有的信息  
            //从Map集合中根据对应的名称来获取信息
            return (List)loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList());
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    然后我们来看下 loadSpringFactories方法

    private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
           MultiValueMap<String, String> result = (MultiValueMap)cache.get(classLoader);
           if (result != null) {
               return result;
           } else {
               try {
               	   //加载所有spring.factories文件
                   Enumeration<URL> urls = classLoader != null ? classLoader.getResources("META-INF/spring.factories") : ClassLoader.getSystemResources("META-INF/spring.factories");
                   //存储文件的信息
                   LinkedMultiValueMap result = new LinkedMultiValueMap();
    			   //遍历每个文件
                   while(urls.hasMoreElements()) {
                       URL url = (URL)urls.nextElement();
                       UrlResource resource = new UrlResource(url);
                       //加载党饿spring.factories文件中所有的属性
                       Properties properties = PropertiesLoaderUtils.loadProperties(resource);
                       Iterator var6 = properties.entrySet().iterator();
    				   //遍历每一个key对应的属性值
                       while(var6.hasNext()) {
                           Entry<?, ?> entry = (Entry)var6.next();
                           String factoryTypeName = ((String)entry.getKey()).trim();
                           String[] var9 = StringUtils.commaDelimitedListToStringArray((String)entry.getValue());
                           int var10 = var9.length;
    
                           for(int var11 = 0; var11 < var10; ++var11) {
                               String factoryImplementationName = var9[var11];
                               //key和对应的属性保存到了result对象中
                               result.add(factoryTypeName, factoryImplementationName.trim());
                           }
                       }
                   }
    		       //加入缓存
                   cache.put(classLoader, result);
                   return result;
               } catch (IOException var13) {//抛出异常
                   throw new IllegalArgumentException("Unable to load factories from location [META-INF/spring.factories]", var13);
               }
           }
    }
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    通过 loadSpringFactories 方法我们看到把 spring.factories文件中的所有信息都加载到了内存中了,但是我们现在只需要加载 ApplicationContextInitializer类型的数据。此时存储结果的容器是一个Map,我们通过key,也就是ApplicationContextInitializer类型来获取对应的集合即可。

    image.png

    然后回退到getSpringFactoriesInstances方法

    // 创建扩展点实例
    List<T> instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);
    
    • 1
    • 2

    此时我们该进入到createSpringFactoriesInstances方法中查看具体的流程。

    @SuppressWarnings("unchecked")
    private <T> List<T> createSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes,
    		ClassLoader classLoader, Object[] args, Set<String> names) {
    	// 创建实例的集合容器
    	List<T> instances = new ArrayList<>(names.size());
    	for (String name : names) {
    		try {
    			// 通过反射将扩展点实例实例化
    			Class<?> instanceClass = ClassUtils.forName(name, classLoader);
    			Assert.isAssignable(type, instanceClass);
    			Constructor<?> constructor = instanceClass.getDeclaredConstructor(parameterTypes);
    			T instance = (T) BeanUtils.instantiateClass(constructor, args);
    			instances.add(instance);
    		}
    		catch (Throwable ex) {
    			throw new IllegalArgumentException("Cannot instantiate " + type + " : " + name, ex);
    		}
    	}
    	return instances;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    到这其实我们就清楚了 getSpringFactoriesInstances方法的作用就是帮我们获取定义在 META-INF/spring.factories文件中的可以为 ApplicationContextInitializer 的值。并通过反射的方式获取实例对象。然后把实例的对象信息存储在了SpringApplication的 initializers属性中。

    /**
     * Sets the {@link ApplicationContextInitializer} that will be applied to the Spring
     * {@link ApplicationContext}.
     * @param initializers the initializers to set
     */
    public void setInitializers(Collection<? extends ApplicationContextInitializer<?>> initializers) {
    	this.initializers = new ArrayList<>(initializers);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    2.2.3.setListeners

    清楚了 setInitializers()方法的作用后,再看 setListeners()方法就非常简单了,都是调用了 getSpringFactoriesInstances方法,只是传入的类型不同。也就是要获取的 META-INF/spring.factories文件中定义的不同信息罢了。

    // 初始化监听器 并将加载的监听器实例对象存储在了listeners成员变量中
    setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
    
    • 1
    • 2
    public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
        void onApplicationEvent(E var1);
    }
    
    • 1
    • 2
    • 3

    image.png

    即加载定义在 META-INF/spring.factories文件中声明的所有的监听器,并将获取后的监听器存储在了 SpringApplicationlisteners属性中。

    /**
     * Sets the {@link ApplicationListener}s that will be applied to the SpringApplication
     * and registered with the {@link ApplicationContext}.
     * @param listeners the listeners to set
     */
    public void setListeners(Collection<? extends ApplicationListener<?>> listeners) {
    	this.listeners = new ArrayList<>(listeners);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    默认加载的监听器为:
    image.png

    2.2.4.mainApplicationClass

    最后我们来看下 duduceMainApplicaitonClass()方法是如何反推导出main方法所在的Class对象的。通过源码我们可以看到是通过 StackTrace来实现的。

    StackTrace:
    我们学习函数调用时,都知道每个函数都拥有自己的栈空间。
    一个函数被调用时,就创建一个新的栈空间。那么通过函数的嵌套调用最后就形成了一个函数调用堆栈

    // 反推main方法所在的Class对象 并记录在了mainApplicationClass对象中
    this.mainApplicationClass = deduceMainApplicationClass();
    
    • 1
    • 2

    进入deduceMainApplicationClass方法查看核心执行流程

    /**
     * StackTrace:
     *    我们在学习函数调用时,都知道每个函数都拥有自己的栈空间。
     *    一个函数被调用时,就创建一个新的栈空间。那么通过函数的嵌套调用最后就形成了一个函数调用堆栈
     * @return
     */
    private Class<?> deduceMainApplicationClass() {
    	try {
    		// 获取当前run方法执行的堆栈信息
    		StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
    		// 遍历堆栈信息
    		for (StackTraceElement stackTraceElement : stackTrace) {
    			// 如果调用的是main方法说明就找到了
    			if ("main".equals(stackTraceElement.getMethodName())) {
    				return Class.forName(stackTraceElement.getClassName());
    			}
    		}
    	}
    	catch (ClassNotFoundException ex) {
    		// Swallow and continue
    	}
    	return null;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    StackTrace其实就是记录了程序方法执行的链路。通过Debug的方式可以更直观的观察到。

    三.SpringApplication构造器的Run方法

    通过上面我们分析了SpringApplication构造器的方法,下面我们要分析run方法。这个就是研究run方法的入口。

    /**
     * Static helper that can be used to run a {@link SpringApplication} from the
     * specified sources using default settings and user supplied arguments.
     * @param primarySources the primary sources to load
     * @param args the application arguments (usually passed from a Java main method)
     * @return the running {@link ApplicationContext}
     */
    public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
    	// 创建了一个SpringApplication对象,并调用其run方法
    	// 1.先看下构造方法中的逻辑
    	// 2.然后再看run方法的逻辑
    	return new SpringApplication(primarySources).run(args);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    接下来我们进入run方法的主流程

    /**
     * Run the Spring application, creating and refreshing a new
     * {@link ApplicationContext}.
     * @param args the application arguments (usually passed from a Java main method)
     * @return a running {@link ApplicationContext}
     */
    public ConfigurableApplicationContext run(String... args) {
    	// 创建一个任务执行观察器
    	StopWatch stopWatch = new StopWatch();
    	// 开始执行记录执行时间
    	stopWatch.start();
    	// 声明 ConfigurableApplicationContext 对象
    	ConfigurableApplicationContext context = null;
    	// 声明集合容器用来存储 SpringBootExceptionReporter 启动错误的回调接口
    	Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
    	// 设置了一个名为java.awt.headless的系统属性
    	// 其实是想设置该应用程序,即使没有检测到显示器,也允许其启动.
    	//对于服务器来说,是不需要显示器的,所以要这样设置.
    	configureHeadlessProperty();
    	// 获取 SpringApplicationRunListener 加载的是 EventPublishingRunListener
    	// 获取启动时的监听器---》 事件发布器  发布相关事件的
    	SpringApplicationRunListeners listeners = getRunListeners(args);
    	// 触发启动事件  发布 starting 事件 --》 那么监听starting事件的监听器就会触发
    	listeners.starting();
    	try {
    		// 构造一个应用程序的参数持有类
    		ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
    		// 创建并配置环境
    		ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
    		// 配置需要忽略的BeanInfo信息
    		configureIgnoreBeanInfo(environment);
    		// 输出的Banner信息
    		Banner printedBanner = printBanner(environment);
    		// 创建应用上下文对象
    		context = createApplicationContext();
    		// 加载配置的启动异常处理器
    		exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
    				new Class[] { ConfigurableApplicationContext.class }, context);
    		// 刷新前操作
    		prepareContext(context, environment, listeners, applicationArguments, printedBanner);
    		// 刷新应用上下文 完成Spring容器的初始化
    		refreshContext(context);
    		// 刷新后操作
    		afterRefresh(context, applicationArguments);
    		// 结束记录启动时间
    		stopWatch.stop();
    		if (this.logStartupInfo) {
    			new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
    		}
    		// 事件广播 启动完成了
    		listeners.started(context);
    		callRunners(context, applicationArguments);
    	}
    	catch (Throwable ex) {
    		// 事件广播启动出错了
    		handleRunFailure(context, ex, exceptionReporters, listeners);
    		throw new IllegalStateException(ex);
    	}
    	try {
    		// 监听器运行中
    		listeners.running(context);
    	}
    	catch (Throwable ex) {
    		handleRunFailure(context, ex, exceptionReporters, null);
    		throw new IllegalStateException(ex);
    	}
    	// 返回上下文对象--> Spring容器对象
    	return context;
    }
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69

    按照上面方法的顺序补充一些具体方法:
    configureHeadlessProperty方法

    // 设置了一个名为java.awt.headless的系统属性
    // 其实是想设置该应用程序,即使没有检测到显示器,也允许其启动.
    //对于服务器来说,是不需要显示器的,所以要这样设置.
    private void configureHeadlessProperty() {
    		System.setProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS,
    				System.getProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, Boolean.toString(this.headless)));
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    getRunListeners方法,获取所有的监听器

    private SpringApplicationRunListeners getRunListeners(String[] args) {
    		Class<?>[] types = new Class<?>[] { SpringApplication.class, String[].class };
    		return new SpringApplicationRunListeners(logger,
    				getSpringFactoriesInstances(SpringApplicationRunListener.class, types, this, args));
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5

    prepareEnvironment方法,创建并配置环境

    private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
    			ApplicationArguments applicationArguments) {
    		// Create and configure the environment
    		ConfigurableEnvironment environment = getOrCreateEnvironment();
    		configureEnvironment(environment, applicationArguments.getSourceArgs());
    		ConfigurationPropertySources.attach(environment);
    		// 在配置环境信息之前发布事件
    		listeners.environmentPrepared(environment);
    		bindToSpringApplication(environment);
    		if (!this.isCustomEnvironment) {
    			environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,
    					deduceEnvironmentClass());
    		}
    		ConfigurationPropertySources.attach(environment);
    		return environment;
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    configureIgnoreBeanInfo配置需要忽略的BeanInfo信息

    private void configureIgnoreBeanInfo(ConfigurableEnvironment environment) {
    		if (System.getProperty(CachedIntrospectionResults.IGNORE_BEANINFO_PROPERTY_NAME) == null) {
    			Boolean ignore = environment.getProperty("spring.beaninfo.ignore", Boolean.class, Boolean.TRUE);
    			System.setProperty(CachedIntrospectionResults.IGNORE_BEANINFO_PROPERTY_NAME, ignore.toString());
    		}
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    printBanner方法,打印banner信息

    private Banner printBanner(ConfigurableEnvironment environment) {
    		if (this.bannerMode == Banner.Mode.OFF) {
    			return null;
    		}
    		ResourceLoader resourceLoader = (this.resourceLoader != null) ? this.resourceLoader
    				: new DefaultResourceLoader(getClassLoader());
    		SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter(resourceLoader, this.banner);
    		if (this.bannerMode == Mode.LOG) {
    			return bannerPrinter.print(environment, this.mainApplicationClass, logger);
    		}
    		return bannerPrinter.print(environment, this.mainApplicationClass, System.out);
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    createApplicationContext方法,创建应用上下文

    /**
    	 * Strategy method used to create the {@link ApplicationContext}. By default this
    	 * method will respect any explicitly set application context or application context
    	 * class before falling back to a suitable default.
    	 * @return the application context (not yet refreshed)
    	 * @see #setApplicationContextClass(Class)
    	 */
    	protected ConfigurableApplicationContext createApplicationContext() {
    		Class<?> contextClass = this.applicationContextClass;
    		if (contextClass == null) {
    			try {
    				switch (this.webApplicationType) {
    				case SERVLET:
    					contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS);
    					break;
    				case REACTIVE:
    					contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
    					break;
    				default:
    					contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
    				}
    			}
    			catch (ClassNotFoundException ex) {
    				throw new IllegalStateException(
    						"Unable create a default ApplicationContext, please specify an ApplicationContextClass", ex);
    			}
    		}
    		return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
    	}
    
    • 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

    getSpringFactoriesInstances方法,和我们之前研究的SpringApplication中的这个方法一样,就是传入的对应不同。接下来具体的逻辑不同。

    /**
    	 * 初始化Initializer
    	 * SpringFactoriesLoader.loadFactoryNames(type, classLoader)
    	 *    根据对应的类型加载 spring.factories 文件中的配置信息
    	 * @param type
    	 * @param parameterTypes
    	 * @param args
    	 * @param 
    	 * @return
    	 */
    	private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) {
    		// 获取当前上下文类加载器
    		ClassLoader classLoader = getClassLoader();
    		// 获取到的扩展类名存入set集合中防止重复
    		Set<String> names = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
    		// 创建扩展点实例
    		List<T> instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);
    		AnnotationAwareOrderComparator.sort(instances);
    		return instances;
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    prepareContext方法,刷新前操作。

    private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment,
    			SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) {
    		context.setEnvironment(environment);
    		postProcessApplicationContext(context);
    		applyInitializers(context);
    		listeners.contextPrepared(context);
    		if (this.logStartupInfo) {
    			logStartupInfo(context.getParent() == null);
    			logStartupProfileInfo(context);
    		}
    		// Add boot specific singleton beans
    		ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    		beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
    		if (printedBanner != null) {
    			beanFactory.registerSingleton("springBootBanner", printedBanner);
    		}
    		if (beanFactory instanceof DefaultListableBeanFactory) {
    			((DefaultListableBeanFactory) beanFactory)
    					.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
    		}
    		if (this.lazyInitialization) {
    			context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor());
    		}
    		// Load the sources
    		Set<Object> sources = getAllSources();
    		Assert.notEmpty(sources, "Sources must not be empty");
    		load(context, sources.toArray(new Object[0]));
    		listeners.contextLoaded(context);
    	}
    
    • 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

    refreshContext方法,刷新应用上下文 完成Spring容器的初始化

    private void refreshContext(ConfigurableApplicationContext context) {
    		refresh(context);
    		if (this.registerShutdownHook) {
    			try {
    				context.registerShutdownHook();
    			}
    			catch (AccessControlException ex) {
    				// Not allowed in some environments.
    			}
    		}
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    afterRefresh方法,刷新后操作

    /**
    	 * Called after the context has been refreshed.
    	 * @param context the application context
    	 * @param args the application arguments
    	 */
    	protected void afterRefresh(ConfigurableApplicationContext context, ApplicationArguments args) {
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    关于run方法的核心流程大致就是这些,想要查看具体细节的同学可以通过在本地编译源码的方式来详细的研究,怎么进行导入源码编译的详细流程之前的文章都有,感兴趣的可以自行查看。


    感谢恩师,dpb,感兴趣的可以搜索关注<波波烤鸭>


    好了,到这里【详细学习SpringBoot核心源码之SpringApplication构造器源码详细流程-4】就先学习到这里,关于SpringBoot源码更多内容持续创作学习中,持续更新中。

  • 相关阅读:
    大数据之SparkSQL 完整使用 (第八章)
    001 redis高并发减库存
    KubeSphere安装redis集群,全程超带劲
    想转行数字IC验证,需要提前了解哪些内容?
    Drools实战
    ZZNUOJ_C语言1035:分段函数求值(完整代码)
    STM32开发(三十)STM32F103 数据手册 —— 模拟/数字转换 DAC 详解
    【无标题】
    【EI会议投稿】第三届计算机、人工智能与控制工程国际学术会议 (CAICE 2024)
    GlassFish内存马分析
  • 原文地址:https://blog.csdn.net/Coder_ljw/article/details/127798462