springboot启动流程

2021-03-24 01:27

阅读:540

标签:listen   util   sas   gap   obj   err   cat   info   nfa   

关键源码

 1 public ConfigurableApplicationContext run(String... args) {
 2     StopWatch stopWatch = new StopWatch();
 3     stopWatch.start();
 4     ConfigurableApplicationContext context = null;
 5     Collection 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  List createSpringFactoriesInstances(Class type, Class>[] parameterTypes,
 2             ClassLoader classLoader, Object[] args, Set names) {
 3     List 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 }

 

springboot启动流程

标签:listen   util   sas   gap   obj   err   cat   info   nfa   

原文地址:https://www.cnblogs.com/listened/p/13830635.html


评论


亲,登录后才可以留言!