本文主要是介绍Spring Boot2 源码 SpringApplication构造方法(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
SpringApplication run方法 getRunListeners监听器
- 指定web环境类型
- 加载META-INF/spring.factories 获取initializers和listeners
本文采用2.1.3.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构造方法
跟踪SpringApplication构造方法
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这里设置好web环境类型设置为SERVLET,后面会根据次类型创建上下文
getSpringFactoriesInstances方法会加载META-INF/spring.factories
ApplicationContextInitializer
是spring组件spring-context
组件中的一个接口,主要是spring ioc容器刷新之前的一个回调接口,用于处于自定义逻辑
ApplicationListener.class监听器,本示例是12个监听器 后面会用到
初始化器实例
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;}
deduceMainApplicationClass返回入口应用类
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();返回表示此线程的堆栈转储的堆栈跟踪元素数组。
org.springframework.boot.SpringApplication.run(SpringApplication.java:1260), org.springframework.boot.SpringApplication.run(SpringApplication.java:1248), com.bjsdzk.api.BjsdzkApiApplication.main(BjsdzkApiApplication.java:17)]k.boot.context.config.DelegatingApplicationListener@af27d7e, org.springframework.boot.builder.ParentContextCloserApplicationListener@593095e5, org.springframework.boot.ClearCachesApplicationListener@6cedc76, org.springframework.boot.context.FileEncodingApplicationListener@755aa205, org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener@6cda385d, org.springframework.boot.devtools.logger.DevToolsLogFactory$Listener@3118079e]
找到main方法的类BjsdzkApiApplication
这篇关于Spring Boot2 源码 SpringApplication构造方法(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!