1. 前言
- 装配了web能力的
Spring Boot项目启动 —— 监听8080端口
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

- 一个最简单的
Spring Boot项目启动 —— 自然退出,“啥也不干”
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

- 本文探究
spring-boot-starter-web依赖做了什么事情,让项目开始监听8080端口,成为了一个web服务。
2. 环境准备
- 开启logback支持,设置日志级别为DEBUG
logging:
config: classpath:logback.xml
<?xml version='1.0' encoding='UTF-8'?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d [%thread] %-5level %logger{50} -[%file:%line]- %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
- 每次运行项目都执行
mvn clean避免class path有旧依赖引入的类
3. 找到 XXXAutoConfiger 的类
- pom文件声明为web项目
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3.1. 怎么找?
spring-boot-starter-web 那么就找到autoconfigure 模块下的web 包。找一个servlet的子包下的任意一个类。现在取WebMvcAutoConfiguration

- 条件装配
@ConditionalOnWebApplication表达当前是一个web应用@ConditionalOnClass表达在classpath中需要有的class才能装配@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })表示需要引入ServletDispatcherServletWebMvcConfigurer到classpath中。

当pom.xml引入以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- 装配
启动时WebMvcAutoConfiguration上的条件注解都满足,所以执行装配。通过引入依赖就让项目拥有监听端口的能力(变成web)项目,这个特性就是自动装配。而这个加入到classpath的操作可以由maven替我们完成,而不用写配置文件。
4. 后记
Under the hood, auto-configuration is implemented with standard @Configuration classes. Additional @Conditional annotations are used to constrain when the auto-configuration should apply. Usually, auto-configuration classes use @ConditionalOnClass and @ConditionalOnMissingBean annotations. This ensures that auto-configuration applies only when relevant classes are found and when you have not declared your own @Configuration.
You can browse the source code of spring-boot-autoconfigure to see the @Configuration classes that Spring provides (see the META-INF/spring.factories file).
Spring Boot checks for the presence of a META-INF/spring.factories file within your published jar. The file should list your configuration classes under the EnableAutoConfiguration key, as shown in the following example:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.mycorp.libx.autoconfigure.LibXAutoConfiguration,\
com.mycorp.libx.autoconfigure.LibXWebAutoConfiguration
借官网的片段,总结下其他要注意的规范:
. Usually, auto-configuration classes use @ConditionalOnClass and @ConditionalOnMissingBean annotations. This ensures that auto-configuration applies only when relevant classes are found and when you have not declared your own @Configuration.
- 在自己的工程中没有额外声明@Configuration,@ConditionalOnXXX 注解能确保在相关的类在classpath被找到才触发自动装配。
You can browse the source code of spring-boot-autoconfigure to see the @Configuration classes that Spring provides (see the META-INF/spring.factories file).
META-INF/spring.factories提供了Spring 内置的自动装配类声明。
Spring Boot checks for the presence of a META-INF/spring.factories file within your published jar. The file should list your configuration classes under the EnableAutoConfiguration key
- 自己要实现有自动装配的配置类,同样需要在
META-INF/spring.factories中声明。并且作为一个published jar被引入。

本文探讨了Spring Boot的自动装配特性,通过分析`spring-boot-starter-web`依赖如何使项目监听8080端口,揭示了自动装配的工作机制。作者设置了DEBUG日志并借助`WebMvcAutoConfiguration`类来研究条件装配,如`@ConditionalOnWebApplication`和`@ConditionalOnClass`注解。当引入`spring-boot-starter-web`,满足条件的配置类会被自动装配,使得项目具备web服务功能。文章还强调了`META-INF/spring.factories`文件在自动装配配置类中的作用,并总结了自动装配类使用`@ConditionalOnClass`和`@ConditionalOnMissingBean`注解的规范。
1022

被折叠的 条评论
为什么被折叠?



