【spring-boot】SpringApplication学习

SpringApplication的用法概括

参考资料:
https://docs.spring.io/springboot/docs/2.1.3.RELEASE/api/org/springframework/boot/SpringApplication.html

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应用是怎么一步步启动起来的。关于这一点,官方的文档中是这么说道的:

  1. Create an appropriate ApplicationContext instance (depending on your
    classpath)
  2. Register a CommandLinePropertySource to expose command
    line arguments as Spring properties
  3. Refresh the application context, loading all singleton beans
  4. 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()方法中相关的类
  1. StopWatch类:主要用来记录应用各个任务的启动时间
  2. ConfigurableApplicationContext:方法的返回值类型,执行run方法后,会返回一个ConfigurableApplicationContext的一个实例,目前我学习到的是,这个类是ApplicationContext的一个子类,可以通过这个东西获取到应用的上下文信息,包括注册在ioc容器中的bean的信息
  3. SpringApplicationRunListeners类,关于这个类的说明见另一篇文章
  4. ConfigurableEnvironment接口,这个类继承了Environment接口,关于这个接口具体干啥的,暂时还没看
  5. createApplicationContext()方法,创建一个SpringApplication对象,默认是AnnotationConfigApplicationContext。这个方法主要根据this.applicationContextClass这个Field来创建合适的SpringApplication对象,这个Field值的设置可以通过下面的方式来设置
application.setApplicationContextClass(AnnotationConfigApplicationContext.class);
  1. prepareContext()方法,这个方法的作用是见另一篇博客
  2. 未完待续。。。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值