Spring Boot 源码分析

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

Spring boot的特点

  1. 创建独立的Spring应用程序
  2. 嵌入的Tomcat,无需部署WAR文件
  3. 简化Maven配置
  4. 自动配置Spring
  5. 提供生产就绪型功能,如指标,健康检查和外部配置
  6. 绝对没有代码生成和对XML没有要求配置

Spring boot的优点

spring boot 可以支持你快速的开发出 restful 风格的微服务架构

自动化确实方便,做微服务再合适不过了,单一jar包部署和管理都非常方便。只要系统架构设计合理,大型项目也能用,加上nginx负载均衡,轻松实现横向扩展

spring boot 要解决的问题, 精简配置是一方面, 另外一方面是如何方便的让spring生态圈和其他工具链整合(比如redis, email, elasticsearch)

1、项目初始化过程
springboot启动类

SpringBoot的启动很简单,代码如下:

@SpringBootApplication
    public class MyApplication {
        public static void main(String[] args) {
            SpringApplication.run(MyApplication.class, args);
        }
    }

跟进去可以看到有两步,一个是初始化,一个是run方法的执行:
1.1、SpringApplication初始化化过程:
SpringApplication的初始化大致分为以下的步骤:

  1. 判断是否是web应用程序
  2. 从所有类中查找META-INF/spring.factories文件,加载其中的初始化类和监听类。
  3. 查找运行的主类 默认初始化Initializers都继承自ApplicationContextInitializer。

SpringApplication构建函数:

    public SpringApplication(ResourceLoader resourceLoader, Object... sources) {
		this.resourceLoader = resourceLoader;
		initialize(sources);
	}
         private void initialize(Object[] sources) {
		if (sources != null && sources.length > 0) {
			this.sources.addAll(Arrays.asList(sources));
		}
		//是否是web应用程序。通过判断应用程序中是否可以加载(class.forname)【"javax.servlet.Servlet","org.springframework.web.context.ConfigurableWebApplicationContext"】这两个类
		this.webEnvironment = deduceWebEnvironment();
		//设置初始化类:从配置文件spring.factories中查找所有的key=org.springframework.context.ApplicationContextInitializer的类【加载,初始化,排序】
        //SpringFactoriesLoader:工厂加载机制
		setInitializers((Collection) getSpringFactoriesInstances(
				ApplicationContextInitializer.class));
	    //设置Listeners:从配置文件spring.factories中查找所有的key=org.springframework.context.ApplicationListener的类.【加载,初始化,排序】
		setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
		 //从当前调用栈中,查找主类
		this.mainApplicationClass = deduceMainApplicationClass();
	}

SpringFactoriesLoader工厂加载机制
Initializers和Listeners的加载过程都是使用到了SpringFactoriesLoader工厂加载机制。我们进入到getSpringFactoriesInstances这个方法中:

        private <T> Collection<? extends T> getSpringFactoriesInstances(Class<T> type,
			Class<?>[] parameterTypes, Object... args) {
		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
		// Use names and ensure unique to protect against duplicates
		//获取META-INF/spring.factories文件中key为type类型的所有的类全限定名。注意是所有jar包内的。
		Set<String> names = new LinkedHashSet<String>(
				SpringFactoriesLoader.loadFactoryNames(type, classLoader));
				
        //通过上面获取到的类的全限定名,这里将会使用Class.forName加载类,并调用构造方法实例化类
		List<T> instances = createSpringFactoriesInstances(type, parameterTypes,
				classLoader, args, names);
	
          //根据类上的org.springframework.core.annotation.Order注解,排序
		AnnotationAwareOrderComparator.sort(instances);
		return instances;
	}

SpringFactoriesLoader.loadFactoryNames(type, classLoader));展示类方法加载的过程:

	public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) {
		String factoryClassName = factoryClass.getName();
		try {
			Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
					ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
			List<String> result = new ArrayList<String>();
			while (urls.hasMoreElements()) {
				URL url = urls.nextElement();
				Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));
				String factoryClassNames = properties.getProperty(factoryClassName);
				result.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(factoryClassNames)));
			}
			return result;
		}
		catch (IOException ex) {
			throw new IllegalArgumentException("Unable to load [" + factoryClass.getName() +
					"] factories from location [" + FACTORIES_RESOURCE_LOCATION + "]", ex);
		}
	}

ApplicationContextInitializer的类图:
《Spring Boot 源码分析》

