springboot启动流程
SpringBoot流程 启动
2023-09-27 14:28:06 时间
关键源码
1 public ConfigurableApplicationContext run(String... args) { 2 StopWatch stopWatch = new StopWatch(); 3 stopWatch.start(); 4 ConfigurableApplicationContext context = null; 5 Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>(); 6 configureHeadlessProperty(); 7 // SpringApplicationRunListener作用:用户可以根据该接口,在各个流程中加入自己的逻辑(自定义实现该接口) 8 SpringApplicationRunListeners listeners = getRunListeners(args); // 通过spi,根据spring.factories获取监听器:1)加载class文件;2)通过反射创建监听对象 9 listeners.starting(); // 发布ApplicationStartingEvent 10 try { 11 ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); 12 ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); 13 configureIgnoreBeanInfo(environment); 14 Banner printedBanner = printBanner(environment); 15 context = createApplicationContext(); 16 exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class, 17 new Class[] { ConfigurableApplicationContext.class }, context); 18 prepareContext(context, environment, listeners, applicationArguments, printedBanner); 19 refreshContext(context); 20 afterRefresh(context, applicationArguments); 21 stopWatch.stop(); 22 if (this.logStartupInfo) { 23 new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch); 24 } 25 listeners.started(context); 26 callRunners(context, applicationArguments); 27 } 28 catch (Throwable ex) { 29 handleRunFailure(context, ex, exceptionReporters, listeners); 30 throw new IllegalStateException(ex); 31 } 32 33 try { 34 listeners.running(context); 35 } 36 catch (Throwable ex) { 37 handleRunFailure(context, ex, exceptionReporters, null); 38 throw new IllegalStateException(ex); 39 } 40 return context; 41 }
其中,根据spring.factories的方法如下:
1 private <T> List<T> createSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, 2 ClassLoader classLoader, Object[] args, Set<String> names) { 3 List<T> instances = new ArrayList<>(names.size()); 4 for (String name : names) { 5 try { 6 // 加载class文件 7 Class<?> instanceClass = ClassUtils.forName(name, classLoader); 8 Assert.isAssignable(type, instanceClass); 9 // 通过反射创建对象 10 Constructor<?> constructor = instanceClass.getDeclaredConstructor(parameterTypes); 11 T instance = (T) BeanUtils.instantiateClass(constructor, args); 12 instances.add(instance); 13 } 14 catch (Throwable ex) { 15 throw new IllegalArgumentException("Cannot instantiate " + type + " : " + name, ex); 16 } 17 } 18 return instances; 19 }
相关文章
- 2022 11月14日 每日面试题(SpringBoot)
- 可能是全网最全的SpringBoot启动流程源码分析(基于 2.1.5 版本)
- springboot整合shiro的认证与授权流程
- springboot使用spring initlize创建web项目
- 第十二章 springboot + mongodb(复杂查询)
- springboot属性类自动加载配置文件中的值
- SpringBoot和SpringCloud配置
- SpringBoot入门系列~Spring-Data-JPA自动建表
- Springboot+Redis序列化坑
- SpringBoot自定义Filter
- SpringBoot和Mybatis的整合
- 【项目精选】基于 SpringBoot + Vue + ElementUI 的人力资源管理系统
- 3-3. SpringBoot项目集成【用户身份认证】实战 【全流程篇】基于JWT+双重检查的登录+登出+拦截器