java - 创建名为“redisConnectionFactory”的 bean 时出错
问题描述
我们正在使用 RedisTemplate 进行 redis 调用。
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setEnableTransactionSupport(true);
// Replace the default serialization with Jackson2JsonRedisSerialize
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
// Set the value of the serialization rules and key serialization rules
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
redisTemplate.setEnableDefaultSerializer(true);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
我们每分钟进行大约1K 次调用,例如redisTemplate.opsForHash().get(HASH_KEY, key);
我们得到以下错误:
stack_trace: org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'redisConnectionFactory': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:212)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:611)
at org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors(BeanFactoryUtils.java:340)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.detectPersistenceExceptionTranslators(PersistenceExceptionTranslationInterceptor.java:168)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:150)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691)
我们正在使用 SpringBoot + Redis 2.8.24 + JDK11。
任何输入?
解决方案
以下方法可能对您有用。
@Configuration
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private Integer port;
@Value("${spring.redis.database}")
private Integer database;
@Bean
JedisConnectionFactory jedisConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(host, port);
redisStandaloneConfiguration.setDatabase(database);
return new JedisConnectionFactory(redisStandaloneConfiguration);
}
@Bean
public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory jedisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
template.setHashKeySerializer(new GenericToStringSerializer<>(String.class));
template.setHashValueSerializer(new GenericToStringSerializer<>(String.class));
return template;
}
}
您可以根据需要设置序列化,并可以基于此访问数据。
public class RedisHashRepository {
final Logger logger = LoggerFactory.getLogger(RedisHashRepository.class);
private HashOperations hashOperations;
@Autowired
public RedisHashRepository(RedisTemplate redisTemplate) {
this.hashOperations = redisTemplate.opsForHash();
}
public String get(String mainKey,String key) {
return (String) hashOperations.get(mainKey, key);
}
}
推荐阅读
- java - 任务“:app:preDebugAndriodTestBuild”执行失败
- asp.net - Odata 提要未在 PowerBI 或 Excel 中连接
- python - 散点图网格由 matplotlib 或 seaborn 中的列分面
- php - 致命错误:找不到类“Elementor\Widget_Base”
- plot - 为 xrange 和 yrange 添加一个额外的空间
- flutter - 为什么我的 Flutter 应用中的 FAB 很大?
- node.js - 了解部署 url 和基本 href
- sql - JPA - 返回更新的列
- iis - IIS 请求内容过滤是否在过滤前加载完整请求
- c# - C# 创建整数和数组字典