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.的图解