springboot知识点整合

springboot是什么

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。

在我看来springboot最大好处就是简化配置,以前一个SpringMvc项目需要在xml配置各种东西,现在简化了很多配置,提高了开发效率

springboot是如何自动配置的

一般我的启动类会有@SpringBootApplication注解,这是一个组合注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
    @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
    @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

这里比较重要的是@EnableAutoConfiguration

@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
}

@Import是Spring提供的,会注入后面这个类AutoConfigurationImportSelector

其中方法调用为selectImports->getAutoConfigurationEntry->getCandidateConfigurations

往里面看会将META-INF/spring.factories文件里需要的类型获取

获取类的方法会在springboot启动的时候AbstractApplicationContext.refresh()调用。

springboot的启动流程

以tomcat为例来看看springboot的启动加载流程

我们一般会运行main方法SpringApplication.run

先看看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));
  // 获取类型
   this.webApplicationType = WebApplicationType.deduceFromClasspath();
  // 获取初始化的类并set进去
   setInitializers((Collection) getSpringFactoriesInstances(
         ApplicationContextInitializer.class));
  // 获取监听器
   setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
   this.mainApplicationClass = deduceMainApplicationClass();
}

接下来我们主要看springboot的run方法

public ConfigurableApplicationContext run(String... args) {
  // 计时器
   StopWatch stopWatch = new StopWatch();
   stopWatch.start();
   ConfigurableApplicationContext context = null;
   Collection exceptionReporters = new ArrayList<>();
   configureHeadlessProperty();
   SpringApplicationRunListeners listeners = getRunListeners(args);
   listeners.starting();
   try {
      ApplicationArguments applicationArguments = new DefaultApplicationArguments(
            args);
      ConfigurableEnvironment environment = prepareEnvironment(listeners,
            applicationArguments);
      configureIgnoreBeanInfo(environment);
     // 打印banner
      Banner printedBanner = printBanner(environment);
     // 创建context容器
     // AnnotationConfigServletWebServerApplicationContext
      context = createApplicationContext();
      exceptionReporters = getSpringFactoriesInstances(
            SpringBootExceptionReporter.class,
            new Class[] { ConfigurableApplicationContext.class }, context);
      prepareContext(context, environment, listeners, applicationArguments,
            printedBanner);
     // 刷新上下文
      refreshContext(context);
      afterRefresh(context, applicationArguments);
      stopWatch.stop();
      if (this.logStartupInfo) {
         new StartupInfoLogger(this.mainApplicationClass)
               .logStarted(getApplicationLog(), stopWatch);
      }
      listeners.started(context);
      callRunners(context, applicationArguments);
   }
   catch (Throwable ex) {
      handleRunFailure(context, ex, exceptionReporters, listeners);
      throw new IllegalStateException(ex);
   }

   try {
      listeners.running(context);
   }
   catch (Throwable ex) {
      handleRunFailure(context, ex, exceptionReporters, null);
      throw new IllegalStateException(ex);
   }
   return context;
}

我们主要看refreshContext方法,他会调用父类AbstractApplicationContextrefresh方法,里面会调用子类的onRefresh方法,这是一个模板方法模式。接下来看

protected void onRefresh() {
   super.onRefresh();
   try {
      // 创建web容器
      createWebServer();
   }
   catch (Throwable ex) {
      throw new ApplicationContextException("Unable to start web server", ex);
   }
}

createWebServer方法会创建web容器接下来就能看见创建tomcat的代码了

public WebServer getWebServer(ServletContextInitializer... initializers) {
   Tomcat tomcat = new Tomcat();
   File baseDir = (this.baseDirectory != null) ? this.baseDirectory
         : createTempDir("tomcat");
   tomcat.setBaseDir(baseDir.getAbsolutePath());
   Connector connector = new Connector(this.protocol);
   tomcat.getService().addConnector(connector);
   customizeConnector(connector);
   tomcat.setConnector(connector);
   tomcat.getHost().setAutoDeploy(false);
   configureEngine(tomcat.getEngine());
   for (Connector additionalConnector : this.additionalTomcatConnectors) {
      tomcat.getService().addConnector(additionalConnector);
   }
   prepareContext(tomcat.getHost(), initializers);
   return getTomcatWebServer(tomcat);
}

参考

https://mp.weixin.qq.com/s/axRia4KH1YUSVI3WcOfWPg

https://mp.weixin.qq.com/s/TnVL1chwTqlXmlOdcuJCSg

https://mp.weixin.qq.com/s/y6pUTKRCucoeCGmgqwRDow

展开阅读全文

页面更新:2024-02-11

标签:目的   监听器   组合   子类   计时器   上下文   知识点   注解   初始化   样板   容器   框架   加载   流程   类型   方法   科技

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top