Spring Boot2 源码 SpringApplication构造方法(一)

SpringApplication run方法 getRunListeners监听器 

  1. 指定web环境类型
  2. 加载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

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值