Skip to content

Commit

Permalink
fix: 修复某些场景下获取不到 RedisTemplate<String, DictTypeVo> 导致启动失败问题
Browse files Browse the repository at this point in the history
在一些多模块项目中,公共模块配置了 RedisTemplate<String, Object> Bean 先注入到上下文中,系统字典的
RedisTemplate<String, DictTypeVo> 无法继续注入,因此导致启动失败
  • Loading branch information
houkunlin committed Dec 13, 2021
1 parent c6311b6 commit 6ce4163
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
* Redis 配置(在存在 Redis 环境时自动配置相关对象,无论后续是否需要)
Expand All @@ -21,17 +21,22 @@
@Configuration(proxyBeanMethods = false)
@AllArgsConstructor
public class DictRedisConfiguration {
/**
* 系统字典 RedisTemplate Bean 名称
*/
public static final String DICT_REDIS_BEAN_NAME = "DictTypeRedisTemplate";

/**
* 创建一个默认的 DictTypeVo 类型 Redis 客户端
*
* @param connectionFactory RedisConnectionFactory
* @return RedisTemplate<String, DictTypeVo>
*/
@ConditionalOnMissingBean
@Bean
public RedisTemplate<String, DictTypeVo> dictTypeRedisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, DictTypeVo> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
@ConditionalOnMissingBean(name = DICT_REDIS_BEAN_NAME)
@Bean(DICT_REDIS_BEAN_NAME)
public RedisTemplate<String, DictTypeVo> dictTypeRedisTemplate(final RedisConnectionFactory connectionFactory) {
final RedisTemplate<String, DictTypeVo> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(RedisSerializer.string());
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(DictTypeVo.class));
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
Expand All @@ -45,7 +50,7 @@ public RedisTemplate<String, DictTypeVo> dictTypeRedisTemplate(RedisConnectionFa
*/
@ConditionalOnMissingBean
@Bean
public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory) {
public RedisMessageListenerContainer redisMessageListenerContainer(final RedisConnectionFactory connectionFactory) {
final RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
return container;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RedisDictStoreConfiguration {
*/
@Bean
@ConditionalOnMissingBean
public DictStore dictStore(RedisTemplate<String, DictTypeVo> redisTemplate1, StringRedisTemplate redisTemplate2, RemoteDict remoteDict) {
public DictStore dictStore(final RedisTemplate<String, DictTypeVo> redisTemplate1, final StringRedisTemplate redisTemplate2, final RemoteDict remoteDict) {
return new RedisDictStore(redisTemplate1, redisTemplate2, remoteDict);
}
}

0 comments on commit 6ce4163

Please sign in to comment.