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();

}


相关文章
|
开发框架 监控 安全
Windows Defender 导致 Web IIS 服务异常停止排查
某日凌晨IIS服务异常停止,经查为Windows Defender安全补丁KB2267602触发引擎更新,导致系统资源波动,进而引发应用池回收。确认非人为操作,系统无重启。通过分析日志与监控,定位原因为Defender更新后扫描加重负载。解决方案:将IIS及.NET相关路径添加至Defender排除列表,避免业务影响。
1598 116
|
人工智能 Java Serverless
【MCP教程系列】搭建基于 Spring AI 的 SSE 模式 MCP 服务并自定义部署至阿里云百炼
本文详细介绍了如何基于Spring AI搭建支持SSE模式的MCP服务,并成功集成至阿里云百炼大模型平台。通过四个步骤实现从零到Agent的构建,包括项目创建、工具开发、服务测试与部署。文章还提供了具体代码示例和操作截图,帮助读者快速上手。最终,将自定义SSE MCP服务集成到百炼平台,完成智能体应用的创建与测试。适合希望了解SSE实时交互及大模型集成的开发者参考。
15894 60
|
负载均衡 监控 Java
Spring Cloud Gateway 全解析:路由配置、断言规则与过滤器实战指南
本文详细介绍了 Spring Cloud Gateway 的核心功能与实践配置。首先讲解了网关模块的创建流程,包括依赖引入(gateway、nacos 服务发现、负载均衡)、端口与服务发现配置,以及路由规则的设置(需注意路径前缀重复与优先级 order)。接着深入解析路由断言,涵盖 After、Before、Path 等 12 种内置断言的参数、作用及配置示例,并说明了自定义断言的实现方法。随后重点阐述过滤器机制,区分路由过滤器(如 AddRequestHeader、RewritePath、RequestRateLimiter 等)与全局过滤器的作用范围与配置方式,提
Spring Cloud Gateway 全解析:路由配置、断言规则与过滤器实战指南
|
Java 关系型数据库 MySQL
Spring Boot自动配置:魔法背后的秘密
Spring Boot 自动配置揭秘:只需简单配置即可启动项目,背后依赖“约定大于配置”与条件化装配。核心在于 `@EnableAutoConfiguration` 注解与 `@Conditional` 系列条件判断,通过 `spring.factories` 或 `AutoConfiguration.imports` 加载配置类,实现按需自动装配 Bean。
|
人工智能 Java 开发者
【Spring】原理解析:Spring Boot 自动配置
Spring Boot通过“约定优于配置”的设计理念,自动检测项目依赖并根据这些依赖自动装配相应的Bean,从而解放开发者从繁琐的配置工作中解脱出来,专注于业务逻辑实现。
3254 0
|
Java Spring
Spring Boot配置的优先级?
在Spring Boot项目中,配置可通过配置文件和外部配置实现。支持的配置文件包括application.properties、application.yml和application.yaml,优先级依次降低。外部配置常用方式有Java系统属性(如-Dserver.port=9001)和命令行参数(如--server.port=10010),其中命令行参数优先级高于系统属性。整体优先级顺序为:命令行参数 &gt; Java系统属性 &gt; application.properties &gt; application.yml &gt; application.yaml。
1583 0
|
前端开发 Java 应用服务中间件
《深入理解Spring》 Spring Boot——约定优于配置的革命者
Spring Boot基于“约定优于配置”理念,通过自动配置、起步依赖、嵌入式容器和Actuator四大特性,简化Spring应用的开发与部署,提升效率,降低门槛,成为现代Java开发的事实标准。
|
缓存 安全 Java
《深入理解Spring》过滤器(Filter)——Web请求的第一道防线
Servlet过滤器是Java Web核心组件,可在请求进入容器时进行预处理与响应后处理,适用于日志、认证、安全、跨域等全局性功能,具有比Spring拦截器更早的执行时机和更广的覆盖范围。
|
缓存 Java 应用服务中间件
Spring Boot配置优化:Tomcat+数据库+缓存+日志,全场景教程
本文详解Spring Boot十大核心配置优化技巧,涵盖Tomcat连接池、数据库连接池、Jackson时区、日志管理、缓存策略、异步线程池等关键配置,结合代码示例与通俗解释,助你轻松掌握高并发场景下的性能调优方法,适用于实际项目落地。
2227 5
|
传感器 Java 数据库
探索Spring Boot的@Conditional注解的上下文配置
Spring Boot 的 `@Conditional` 注解可根据不同条件动态控制 Bean 的加载,提升应用的灵活性与可配置性。本文深入解析其用法与优势,并结合实例展示如何通过自定义条件类实现环境适配的智能配置。
886 0
探索Spring Boot的@Conditional注解的上下文配置