【Spring Boot】spring-boot-starter-web 如何完成自动装配

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. 环境准备

  • 开启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 }) 表示需要引入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.

  1. 在自己的工程中没有额外声明@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).

  1. 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

  1. 自己要实现有自动装配的配置类,同样需要在 META-INF/spring.factories 中声明。并且作为一个 published jar被引入。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,spring-boot-starter-data-redis是Spring Boot中用于自动装配Redis的starter包。它包含了自动装配所需的类和注解等。当我们在项目的pom.xml文件中引入spring-boot-starter-data-redis包时,Spring Boot会自动根据配置文件中的相关配置信息来完成Redis的自动装配。 具体来说,spring-boot-starter-data-redis使用了RedisAutoConfiguration类来实现自动装配。该类通过读取配置文件中的相关配置信息,例如主机名、端口号、密码等,来创建Redis连接工厂和RedisTemplate等实例。这些实例可以在应用程序中直接使用,而无需手动配置和初始化。 下面是一个示例代码,展示了如何使用spring-boot-starter-data-redis进行自动装配: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.redis.core.RedisTemplate; @SpringBootApplication public class RedisApplication { private final RedisTemplate<String, String> redisTemplate; public RedisApplication(RedisTemplate<String, String> redisTemplate) { this.redisTemplate = redisTemplate; } public static void main(String[] args) { SpringApplication.run(RedisApplication.class, args); } // 在需要使用Redis的地方,可以直接注入RedisTemplate实例,并进行操作 // 例如: // redisTemplate.opsForValue().set("key", "value"); // String value = redisTemplate.opsForValue().get("key"); } ``` 通过上述代码,我们可以看到,在Spring Boot应用程序中,我们只需要在需要使用Redis的地方注入RedisTemplate实例,就可以直接使用Redis的相关操作方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值