77 lines
2.7 KiB
Markdown
77 lines
2.7 KiB
Markdown
|
```java
|
|||
|
SpringApplication.run(PrimarySources.class, args);
|
|||
|
```
|
|||
|
|
|||
|
```java
|
|||
|
new SpringApplication(primarySources)
|
|||
|
setInitializers ApplicationContextInitializer.class
|
|||
|
setListeners ApplicationListener.class
|
|||
|
Class.forName(stackTraceElement.getClassName())
|
|||
|
private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
|
|||
|
MultiValueMap<String, String> result = (MultiValueMap)cache.get(classLoader);
|
|||
|
if (result != null) {
|
|||
|
return result;
|
|||
|
} else {
|
|||
|
try {
|
|||
|
Enumeration<URL> urls = classLoader != null ? classLoader.getResources("META-INF/spring.factories") : ClassLoader.getSystemResources("META-INF/spring.factories");
|
|||
|
LinkedMultiValueMap result = new LinkedMultiValueMap();
|
|||
|
|
|||
|
while(urls.hasMoreElements()) {
|
|||
|
URL url = (URL)urls.nextElement();
|
|||
|
UrlResource resource = new UrlResource(url);
|
|||
|
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
|
|||
|
Iterator var6 = properties.entrySet().iterator();
|
|||
|
|
|||
|
while(var6.hasNext()) {
|
|||
|
Entry<?, ?> entry = (Entry)var6.next();
|
|||
|
String factoryClassName = ((String)entry.getKey()).trim();
|
|||
|
String[] var9 = StringUtils.commaDelimitedListToStringArray((String)entry.getValue());
|
|||
|
int var10 = var9.length;
|
|||
|
|
|||
|
for(int var11 = 0; var11 < var10; ++var11) {
|
|||
|
String factoryName = var9[var11];
|
|||
|
result.add(factoryClassName, factoryName.trim());
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
cache.put(classLoader, result);
|
|||
|
return result;
|
|||
|
} catch (IOException var13) {
|
|||
|
throw new IllegalArgumentException("Unable to load factories from location [META-INF/spring.factories]", var13);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
1. 设置资源加载(包含类加载器)
|
|||
|
2. 设置资源primarySources Map
|
|||
|
3. 设置web类型
|
|||
|
4. 设置工厂实例ApplicationContextInitializer,META-INF/spring.factories
|
|||
|
5. 设置监听ApplicationListener
|
|||
|
5. 加载类,创建实例,排序。
|
|||
|
6. 找到main方法的类
|
|||
|
|
|||
|
```java
|
|||
|
public ClassLoader getClassLoader() {
|
|||
|
if (this.resourceLoader != null) {
|
|||
|
return this.resourceLoader.getClassLoader();
|
|||
|
}
|
|||
|
return ClassUtils.getDefaultClassLoader();
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
```java
|
|||
|
// Run the Spring application, creating and refreshing a new ApplicationContext.
|
|||
|
.run(args)
|
|||
|
```
|
|||
|
|
|||
|
```java
|
|||
|
listeners.starting()
|
|||
|
```
|
|||
|
|
|||
|
```java
|
|||
|
DefaultApplicationArguments (new SimpleCommandLineArgsParser()).parse(args)
|
|||
|
```
|
|||
|
|