SpringApplication的用法概括
SpringApplication提供了一个main方法来启动一个spring boot应用,常规的用法如下如下:
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args){
SpringApplication.run(HelloApplication.class, args);
}
}
同时,也可以通过实例化一个SpringApplication来定制化的配置属性
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args){
SpringApplication application = new SpringApplication(HelloApplication.class);
/**
* 自定义的配置信息
*/
application.setApplicationContextClass(AnnotationConfigApplicationContext.class);
application.setBannerMode(Banner.Mode.OFF);
// others
application.run(args);
}
}
SpringApplication作为spring应用启动的入口,下面主要记录在SpringApplication中,spring应用是怎么一步步启动起来的。关于这一点,官方的文档中是这么说道的:
- Create an appropriate ApplicationContext instance (depending on your
classpath) - Register a CommandLinePropertySource to expose command
line arguments as Spring properties - Refresh the application context, loading all singleton beans
- Trigger any CommandLineRunner beans
这里先把这段说明放一放,进入代码中,慢慢看具体的启动过程,同时我会记录在整个流程中遇到的各种相关的类。下面的代码给出启动的入口run()方法:
/**
* Run the Spring application, creating and refreshing a new
* {@link ApplicationContext}.
* @param args the application arguments (usually passed from a Java main method)
* @return a running {@link ApplicationContext}
*/
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
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;
}
run()方法中相关的类
- StopWatch类:主要用来记录应用各个任务的启动时间
- ConfigurableApplicationContext:方法的返回值类型,执行run方法后,会返回一个ConfigurableApplicationContext的一个实例,目前我学习到的是,这个类是ApplicationContext的一个子类,可以通过这个东西获取到应用的上下文信息,包括注册在ioc容器中的bean的信息
- SpringApplicationRunListeners类,关于这个类的说明见另一篇文章。
- ConfigurableEnvironment接口,这个类继承了Environment接口,关于这个接口具体干啥的,暂时还没看
- createApplicationContext()方法,创建一个SpringApplication对象,默认是AnnotationConfigApplicationContext。这个方法主要根据this.applicationContextClass这个Field来创建合适的SpringApplication对象,这个Field值的设置可以通过下面的方式来设置
application.setApplicationContextClass(AnnotationConfigApplicationContext.class);
- prepareContext()方法,这个方法的作用是见另一篇博客。
- 未完待续。。。。