Spring Boot 加载过程(自动配置及web服务)

简介: Spring Boot 加载过程(自动配置及web服务)

Spring Boot 加载过程(自动配置及web服务)

运行环境

IntelliJ IDEA 2019.2 (Community Edition)

Java 1.8.0_131

Maven apache-maven-3.5.4

Spring Boot  spring-boot-2.2.14.BUILD-SNAPSHOT

引入Spring Boot 依赖

pom.xml

<!-- 第一种方式 -->

<parent>

   <groupId>org.springframework.boot</groupId>

   <artifactId>spring-boot-starter-parent</artifactId>

   <version>2.2.14.BUILD-SNAPSHOT</version>

</parent>

<!-- 第二种方式 -->

<dependencyManagement>

   <dependencies>

       <dependency>

           <!-- Import dependency management from Spring Boot -->

           <groupId>org.springframework.boot</groupId>

           <artifactId>spring-boot-dependencies</artifactId>

           <version>2.2.14.BUILD-SNAPSHOT</version>

           <type>pom</type>

           <scope>import</scope>

       </dependency>

   </dependencies>

</dependencyManagement>

项目入口

importorg.springframework.boot.SpringApplication;

importorg.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

publicclassTestApplication {

   publicstaticvoidmain(String[] args) {

       SpringApplication.run(TestApplication.class, args);

   }

}

@SpringBootApplication

@SpringBootApplication的源码,可以看到里面组合了三个注解:@ComponentScan,@SpringBootConfiguration,@EnableAutoConfiguration

@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) })

public@interfaceSpringBootApplication {

// ...

}

@ComponentScan

/**may be specified to define specific packages to scan. If specific

* packages are not defined, scanning will occur from the package of the

* class that declares this annotation.

可以指定来定义要扫描的特定包。如果未定义特定的包,则将从声明此注释的类的包中进行扫描。

*/

public@interfaceComponentScan {

//...

}

@SpringBootConfiguration

/**

* Indicates that a class provides Spring Boot application

* {@link Configuration @Configuration}. Can be used as an alternative to the Spring's

* standard {@code @Configuration} annotation so that configuration can be found

* automatically (for example in tests).

指示类提供Spring引导应用程序。可以作为Spring的标准{@code@Configuration}注释的替代,以便可以找到自动配置(例如在测试中)。

*/

@Configuration

public@interfaceSpringBootConfiguration {

//...

}

@EnableAutoConfiguration

@Import(AutoConfigurationImportSelector.class)

public@interfaceEnableAutoConfiguration {

//...

}

@EnableAutoConfiguration是springboot实现自动化配置的核心注解,通过这个注解把spring应用所需的bean注入容器中。@EnableAutoConfiguration源码通过@Import注入了一个ImportSelector的实现类 AutoConfigurationImportSelector,这个ImportSelector最终实现动态加载。 AutoConfigurationImportSelector完成动态加载过程如下:

org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.AutoConfigurationGroup#process

org.springframework.boot.autoconfigure.AutoConfigurationImportSelector#getAutoConfigurationEntry

org.springframework.boot.autoconfigure.AutoConfigurationImportSelector#getCandidateConfigurations

org.springframework.core.io.support.SpringFactoriesLoader#loadFactoryNames

publicfinalclassSpringFactoriesLoader {

   publicstaticfinalStringFACTORIES_RESOURCE_LOCATION="META-INF/spring.factories";

   privatestaticMap<String, List<String>>loadSpringFactories(@NullableClassLoaderclassLoader) {

       MultiValueMap<String, String>result=cache.get(classLoader);

       if (result!=null) {

           returnresult;

       }

       try {

           Enumeration<URL>urls= (classLoader!=null?

                   classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :

                   ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));

           result=newLinkedMultiValueMap<>();

           while (urls.hasMoreElements()) {

               URLurl=urls.nextElement();

               UrlResourceresource=newUrlResource(url);

               Propertiesproperties=PropertiesLoaderUtils.loadProperties(resource);

               for (Map.Entry<?, ?>entry : properties.entrySet()) {

                   StringfactoryTypeName= ((String) entry.getKey()).trim();

                   for (StringfactoryImplementationName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {

                       result.add(factoryTypeName, factoryImplementationName.trim());

                   }

               }

           }

           cache.put(classLoader, result);

           returnresult;

       }

       catch (IOExceptionex) {

           thrownewIllegalArgumentException("Unable to load factories from location ["+

                   FACTORIES_RESOURCE_LOCATION+"]", ex);

       }

   }

}