初始化ApplicationContextInitializer:
“org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer”
“org.springframework.boot.context.ContextIdApplicationContextInitializer”
“org.springframework.boot.context.config.DelegatingApplicationContextInitializer”
“org.springframework.boot.context.web.ServerPortInfoApplicationContextInitializer”
“org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer”
“org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer”

初始化ApplicationListener:
“org.springframework.boot.ClearCachesApplicationListener”
“org.springframework.boot.builder.ParentContextCloserApplicationListener”
“org.springframework.boot.context.FileEncodingApplicationListener”
“org.springframework.boot.context.config.AnsiOutputApplicationListener”
“org.springframework.boot.context.config.ConfigFileApplicationListener”
“org.springframework.boot.context.config.DelegatingApplicationListener”
“org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener”
“org.springframework.boot.logging.ClasspathLoggingApplicationListener”
“org.springframework.boot.logging.LoggingApplicationListener”
“org.springframework.boot.autoconfigure.BackgroundPreinitializer”

1.2、Run 方法
启动run过程

  1. 注册一个StopWatch,用于监控启动过程
  2. 获取监听器SpringApplicationRunListener,用于springboot启动过程中的事件广播
  3. 设置环境变量environment
  4. 创建spring容器
  5. 创建FailureAnalyzers错误分析器,用于处理记录启动过程中的错误信息
  6. 调用所有初始化类的initialize方法
  7. 初始化spring容器
  8. 执行ApplicationRunner和CommandLineRunner的实现类
  9. 启动完成
	public ConfigurableApplicationContext run(String... args) {	
              //stopWatch 用于简单监听run启动过程
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		ConfigurableApplicationContext context = null;
		FailureAnalyzers analyzers = null;
		configureHeadlessProperty();
		 //获取监听器。springboot中有一个SpringApplicationRunListener监听器
		SpringApplicationRunListeners listeners = getRunListeners(args);
		listeners.started();
		try {
		  //下面两句是加载属性配置,执行完成后,所有的environment的属性都会加载进来,包括application.properties和外部的属性配置。
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(
					args);
			ConfigurableEnvironment environment = prepareEnvironment(listeners,
					applicationArguments);
			//打印Banner		
			Banner printedBanner = printBanner(environment);
			context = createApplicationContext();
			//错误分析器
			analyzers = new FailureAnalyzers(context);
             //主要是调用所有初始化类的initialize方法
			prepareContext(context, environment, listeners, applicationArguments,
					printedBanner);
			//初始化spring容器
			refreshContext(context);
			//主要是执行ApplicationRunner和CommandLineRunner的实现类
			afterRefresh(context, applicationArguments);
			//通知监听器
			listeners.finished(context, null);
			stopWatch.stop();
			if (this.logStartupInfo) {
				new StartupInfoLogger(this.mainApplicationClass)
						.logStarted(getApplicationLog(), stopWatch);
			}
			return context;
		}
		catch (Throwable ex) {
			handleRunFailure(context, listeners, analyzers, ex);
			throw new IllegalStateException(ex);
		}
	}

上述run过程广泛应用了spring事件机制(主要是广播)。上述代码中首先获取SpringApplicationRunListeners。这就是在spring.factories文件中配置的所有监听器。然后整个run 过程使用了listeners的5个方法,每个方法对应一个事件Event:

starting() run方法执行的时候立马执行;对应事件的类型是ApplicationStartedEvent
environmentPrepared() ApplicationContext创建之前并且环境信息准备好的时候调用;对应事件的类型是ApplicationEnvironmentPreparedEvent
contextPrepared() ApplicationContext创建好并且在source加载之前调用一次;没有具体的对应事件
contextLoaded() ApplicationContext创建并加载之后并在refresh之前调用;对应事件的类型是ApplicationPreparedEvent
finished() run方法结束之前调用;对应事件的类型是ApplicationReadyEvent或ApplicationFailedEven
SpringApplicationRunListeners是SpringApplicationRunListener的集合,SpringApplicationRunListener只有一个实现类:EventPublishingRunListener,在这个实现类中,有一个SimpleApplicationEventMulticaster类型的属性initialMulticaster,所有的事件都是通过这个属性的multicastEvent方法广播出去的。

    原文作者:Spring Boot
    原文地址: https://blog.csdn.net/mozf881/article/details/84455359
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