写点什么

springBoot 中 redis 的自动装配

用户头像
Rubble
关注
发布于: 3 小时前
springBoot中redis的自动装配

引入 redis 依赖


<dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-data-redis</artifactId></dependency>
复制代码

spring-boot-starter-data-redis

查看 spring-boot-starter-data-redis jar 包并无特殊内容,再查看依赖关系,引入了spring-data-redislettuce-core, springboot2.x 使用 lettuce 进行链接 redis。


<dependencies>    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter</artifactId>      <version>2.2.5.RELEASE</version>      <scope>compile</scope>    </dependency>    <dependency>      <groupId>org.springframework.data</groupId>      <artifactId>spring-data-redis</artifactId>      <version>2.2.5.RELEASE</version>      <scope>compile</scope>      <exclusions>        <exclusion>          <artifactId>jcl-over-slf4j</artifactId>          <groupId>org.slf4j</groupId>        </exclusion>      </exclusions>    </dependency>    <dependency>      <groupId>io.lettuce</groupId>      <artifactId>lettuce-core</artifactId>      <version>5.2.2.RELEASE</version>      <scope>compile</scope>    </dependency>  </dependencies>
复制代码


查看 spring-data-redis 下的 spring.factories,仅有一个 RepositoryFactorySupport 并无自动装配。


org.springframework.data.repository.core.support.RepositoryFactorySupport=org.springframework.data.redis.repository.support.RedisRepositoryFactory
复制代码

spring-boot-autoconfigure

redis 自动装配由 spring-boot-autoconfigure 实现。


META-INF 下 spring.factories 中自动配置关于 redis 的配置有


# Auto Configureorg.springframework.boot.autoconfigure.EnableAutoConfiguration=\org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration,\org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,\
复制代码


META-INF 下 spring-autoconfigure-metadata.properties 中关于 redis 的元数据配置


org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration.ConditionalOnClass=org.springframework.data.redis.core.RedisOperationsorg.springframework.boot.autoconfigure.session.RedisReactiveSessionConfiguration.ConditionalOnBean=org.springframework.data.redis.connection.ReactiveRedisConnectionFactoryorg.springframework.boot.autoconfigure.session.RedisSessionConfiguration=org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration.ConditionalOnBean=org.springframework.data.redis.connection.RedisConnectionFactoryorg.springframework.boot.autoconfigure.cache.RedisCacheConfiguration=org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration.AutoConfigureAfter=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfigurationorg.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration.ConditionalOnClass=org.springframework.data.redis.repository.configuration.EnableRedisRepositoriesorg.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration=org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration.AutoConfigureAfter=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfigurationorg.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration=org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration.AutoConfigureAfter=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfigurationorg.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration.ConditionalOnBean=org.springframework.data.redis.connection.RedisConnectionFactoryorg.springframework.boot.autoconfigure.session.RedisSessionConfiguration.ConditionalOnBean=org.springframework.data.redis.connection.RedisConnectionFactoryorg.springframework.boot.autoconfigure.cache.RedisCacheConfiguration.ConditionalOnClass=org.springframework.data.redis.connection.RedisConnectionFactoryorg.springframework.boot.autoconfigure.session.RedisSessionConfiguration.ConditionalOnClass=org.springframework.data.redis.core.RedisTemplate,org.springframework.session.data.redis.RedisIndexedSessionRepositoryorg.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration.ConditionalOnClass=reactor.core.publisher.Flux,org.springframework.data.redis.connection.ReactiveRedisConnectionFactory,org.springframework.data.redis.core.ReactiveRedisTemplateorg.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration=org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration.ConditionalOnClass=io.lettuce.core.RedisClientorg.springframework.boot.autoconfigure.session.RedisReactiveSessionConfiguration.ConditionalOnClass=org.springframework.data.redis.connection.ReactiveRedisConnectionFactory,org.springframework.session.data.redis.ReactiveRedisSessionRepositoryorg.springframework.boot.autoconfigure.session.RedisReactiveSessionConfiguration=org.springframework.boot.autoconfigure.data.redis.JedisConnectionConfiguration.ConditionalOnClass=org.apache.commons.pool2.impl.GenericObjectPool,redis.clients.jedis.Jedis,org.springframework.data.redis.connection.jedis.JedisConnectionorg.springframework.boot.autoconfigure.data.redis.JedisConnectionConfiguration=org.springframework.boot.autoconfigure.session.SessionAutoConfiguration.AutoConfigureAfter=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfigurationorg.springframework.boot.autoconfigure.cache.CacheAutoConfiguration.AutoConfigureAfter=org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
复制代码


