SpringBoot-源码学习<一> SpringApplication初始化

SpringBoot判断该应用是已那种方式启动
WebApplicationType webApplicationType = REACTIVE/NONE/SERVLET;

/*
	REACTIVE:Jar包存在DispatcherHandler并且不存在DispatcherServlet和ServletContainer则为响应式web应用;内嵌的响应式web服务
	NONE:Jar包不存在Servlet和ConfigurableWebApplicationContext则为普通应用程序;不启动内嵌的服务
	SERVLET :其他情况则为基于servlet的web应用,需加载并启动内嵌的web web服务;内嵌servlet web服务
*/
static WebApplicationType deduceFromClasspath() {
		//isPresent底层是通过Class.forName()是否异常来判断	
		if (ClassUtils.isPresent("DispatcherHandler", null) && !ClassUtils.isPresent("DispatcherServlet", null)
				&& !ClassUtils.isPresent("ServletContainer", null)) {
			return WebApplicationType.REACTIVE;
		}
		for (String className : "Servlet,ConfigurableWebApplicationContext") {
			if (!ClassUtils.isPresent(className, null)) {
				return WebApplicationType.NONE;
			}
		}
		return WebApplicationType.SERVLET;
	}

根据ClassLoader获取SpringFactoriesLoader.cache里的数据
如果数据为空默认获取 spring.factories 配置文件中的所有配置类,放入SpringFactoriesLoader.cache

static final Map<ClassLoader, Map<String, List<String>>> cache = new ConcurrentReferenceHashMap<>();
private static Map<String, List<String>> loadSpringFactories(ClassLoader classLoader) {
		Map<String, List<String>> result = cache.get(classLoader);
		if (result != null) {
			return result;
		}
		result = new HashMap<>();
		try {
			Enumeration<URL> urls = classLoader.getResources(FACTORIES_RESOURCE_LOCATION);
			while (urls.hasMoreElements()) {
				URL url = urls.nextElement();
				UrlResource resource = new UrlResource(url);
				Properties properties = PropertiesLoaderUtils.loadProperties(resource);
				for (Map.Entry<?, ?> entry : properties.entrySet()) {
					String factoryTypeName = ((String) entry.getKey()).trim();
					String[] factoryImplementationNames =
							StringUtils.commaDelimitedListToStringArray((String) entry.getValue());
					for (String factoryImplementationName : factoryImplementationNames) {
						result.computeIfAbsent(factoryTypeName, key -> new ArrayList<>())
								.add(factoryImplementationName.trim());
					}
				}
			}

			// Replace all lists with unmodifiable lists containing unique elements
			result.replaceAll((factoryType, implementations) -> implementations.stream().distinct()
					.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList)));
			cache.put(classLoader, result);
		}
		catch (IOException ex) {
			throw new IllegalArgumentException("Unable to load factories from location [" +
					FACTORIES_RESOURCE_LOCATION + "]", ex);
		}
		return result;
	}

从SpringFactoriesLoader.cache获取key为ApplicationContextInitializer所有value类并通过反射执行,并放入->List<ApplicationContextInitializer<?>> initializers;
从SpringFactoriesLoader.cache获取key为ApplicationListener所有value类并通过反射执行,并放入->List<ApplicationListener<?>> listeners;;

private <T> List<T> createSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes,
			ClassLoader classLoader, Object[] args, Set<String> names) {
		List<T> instances = new ArrayList<>(names.size());
		for (String name : names) {
			try {
				Class<?> instanceClass = ClassUtils.forName(name, classLoader);
				Assert.isAssignable(type, instanceClass);
				Constructor<?> constructor = instanceClass.getDeclaredConstructor(parameterTypes);
				T instance = (T) BeanUtils.instantiateClass(constructor, args);
				instances.add(instance);
			}
			catch (Throwable ex) {
				throw new IllegalArgumentException("Cannot instantiate " + type + " : " + name, ex);
			}
		}
		return instances;
	}

通过堆栈获取启动类并赋值给->Class<?> mainApplicationClass;

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;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值