// 在classpath下所有的META-INF/spring.factories文件中查找org.springframework.boot.autoconfigure.EnableAutoConfiguration的值,并将其封装到一个List中返回,并将其加载到Spring 容器中。

ServletWebServerFactory  加载过程

首先在META-INF/spring.factories文件中可以找到 org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration类,Spring会在项目启动的时候,将其加载到Spring容器中。

@Configuration(proxyBeanMethods=false)

@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)

@ConditionalOnClass(ServletRequest.class)

@ConditionalOnWebApplication(type=Type.SERVLET)

@EnableConfigurationProperties(ServerProperties.class)

@Import({ ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class,

       ServletWebServerFactoryConfiguration.EmbeddedTomcat.class,

       ServletWebServerFactoryConfiguration.EmbeddedJetty.class,

       ServletWebServerFactoryConfiguration.EmbeddedUndertow.class })

publicclassServletWebServerFactoryAutoConfiguration {

}

ServletWebServerFactoryAutoConfiguration类通过Import的方式将EmbeddedTomcat加载到容器中。

@Configuration(proxyBeanMethods=false)

classServletWebServerFactoryConfiguration {

   @Configuration(proxyBeanMethods=false)

    //根据条件进行加载 tomcatServletWebServerFactory

   @ConditionalOnClass({ Servlet.class, Tomcat.class, UpgradeProtocol.class })

   @ConditionalOnMissingBean(value=ServletWebServerFactory.class, search=SearchStrategy.CURRENT)

   staticclassEmbeddedTomcat {

       @Bean

       TomcatServletWebServerFactorytomcatServletWebServerFactory(

               ObjectProvider<TomcatConnectorCustomizer>connectorCustomizers,

               ObjectProvider<TomcatContextCustomizer>contextCustomizers,

               ObjectProvider<TomcatProtocolHandlerCustomizer<?>>protocolHandlerCustomizers) {

                   TomcatServletWebServerFactoryfactory=newTomcatServletWebServerFactory();

                   factory.getTomcatConnectorCustomizers()

                           .addAll(connectorCustomizers.orderedStream().collect(Collectors.toList()));

                   factory.getTomcatContextCustomizers()

                           .addAll(contextCustomizers.orderedStream().collect(Collectors.toList()));

                   factory.getTomcatProtocolHandlerCustomizers()

                           .addAll(protocolHandlerCustomizers.orderedStream().collect(Collectors.toList()));

                   returnfactory;

       }

   }

}

项目启动需要初始化SpringApplication类。构造方法会先进行环境类型的判断

org.springframework.boot.SpringApplication#SpringApplication(org.springframework.core.io.ResourceLoader, java.lang.Class<?>...)

@SuppressWarnings({ "unchecked", "rawtypes" })

publicSpringApplication(ResourceLoaderresourceLoader, Class<?>... primarySources) {

   this.resourceLoader=resourceLoader;

   Assert.notNull(primarySources, "PrimarySources must not be null");

   this.primarySources=newLinkedHashSet<>(Arrays.asList(primarySources));

   // 判断环境类型

   this.webApplicationType=WebApplicationType.deduceFromClasspath();

   setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));

   setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));

   this.mainApplicationClass=deduceMainApplicationClass();

}

#在通过以下调用过程最终通过createWebServer创建Web服务

org.springframework.boot.SpringApplication#refreshContext

org.springframework.boot.SpringApplication#refresh

org.springframework.context.support.AbstractApplicationContext#refresh

org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext#onRefresh

org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext#createWebServer

privatevoidcreateWebServer() {

   WebServerwebServer=this.webServer;

   ServletContextservletContext=getServletContext();

   if (webServer==null&&servletContext==null) {

       // 获取 web 容器

       ServletWebServerFactoryfactory=getWebServerFactory();

       this.webServer=factory.getWebServer(getSelfInitializer());

   }

   elseif (servletContext!=null) {

       try {

           getSelfInitializer().onStartup(servletContext);

       }

       catch (ServletExceptionex) {

           thrownewApplicationContextException("Cannot initialize servlet context", ex);

       }

   }

   initPropertySources();

}


