项目的初始化启动**

114 阅读1分钟
     分析完(new SpringApplication(primarySources)).run(args)源码前一部分SpringApplication实例对象的初始化创建后,查看run(args)方法执行的项目初始化启动过程,核心代码具体如下:
public ConfigurableApplicationContext run(String... args) {
	StopWatch stopWatch = new StopWatch();
	stopWatch.start();
	ConfigurableApplicationContext context = null;
	Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
	this.configureHeadlessProperty();
     // 第一步:获取并启动监听器
	SpringApplicationRunListeners listeners = this.getRunListeners(args);
	listeners.starting();
	Collection exceptionReporters;
	try {
		ApplicationArguments applicationArguments = 
new DefaultApplicationArguments(args);
    // 第二步:根据SpringApplicationRunListeners以及参数来准备环境
		ConfigurableEnvironment environment = 
this.prepareEnvironment(listeners, applicationArguments);
		this.configureIgnoreBeanInfo(environment);
     // 准备Banner打印器 - 就是启动Spring Boot的时候打印在console上的ASCII艺术字体
		Banner printedBanner = this.printBanner(environment);
    
		// 第三步:创建Spring容器
		context = this.createApplicationContext();
		exceptionReporters =
                     this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, 
new Class[]{ConfigurableApplicationContext.class}, new Object[]{context});
    


这些内容,是从拉勾教育的《Java工程师高薪训练营》里学到的,课程内容非常全面,还有拉勾的内推大厂服务,推荐你也看看。