RedisAutoConfiguration 开启条件 @ConditionalOnClass


metadata 中也指定了配置 ConditionalOnClass 开启条件指定的类 RedisOperations


org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration.ConditionalOnClass=org.springframework.data.redis.core.RedisOperations
复制代码

RedisAutoConfiguration 类

配置类加载了配置属性类 RedisProperties,导入了配置 LettuceConnectionConfiguration、JedisConnectionConfiguration


通过使用 @ConditionalOnMissingBean 配置了默认的 bean redisTemplate、StringRedisTemplate


@Configuration(proxyBeanMethods = false)@ConditionalOnClass(RedisOperations.class)@EnableConfigurationProperties(RedisProperties.class)@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })public class RedisAutoConfiguration {
@Bean @ConditionalOnMissingBean(name = "redisTemplate") public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { RedisTemplate<Object, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); return template; }
@Bean @ConditionalOnMissingBean public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; }
}
复制代码

RedisReactiveAutoConfiguration

@AutoConfigureAfter(RedisAutoConfiguration.class) 在 RedisAutoConfiguration 自动配置完成后才会加载


@Configuration(proxyBeanMethods = false)@ConditionalOnClass({ ReactiveRedisConnectionFactory.class, ReactiveRedisTemplate.class, Flux.class })@AutoConfigureAfter(RedisAutoConfiguration.class)public class RedisReactiveAutoConfiguration {  @Bean  @ConditionalOnMissingBean(name = "reactiveRedisTemplate")  @ConditionalOnBean(ReactiveRedisConnectionFactory.class)  public ReactiveRedisTemplate<Object, Object> reactiveRedisTemplate(      ReactiveRedisConnectionFactory reactiveRedisConnectionFactory, ResourceLoader resourceLoader) {    JdkSerializationRedisSerializer jdkSerializer = new JdkSerializationRedisSerializer(        resourceLoader.getClassLoader());    RedisSerializationContext<Object, Object> serializationContext = RedisSerializationContext        .newSerializationContext().key(jdkSerializer).value(jdkSerializer).hashKey(jdkSerializer)        .hashValue(jdkSerializer).build();    return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, serializationContext);  }
@Bean @ConditionalOnMissingBean(name = "reactiveStringRedisTemplate") @ConditionalOnBean(ReactiveRedisConnectionFactory.class) public ReactiveStringRedisTemplate reactiveStringRedisTemplate( ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) { return new ReactiveStringRedisTemplate(reactiveRedisConnectionFactory); }}
复制代码

RedisRepositoriesAutoConfiguration

@AutoConfigureAfter(RedisAutoConfiguration.class) 同样在 RedisAutoConfiguration 自动配置完成后才会加载


@Configuration(proxyBeanMethods = false)@ConditionalOnClass(EnableRedisRepositories.class)@ConditionalOnBean(RedisConnectionFactory.class)@ConditionalOnProperty(prefix = "spring.data.redis.repositories", name = "enabled", havingValue = "true",  matchIfMissing = true)@ConditionalOnMissingBean(RedisRepositoryFactoryBean.class)@Import(RedisRepositoriesRegistrar.class)@AutoConfigureAfter(RedisAutoConfiguration.class)public class RedisRepositoriesAutoConfiguration {
}
复制代码

总结

redis 配置由 springBoot 集成,在 spring-boot-autoconfigure 中配置,spring.factories 中 EnableAutoConfiguration 指定自动配置类 RedisAutoConfiguration 进行自动配置,spring-configuration-metadata.properties 指定了配置元数据 RedisAutoConfiguration ConditionalOnClass 配置条件 RedisOperations,当引入 spring-data-redis 时存在类路径 RedisOperations 进行 redis 的自动加载。redis 自动配置使用 Lettuce 作为 client 进行连接,配置了默认的 bean redisTemplate、StringRedisTemplate。

用户头像

Rubble

关注

还未添加个人签名 2021.06.01 加入

还未添加个人简介

评论

发布
暂无评论
springBoot中redis的自动装配