1. 前言
参考官网:理解能够自动装配的bean
- 装配了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. 环境准备
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 的类
<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 }) 表示需要引入Servlet DispatcherServlet WebMvcConfigurer 到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 被引入。
|