Springboot启动过程

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

Springboot的特点:

创建独立的spring应用程序

嵌入的Tomcat,不需要部署war文件

简化maven配置

自动配置spring

提供生产就绪型功能,如外部配置

绝对没有代码生成和对XML没有要求配置

Springboot的优点:

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

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

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

spring boot 项目初始化过程:

Springboot启动类,一个是初始化,一个是run方法的执行,
SpringApplication.run(MyApplication.class, args);

1.1SpringApplication初始化过程

首先,要判断是否有web应用程序;然后,从所有类中查找META-INF/spring.factories文件,加载其中的初始化类和监听类;最后,查找运行的主类默认初始化initializers都继承自ApplicationContextInitializer。

SpringApplication构造函数:

springApplication方法初始化资源initialize(sources);

初始化方法:首先判断一下资源是否为空和长度是否大于0两个条件

满足:直接在资源中添加this.sources.addAll(Arrays.asList(sources));
不满足:

(1)通过判断应用程序是否可以加载Servlet和ConfigurableWebApplicationContext这两个类

(2)设置初识化类,从配置文件spring.factories中查找所有的ApplicationContextInitalizer的类进行加载、初始 化、排序。(工厂加载机制进行设置)

(3)从配置文件spring.factories中查找所有的AppliccationListener的类进行加载、初始化、排序。

(4)最后从当前调用的栈中查找主类。

this.mainApplicationClass = deduceMainApplicationClass();

SpringFactoriesLoader 工厂加载机制

(1)通过Thread.currentThread().getContextClassLoader()获取当前的类加载器。

(2)获取META-INF/spring.factories文件中key为type类型的所有的类的全限定名。

(3)通过上面获取到的类的全限定名,将会使用Class.forName加载类,并调用构造方法实例化类。

(4)最后AnnotationAwareOrderConparator类中的sort()方法进行排序。

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

(1)根据反射获取工厂类名

(2)获取枚举类型的路径,通过三目运算判断类加载器是否为空,为空的话就获取系统提供的资源,不为空的话就直接获取资源信息

(3)创建list类型的结果

(4)遍历是否有下一个更多的节点。有就让路径获取下一个节点,根据属性加载工具类中的加载属性方法获取属性,之后获取工厂类名,直接添加所有到结果中;没有就直接返回结果

1.2run()方法过程

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值