springboot启动加载SpringApplication.run(TradeApplication.class, args)

21 篇文章 0 订阅

Top

public static void main(String[] args) throws Exception {
		SpringApplication.run(new Class<?>[0], args);
}
---------------------------------
public static ConfigurableApplicationContext run(Class<?>[] primarySources,String[] args) {
		return new SpringApplication(primarySources).run(args);
}
// 
通过主要资源实例化spring应用
new SpringApplication(primarySources) 
有参构造器
public SpringApplication(Class<?>... primarySources) {
		this(null, primarySources);
}

在这里插入图片描述

创建一个新的SpringApplication实例。
将加载应用程序上下文来自指定主源的bean。
将主要资源塞进去set中
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
		this.resourceLoader = resourceLoader;
		Assert.notNull(primarySources, "PrimarySources must not be null");
		this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
		//减少web应用类型 有三种 
		//NONE The application should not run as a web application and should not start an embedded web server. 
		//SERVLET(默认)	The application should run as a servlet-based web application and should start an embedded servlet web server.
		//REACTIVEThe application should run as a reactive web application and should start an embedded reactive web server.
		this.webApplicationType = deduceWebApplicationType();
		//采用servlet 里面使用到ClassUtils.isPresent 判断当前class loader中是否存在对应的类型了
		//ClassUtils 类参考学习https://www.cnblogs.com/jixp/articles/9310403.html
//https://vimsky.com/examples/detail/java-method-org.springframework.util.ClassUtils.isPresent.html
		setInitializers((Collection) getSpringFactoriesInstances(
				ApplicationContextInitializer.class));
		//getSpringFactoriesInstances()方法详解,一般都是解析META-INF/spring.factories文件下  这个方法的作用应该就是获取指定类型的实例集合。
		//1.获取当前线程的类加载器
		//2.通过类org.springframework.context.ApplicationContextInitializer和类加载器获取Set<String> names 名字 如 name里面属性如org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer等
			//(1)从private static final Map<ClassLoader, MultiValueMap<String, String>> cache = new ConcurrentReferenceHashMap<>() 中通过类加载器获取缓存 有就直接返回
			//(2)没有就自己获取 
		//3.创建Spring工厂单例list instances
		//4.排序instances 返回instances
		
		//获取ApplicationListener.class的spring工厂单例
		setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
		this.mainApplicationClass = deduceMainApplicationClass();
	}

//2.的代码
public static List<String> loadFactoryNames(Class<?> factoryClass, @Nullable ClassLoader classLoader) {
		String factoryClassName = factoryClass.getName();
		return loadSpringFactories(classLoader).getOrDefault(factoryClassName, Collections.emptyList());
		//Java8中Map接口的getOrDefault方法 如果第一参数有就返回第一个参数的value 否则使用第二个参数返回
}
private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
		MultiValueMap<String, String> result = cache.get(classLoader);
		if (result != null) {
			return result;
		}

		try {
		//读这里的META-INF/spring.factories
			Enumeration<URL> urls = (classLoader != null ?					classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
			result = new LinkedMultiValueMap<>();
			while (urls.hasMoreElements()) {
				URL url = urls.nextElement();
				UrlResource resource = new UrlResource(url);
				Properties properties = PropertiesLoaderUtils.loadProperties(resource);
				for (Map.Entry<?, ?> entry : properties.entrySet()) {
					List<String> factoryClassNames = Arrays.asList(
							StringUtils.commaDelimitedListToStringArray((String) entry.getValue()));
					result.addAll((String) entry.getKey(), factoryClassNames);
				}
			}
			cache.put(classLoader, result);
			return result;
		}
		catch (IOException ex) {
			throw new IllegalArgumentException("Unable to load factories from location [" +
					FACTORIES_RESOURCE_LOCATION + "]", ex);
		}
	}

//3.的代码
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 {
			//ClassUtils.forName()---https://blog.csdn.net/qq_21399231/article/details/80751489
			//https://blog.csdn.net/songbing_/article/details/113782158
				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;
	}

上面2.的图解
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值