返回顶部

springboot源码解读

springboot源码从main函数开始

public static void main(String[] args) {
    ApplicationContext app = SpringApplication.run(BeikbankApplication.class, args);
}

进入run方法,再持续进入到实际操作的步骤

public static ConfigurableApplicationContext run(Object source, String... args) {
	return run(new Object[] { source }, args);
}
public static ConfigurableApplicationContext run(Object[] sources, String[] args) {
	return new SpringApplication(sources).run(args);
}
public ConfigurableApplicationContext run(String... args) {
    //秒表计时器,用来记录整个容器启动时间
	StopWatch stopWatch = new StopWatch();
	stopWatch.start();
	ConfigurableApplicationContext context = null;
	FailureAnalyzers analyzers = null;
	//配置headless系统属性
	configureHeadlessProperty();
	//获取springbootApplication注解的类信息开启监听
	SpringApplicationRunListeners listeners = getRunListeners(args);
	listeners.starting();
	try {
	    //应用参数初始化
		ApplicationArguments applicationArguments = new DefaultApplicationArguments(
				args);
		//环境变量初始化
		ConfigurableEnvironment environment = prepareEnvironment(listeners,
				applicationArguments);
		//打印banner,就是spring图形
		Banner printedBanner = printBanner(environment);
		//创建应用上下文,默认会得到AnnotationConfigEmbeddedWebApplicationContext
		context = createApplicationContext();
		//反射加载实例化FailureAnalyzer实现类
		analyzers = new FailureAnalyzers(context);
		//上下文环境准备
		prepareContext(context, environment, listeners, applicationArguments,
				printedBanner);
		//主要的IOC过程,也是springMVC的主要过程,进行bean的相关接口初始化和bean实例化
		refreshContext(context);
		//到上一步tomcat已经启动完成了,这里算是springboot的扩展接口,可以自己实现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);
	}
}

我把主要步骤的每一步都标明了注释,看起来这里很多都是springboot特有,但主要的实现还是对springMVC的继承,你去看springmvc的源码,会发现,它主要的过程就是refreshContext这个方法所展现的过程

posted @ 2018-10-17 17:23  老梁讲Java  阅读(1216)  评论(0编辑  收藏  举报