相关文章
|
3月前
|
负载均衡 监控 Java
Spring Cloud Gateway 全解析:路由配置、断言规则与过滤器实战指南
本文详细介绍了 Spring Cloud Gateway 的核心功能与实践配置。首先讲解了网关模块的创建流程,包括依赖引入(gateway、nacos 服务发现、负载均衡)、端口与服务发现配置,以及路由规则的设置(需注意路径前缀重复与优先级 order)。接着深入解析路由断言,涵盖 After、Before、Path 等 12 种内置断言的参数、作用及配置示例,并说明了自定义断言的实现方法。随后重点阐述过滤器机制,区分路由过滤器(如 AddRequestHeader、RewritePath、RequestRateLimiter 等)与全局过滤器的作用范围与配置方式,提
Spring Cloud Gateway 全解析:路由配置、断言规则与过滤器实战指南
|
3月前
|
Java 关系型数据库 MySQL
Spring Boot自动配置:魔法背后的秘密
Spring Boot 自动配置揭秘:只需简单配置即可启动项目,背后依赖“约定大于配置”与条件化装配。核心在于 `@EnableAutoConfiguration` 注解与 `@Conditional` 系列条件判断,通过 `spring.factories` 或 `AutoConfiguration.imports` 加载配置类,实现按需自动装配 Bean。
|
3月前
|
人工智能 Java 开发者
【Spring】原理解析:Spring Boot 自动配置
Spring Boot通过“约定优于配置”的设计理念,自动检测项目依赖并根据这些依赖自动装配相应的Bean,从而解放开发者从繁琐的配置工作中解脱出来,专注于业务逻辑实现。
1304 0
|
2月前
|
前端开发 Java 应用服务中间件
《深入理解Spring》 Spring Boot——约定优于配置的革命者
Spring Boot基于“约定优于配置”理念,通过自动配置、起步依赖、嵌入式容器和Actuator四大特性,简化Spring应用的开发与部署,提升效率,降低门槛,成为现代Java开发的事实标准。
|
2月前
|
缓存 安全 Java
《深入理解Spring》过滤器(Filter)——Web请求的第一道防线
Servlet过滤器是Java Web核心组件,可在请求进入容器时进行预处理与响应后处理,适用于日志、认证、安全、跨域等全局性功能,具有比Spring拦截器更早的执行时机和更广的覆盖范围。
|
3月前
|
缓存 Java 应用服务中间件
Spring Boot配置优化:Tomcat+数据库+缓存+日志,全场景教程
本文详解Spring Boot十大核心配置优化技巧,涵盖Tomcat连接池、数据库连接池、Jackson时区、日志管理、缓存策略、异步线程池等关键配置,结合代码示例与通俗解释,助你轻松掌握高并发场景下的性能调优方法,适用于实际项目落地。
605 5
|
3月前
|
传感器 Java 数据库
探索Spring Boot的@Conditional注解的上下文配置
Spring Boot 的 `@Conditional` 注解可根据不同条件动态控制 Bean 的加载,提升应用的灵活性与可配置性。本文深入解析其用法与优势,并结合实例展示如何通过自定义条件类实现环境适配的智能配置。
200 0
探索Spring Boot的@Conditional注解的上下文配置
|
3月前
|
存储 安全 Java
如何在 Spring Web 应用程序中使用 @SessionScope 和 @RequestScope
Spring框架中的`@SessionScope`和`@RequestScope`注解用于管理Web应用中的状态。`@SessionScope`绑定HTTP会话生命周期,适用于用户特定数据,如购物车;`@RequestScope`限定于单个请求,适合无状态、线程安全的操作,如日志记录。合理选择作用域能提升应用性能与可维护性。
175 1
深入实践springboot实战 蓄势待发 我不是雷锋 我是知识搬运工
springboot,说白了就是一个集合了功能的大类库,包括springMVC,spring,spring data,spring security等等,并且提供了很多和可以和其他常用框架,插件完美整合的接口(只能说是一些常用框架,基本在github上能排上名次的都有完美整合,但如果是自己写的一个框架就无法实现快速整合)。
|
Java 数据安全/隐私保护
Neo4j【付诸实践 01】SpringBoot集成报错org.neo4j.driver.exceptions.ClientException:服务器不支持此驱动程序支持的任何协议版本(解决+源代码)
Neo4j【付诸实践 01】SpringBoot集成报错org.neo4j.driver.exceptions.ClientException:服务器不支持此驱动程序支持的任何协议版本(解决+源代码)
803 1