Spring cloud启动一

##2.1.4.RELEASE版本源代码做为参考

SpringApplication 对象的 run 方法的源码和运行流程。

	public static void main(String[] args) {
		SpringApplication.run(BjsdzkApiApplication.class, args);
//		http://localhost:63336/api/doc.html
//		http://localhost:63336/api/swagger-ui.html
	}

进入run方法

	public static ConfigurableApplicationContext run(Class<?>[] primarySources,
			String[] args) {
		return new SpringApplication(primarySources).run(args);
	}

这里开始初始化,查看SpringApplication.class构造函数:

	public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
		this.resourceLoader = resourceLoader;
		Assert.notNull(primarySources, "PrimarySources must not be null");
		this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
                //设置servlet环境 
		this.webApplicationType = WebApplicationType.deduceFromClasspath();
                //获取ApplicationContextInitializer,也是在这里开始首次加载spring.factories文件
		setInitializers((Collection) getSpringFactoriesInstances(
				ApplicationContextInitializer.class));
                //获取监听器,这里是第二次加载spring.factories文件
		setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
		this.mainApplicationClass = deduceMainApplicationClass();
	}

查看断点信息:
在这里插入图片描述
标记几个重点方法:
this.webApplicationType = WebApplicationType.deduceFromClasspath();
在这里插入图片描述
这一步设计环境,最终类型为:SERVLET
下一步:
this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));这一步获取ApplicationContextInitializer,也是在这里开始首次加载spring.factories文件,主要是加载容器初始化需要的类(ApplicationContextInitializer是spring组件spring-context组件中的一个接口,主要是spring ioc容器刷新之前的一个回调接口,用于处于自定义逻辑)
在这里插入图片描述

内部声明ApplicationListener.class,进入查看源码:声明了触发的事件

@FunctionalInterface
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
    void onApplicationEvent(E var1);
}

再看getSpringFactoriesInstances方法,分了俩步,一步加载(loadSpringFactories),一步创建(createSpringFactoriesInstances)
在这里插入图片描述
查看代码

private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) {
        ClassLoader classLoader = this.getClassLoader();
        Set<String> names = new LinkedHashSet(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
        List<T> instances = this.createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);
        AnnotationAwareOrderComparator.sort(instances);
        return instances;
    }

进入loadSpringFactories方法

private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
        ......

                while(urls.hasMoreElements()) {
                    URL url = (URL)urls.nextElement();
                    UrlResource resource = new UrlResource(url);
                    Properties properties = PropertiesLoaderUtils.loadProperties(resource);
                    Iterator var6 = properties.entrySet().iterator();
	.....
}

URL url = (URL)urls.nextElement();
UrlResource resource = new UrlResource(url);便是加载META-INF/spring.factories 的url(class)
在这里插入图片描述
回头再看下createSpringFactoriesInstances方法

    private <T> List<T> createSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, ClassLoader classLoader, Object[] args, Set<String> names) {
        List<T> instances = new ArrayList(names.size());
        Iterator var7 = names.iterator();

        while(var7.hasNext()) {
            String name = (String)var7.next();

            try {
                Class<?> instanceClass = ClassUtils.forName(name, classLoader);
                Assert.isAssignable(type, instanceClass);
                Constructor<?> constructor = instanceClass.getDeclaredConstructor(parameterTypes);
                T instance = BeanUtils.instantiateClass(constructor, args);
                instances.add(instance);
            } catch (Throwable var12) {
                throw new IllegalArgumentException("Cannot instantiate " + type + " : " + name, var12);
            }
        }

        return instances;
    }

拿到url初始化器实例并返回
好的,这里我们回头到初始化方法里的倒数第二步this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class))方法,过程同上,该方法获取ApplicationListener.class监听器,这里是第二次加载spring.factories文件
最终加载了13个Listener
在这里插入图片描述
最后一步终:返回入口应用类

private Class<?> deduceMainApplicationClass() {
		try {
			StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
			for (StackTraceElement stackTraceElement : stackTrace) {
				if ("main".equals(stackTraceElement.getMethodName())) {
					return Class.forName(stackTraceElement.getClassName());
				}
			}
		}
		catch (ClassNotFoundException ex) {
			// Swallow and continue
		}
		return null;
	}

StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();返回表示此线程的堆栈转储的堆栈跟踪元素数组。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值