面试题答案
一键面试Spring Boot自定义Starter原理
Spring Boot Starter的核心原理基于自动配置(Auto-configuration)机制。Spring Boot会在项目启动时扫描特定路径下的配置文件(META-INF/spring.factories
),该文件中定义了自动配置类。当项目引入特定的Starter依赖时,对应的自动配置类会被加载,根据项目的配置情况和类路径下的依赖,自动配置相关的Bean到Spring容器中,从而实现开箱即用的功能。
开发用于简化Redis操作的自定义Starter步骤
- 创建项目结构
- 创建一个Maven项目,在
src/main/java
下创建自定义Starter的包结构,例如com.example.redisstarter
。 - 在
src/main/resources
下创建META-INF/spring.factories
文件。
- 创建一个Maven项目,在
- 依赖管理
- 在
pom.xml
文件中添加必要的依赖:
- 在
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.7.2</version>
</dependency>
</dependencies>
- `spring-boot-autoconfigure`是自动配置的基础依赖,`spring-boot-starter-data-redis`为Redis操作提供了必要的支持。
3. 编写自动配置类
- 创建一个自动配置类,例如RedisAutoConfiguration.java
:
package com.example.redisstarter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
@ConditionalOnClass(RedisTemplate.class)
@EnableConfigurationProperties(RedisProperties.class)
public class RedisAutoConfiguration {
@Autowired
private RedisProperties redisProperties;
@Bean
@ConditionalOnProperty(name = "redis.custom.enabled", havingValue = "true")
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisProperties.getConnectionFactory());
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
return template;
}
}
- `@ConditionalOnClass(RedisTemplate.class)`表示只有当`RedisTemplate`类在类路径中时,该自动配置类才生效。
- `@EnableConfigurationProperties(RedisProperties.class)`用于启用自定义属性配置类。
- `@ConditionalOnProperty(name = "redis.custom.enabled", havingValue = "true")`表示只有当配置文件中`redis.custom.enabled`属性为`true`时,`redisTemplate`这个Bean才会被创建。
4. 编写属性配置类
- 创建RedisProperties.java
:
package com.example.redisstarter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "redis.custom")
public class RedisProperties {
private boolean enabled;
// 可根据需要添加更多自定义属性,如连接池配置等
// 这里省略示例
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
- `@ConfigurationProperties(prefix = "redis.custom")`指定了属性的前缀为`redis.custom`,可通过`application.properties`或`application.yml`文件进行配置。
5. 配置自动配置入口
- 在META-INF/spring.factories
文件中添加以下内容:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.redisstarter.RedisAutoConfiguration
- 这行配置告诉Spring Boot在启动时加载`RedisAutoConfiguration`自动配置类。
6. 打包和使用
- 将自定义的Starter项目打包成JAR文件。
- 在其他Spring Boot项目中引入该JAR包作为依赖,在application.properties
或application.yml
文件中配置相关属性,例如:
redis.custom.enabled=true
- 这样就可以在项目中使用自定义Starter简化Redis操作,例如通过注入`RedisTemplate`来进行Redis数据的存取。