美文网首页
【SpringBoot】 之自动配置解析

【SpringBoot】 之自动配置解析

作者: HansenGuan | 来源:发表于2019-04-11 17:34 被阅读0次

    https://www.cnblogs.com/ityouknow/default.html?page=2

    使用jenkins部署Spring Boot项目
    https://www.cnblogs.com/ityouknow/p/7899349.html

    Error Handling for REST with Spring
    https://www.baeldung.com/exception-handling-for-rest-with-spring

    Introduction to Concurrency in Spring Boot
    https://www.e4developer.com/2018/03/30/introduction-to-concurrency-in-spring-boot/


    完整的配置文件:
    https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

    示例:
    https://spring.io/guides

    文档:
    https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/

    ----;

    自动配置原理

    https://blog.csdn.net/xiaobing_122613/article/details/54943448

    自己实现一个 自定义 Starter

    • 添加依赖

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-autoconfigure</artifactId>
         <version>1.5.4.RELEASE</version>
      </dependency>
      
    • 定义 Properties 类。类定义了默认的属性值, @ConfigurationProperties注解会定义一个匹配,如果想修改属性值,可以在application.properties中使用“匹配.属性=修改的值”进行修改

    • 定义服务类。服务类是指主要的功能类,如果没有SpringBoot,这些服务类在Spring中都是需要自己去配置生成的。如SpringMVC中的DispatcherServlet、Mybatis的DataSource等。

    • 自动配置类。自动配置类主要作用是SpringBoot的配置核心,它会写在MEAT-INF/spring.factories中,告知SpringBoot在启动时去读取该类并根据该类的规则进行配置。

      // @EnableConfigurationProperties注解根据TestProperties类开启属性注入,允许在application.properties修改里面的属性值。
      // @ConditionOnClass会检测是否存在TestService类
      // @ConditionOnProperty类会查看是否开启该自动配置。默认开启(true)。
      // @ConditionOnMissingBean会检测容器中是否有TestService类的对象,如果没有则生成一个。
      @Configuration
      @EnableConfigurationProperties(TestProperties.class)
      @ConditionalOnClass(TestService.class)
      @ConditionalOnProperty(prefix = "test" , value = "enabled" , matchIfMissing = true)
      public class TestServiceAutoConfiguration {
          @Autowired
          TestProperties testProperties;
      
          @Bean
          @ConditionalOnMissingBean(TestService.class)
          public TestService testService(){
              TestService testService = new TestService();
              testService.setMsg(testProperties.getMsg());
              return testService;
          }
      }
      
    • 在MEAT-INF文件夹中创建spring.factories文件。诉SpringBoot去读取TestServiceAutoConfiguration类。

      org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
      cn.miaolovezhen.TestServiceAutoConfiguration
      

    启动原理分析

    https://blog.csdn.net/hengyunabc/article/details/50120001
    https://blog.csdn.net/qq_27294625/article/details/77600001

    打包好的 springboot 应用 jar 包目录结构:

    ├── META-INF
    │   ├── MANIFEST.MF
    ├── application.properties
    ├── com
    │   └── example
    │       └── SpringBootDemoApplication.class
    ├── lib
    │   ├── aopalliance-1.0.jar
    │   ├── spring-beans-4.2.3.RELEASE.jar
    │   ├── ...
    └── org
        └── springframework
            └── boot
                └── loader
                    ├── ExecutableArchiveLauncher.class
                    ├── JarLauncher.class
                    ├── JavaAgentDetector.class
                    ├── LaunchedURLClassLoader.class
                    ├── Launcher.class
                    ├── MainMethodRunner.class
                    ├── ...
    
    • MANIFEST.MF

      Manifest-Version: 1.0
      Start-Class: com.example.SpringBootDemoApplication
      Implementation-Vendor-Id: com.example
      Spring-Boot-Version: 1.3.0.RELEASE
      Created-By: Apache Maven 3.3.3
      Build-Jdk: 1.8.0_60
      Implementation-Vendor: Pivotal Software, Inc.
      Main-Class: org.springframework.boot.loader.JarLauncher
      

      Main-Class是org.springframework.boot.loader.JarLauncher ,这个是jar启动的Main函数。
      Start-Class是com.example.SpringBootDemoApplication,这个是我们应用自己的Main函数。

    • org/springframework/boot/loader 目录存放的是Spring boot loader的.class文件。

      在spring boot里,抽象出了 Archive 的概念。

      一个archive可以是一个jar(JarFileArchive),也可以是一个文件目录(ExplodedArchive)。可以理解为Spring boot抽象出来的统一访问资源的层。

      jar:file:/tmp/target/demo-0.0.1-SNAPSHOT.jar!/lib/aopalliance-1.0.jar
      jar:file:/tmp/target/demo-0.0.1-SNAPSHOT.jar!/lib/spring-beans-4.2.3.RELEASE.jar
      

      Spring boot能做到以一个fat jar来启动,最重要的一点是它实现了jar in jar的加载方式。

      原始的JarFile URL是这样子的:

      jar:file:/tmp/target/demo-0.0.1-SNAPSHOT.jar!/
      

      jar包里的资源的URL:

      jar:file:/tmp/target/demo-0.0.1-SNAPSHOT.jar!/com/example/SpringBootDemoApplication.class
      

      可以看到对于Jar里的资源,定义以’!/’来分隔。原始的JarFile URL只支持一个’!/’。
      Spring boot扩展了这个协议,让它支持多个’!/’,就可以表示jar in jar,jar in directory的资源了。

      在构造一个URL时,可以传递一个Handler,而JDK自带有默认的Handler类,应用可以自己注册Handler来处理自定义的URL。Spring boot 自定义URLStreamHandler,扩展JarFile和JarURLConnection, 通过注册了一个自定义的Handler类来处理多重jar in jar的逻辑

    • Spring boot构造LaunchedURLClassLoader时,传递了一个URL[]数组。数组里是lib目录下面的jar的URL。

      JDK或者ClassLoader如何知道怎么读取到里面的内容的?

      1. spring boot注册了一个Handler来处理”jar:”这种协议的URL
      2. spring boot扩展了JarFile和JarURLConnection,内部处理jar in jar的情况
      3. 在处理多重jar in jar的URL时,spring boot会循环处理,并缓存已经加载到的JarFile
      4. 对于多重jar in jar,实际上是解压到了临时目录来处理,可以参考JarFileArchive里的代码
      5. 在获取URL的InputStream时,最终获取到的是JarFile里的JarEntryData

    ----;

    Embead Tomcat的启动流程

    1. 判断是否在web环境,spring boot在启动时,先通过一个简单的查找Servlet类的方式来判断是不是在web环境
    2. 如果是的话,则会创建AnnotationConfigEmbeddedWebApplicationContext,否则Spring context就是AnnotationConfigApplicationContext;
    3. 获取EmbeddedServletContainerFactory的实现类来启动对应的web服务器,常用的两个实现类是TomcatEmbeddedServletContainerFactoryJettyEmbeddedServletContainerFactory

    web 资源访问

    当spring boot应用被打包为一个fat jar时,是如何访问到web resource的?

    实际上是通过Archive提供的URL,然后通过Classloader提供的访问classpath resource的能力来实现的。

    //ResourceProperties
    public class ResourceProperties implements ResourceLoaderAware {
    
        private static final String[] SERVLET_RESOURCE_LOCATIONS = { "/" };
    
        private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
                "classpath:/META-INF/resources/", "classpath:/resources/",
                "classpath:/static/", "classpath:/public/" };
    
    
    //WebMvcAutoConfigurationAdapter
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                Resource page = this.resourceProperties.getWelcomePage();
                if (page != null) {
                    logger.info("Adding welcome page: " + page);
                    registry.addViewController("/").setViewName("forward:index.html");
                }
            }
    

    相关文章

      网友评论

          本文标题:【SpringBoot】 之自动配置解析

          本文链接:https://www.haomeiwen.com/subject/xdefnftx.html