SpringBoot启动过程(二)

ImportSelector调用

我们上一片文章剖析到了@Import注解方法,以及AutoConfigurationImportSelector.class 的作用,他的作用就是定义了ImportSelector()方法,然后等待Spring去调用此方法。那么Spring是如何调用此方法的呢,一起来找一下。
首先得有个思路,不能盲目的去找。我们既然知道是Spring调用的此方法,那么肯定是在启动过程中调用的。那么Spring是如何启动的呢,还有一个地方我们没看,

	SpringApplication.run(ProviderApplication.class, args);

方法。也就这里能启动Spring,说到底,Spring也就是一大堆的Java程序,他也需要一个入口去启动它。我们点进去看看。

	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;
	}

我们进入 prepareContext(context, environment, listeners, applicationArguments, printedBanner); 方法的时候,其实就能知道了,这个run()方法基本就是启动了Spring,具体可以参看Spring是如何启动的。这里赘述不了了,内容可以写一本书了。这其中掺杂了内置Tomcat啊、各种环境加载啊、信息加载等逻辑。
换个思路,使用编译器来看下ImportSelectors方法都有谁调用的吧。使用强大的编译器可以看到ConfigurationClassParser.class中调用了此方法。
在这里插入图片描述
在processImports方法里,直接调用此方法。那么谁有调用了processImports方法?我只能告诉你,只能通过编译器一层一层的找。。。最终可以定位到这个方法

	@Override
	public void refresh() throws BeansException, IllegalStateException {
	----省略逻辑
	}

这个方法我们见过啊,就是启动Spring中的一个方法。可以在

	SpringApplication.run(ProviderApplication.class, args);

方法中的

	refreshContext(context);

发放中,定位到。好了,找到如何调用此方法了。到现在我们就知道了,这个方法是怎么调用的。那么Spring调用了importselector以后,拿到全路径的类名称之后,又做了什么处理了。继续探索把。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值