在学习spring boot 2.0源码之前,我们先利用spring initializr快速地创建一个基本的简单的示例:
1.先从创建示例中的main函数开始读起:
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* spring boot 2.0 源码分析
* author lizongshen
* date 2018/06/02
*/
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
2.在这里我们可以看到,spring boot是通过SpringApplication.run这个函数来进行启动的,其中args可以传递启动时需要的个性化参数。跳转到源码中继续一探究竟:
/**
* Static helper that can be used to run a {@link SpringApplication} from the
* specified source using default settings.
* @param primarySource the primary source to load
* @param args the application arguments (usually passed from a Java main method)
* @return the running {@link ApplicationContext}
*/
public static ConfigurableApplicationContext run(Class<?> primarySource,
String... args) {
return run(new Class<?>[] { primarySource }, args);
}
3.在这里我们看到,其把primarySource这个参数包装成数组,跳转到了另外一个同样的方法中。
小发现:SpringApplication.run()函数是允许同时启动多个Application的。
接着往下读
/**
* Static helper that can be used to run a {@link SpringApplication} from the
* specified sources using default settings and user supplied arguments.
* @param primarySources the primary sources to load
* @param args the application arguments (usually passed from a Java main method)
* @return the running {@link ApplicationContext}
*/
public static ConfigurableApplicationContext run(Class<?>[] primarySources,
String[] args) {
return new SpringApplication(primarySources).run(args);
}
4.在这段代码中,我们可以看到spring boot把我们所使用的静态方法,创建出了一个SpringApplication的实例,并启动了实例中的run方