【SpringBoot】 之自动配置解析

2019-04-11  本文已影响0人  HansenGuan

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

启动原理分析

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
                ├── ...

----;

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");
            }
        }
上一篇 下一篇

猜你喜欢

热点阅读