springboot学习之——SpringApplication.run方法
目录
springboot 版本3.1.5
第一步
/
* Static helper that can be used to run a {@link SpringApplication} from the
* specified source using default settings.
* @param primarySource the primary source to load
* @param args the application arguments (usually passed from a Java main method)
* @return the running {@link ApplicationContext}
*/
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
return run(new Class<?>[] { primarySource }, args);
}
注释说的啥屁话,这玩意返回一个可配置的应用上下文ConfigurableApplicationContext。看这种名字的类就知道不简单。
run()方法其实创建了一个新的SpringApplication。这个新的SpringApplication会被添加一些属性。
到这里就没了,实例化SpringApplication之后,返回的是更加具体的ConfigurableApplicationContext,也就是说,后面的工作交给了ConfigurableApplicationContext,所以要研究它了。
/
* Create a new {@link SpringApplication} instance. The application context will load * beans from the specified primary sources (see {@link SpringApplication class-level} * documentation for details). The instance can be customized before calling * {@link #run(String...)}. * @param resourceLoader the resource loader to use * @param primarySources the primary bean sources * @see #run(Class, String[]) * @see #setSources(Set) / @SuppressWarnings({ "unchecked", "rawtypes" }) public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) { this.resourceLoader = resourceLoader;//null Assert.notNull(primarySources, "PrimarySources must not be null"); this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));//这个资源类就是自己 this.webApplicationType = WebApplicationType.deduceFromClasspath();//推断使用什么类型的web应用。我们经常用WEBMVC_INDICATOR_CLASS this.bootstrapRegistryInitializers = new ArrayList<>( getSpringFactoriesInstances(BootstrapRegistryInitializer.class));//启动注册实例化 setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class)); setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));//监听器 this.mainApplicationClass = deduceMainApplicationClass();//主应用类(就是自己项目的启动类) }
第二步
ConfigurableApplicationContext
/
* SPI interface to be implemented by most if not all application contexts.
* Provides facilities to configure an application context in addition
* to the application context client methods in the
* {@link org.springframework.context.ApplicationContext} interface.
* <p>Configuration and lifecycle methods are encapsulated here to avoid
* making them obvious to ApplicationContext client code. The present
* methods should only be used by startup and shutdown code.
* @author Juergen Hoeller
* @author Chris Beams
* @author Sam Brannen
* @since 03.11.2003
*/
public interface ConfigurableApplicationContext extends ApplicationContext, Lifecycle, Closeable {
}
SPI接口(串行外设接口,芯片和外设通信用的),实现了大多数应用上下文的SPI接口。注释翻译了结果还是配置应用上下文。
前面的run()创建了它,程序运行实际是用它来执行的。然而它还是接口,还是要看谁实现了它。
实现它的类也很多。找到一个我们熟悉的看看。AnnotationConfigServletWebApplicationContext
springboot学习之——SpringApplication.run方法/
* Create a new {@link AnnotationConfigServletWebApplicationContext}, deriving bean * definitions from the given annotated classes and automatically refreshing the * context. * @param annotatedClasses one or more annotated classes, e.g. {@code @Configuration} * classes
/ public AnnotationConfigServletWebApplicationContext(Class<?>... annotatedClasses //注册被注解的类) { this(); register(annotatedClasses); refresh(); }