为你带来springboot最详细的源码分析

源码一直是一个比较头疼的问题,今天本人就为你们浅谈一下
1.项目初始化过程

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

跟进去可以看到有两步,一个是初始化,一个是run方法的执行:
1.1、SpringApplication初始化化过程:
SpringApplication的初始化大致分为以下的步骤:
判断是否是web应用程序
从所有类中查找META-INF/spring.factories文件,加载其中的初始化类和监听类。
查找运行的主类 默认初始化Initializers都继承自ApplicationContextInitializer。

    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);
		}
	}
**

spring.factories文件中的默认的实现类如下:

在这里插入图片描述
这里监听器为9个:

# Application Listeners
org.springframework.context.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.context.logging.ClasspathLoggingApplicationListener,\
org.springframework.boot.context.logging.LoggingApplicationListener,\
org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener

来看一下run方法:


```c
public ConfigurableApplicationContext run(String... args) {
		//时间监控
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		ConfigurableApplicationContext context = null;
		Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
		//java.awt.headless是J2SE的一种模式用于在缺少显示屏、键盘或者鼠标时的系统配置,很多监控工具如jconsole 需要将该值设置为true,系统变量默认为true
		configureHeadlessProperty();
		//获取spring.factories中的监听器变量,args为指定的参数数组,默认为当前类SpringApplication
		//第一步:获取并启动监听器
		SpringApplicationRunListeners listeners = getRunListeners(args);
		listeners.starting();
		try {
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(
					args);
			//第二步:构造容器环境
			ConfigurableEnvironment environment = prepareEnvironment(listeners,
					applicationArguments);
			//设置需要忽略的bean
			configureIgnoreBeanInfo(environment);
			//打印banner
			Banner printedBanner = printBanner(environment);
			//第三步:创建容器
			context = createApplicationContext();
			//第四步:实例化SpringBootExceptionReporter.class,用来支持报告关于启动的错误
			exceptionReporters = getSpringFactoriesInstances(
					SpringBootExceptionReporter.class,
					new Class[] { ConfigurableApplicationContext.class }, context);
			//第五步:准备容器
			prepareContext(context, environment, listeners, applicationArguments,
					printedBanner);
			//第六步:刷新容器
			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);
		}
		return context;
	}

1.2、Run 方法
启动run过程

注册一个StopWatch,用于监控启动过程
获取监听器SpringApplicationRunListener,用于springboot启动过程中的事件广播
设置环境变量environment
创建spring容器
创建FailureAnalyzers错误分析器,用于处理记录启动过程中的错误信息
调用所有初始化类的initialize方法
初始化spring容器
执行ApplicationRunner和CommandLineRunner的实现类
启动完成

	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方法广播出去的。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值