admin管理员组文章数量:1130349
使用 RedisTemplate<String, Object> 报错:“Unsatisfied dependency expressed through field 'redisTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisTemplate' defined in class path resource [com/cx/sasmc/redisobj/RedisBeanConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: RedisConnectionFactory is required
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:659) ~[spring-beans-5.3.23.jar:5.3.23]”
我的解决办法 1.Maven:
<!-- Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--连接池-->
<dependency>
<groupId>org.apachemons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
2.新建一个配置类:内容可以到网上找,有好多。
package com.cx.sasmc.redisobj;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
/**
* <p>
* 配置Redis
* </p>
*
* @author Lch
* @dateTime 2024/2/1 16:47
*/
@Configuration
public class RedisBeanConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
System.out.println("开始redis序列化.....");
//创建RedisTempLate对象
RedisTemplate<String, Object> template = new RedisTemplate<>();
//设置连接工厂
template.setConnectionFactory(connectionFactory);
//创建JSON序列化工具
GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
//设置Key的序列化(UTF_8格式)
template.setKeySerializer(RedisSerializer.string());
template.setHashKeySerializer(RedisSerializer.string());
//设置value的序列化
template.setValueSerializer(jsonRedisSerializer);
template.setHashValueSerializer(jsonRedisSerializer);
// 开启事务
template.setEnableTransactionSupport(true);
//返回
return template;
}
}
3. 不要使用 @Autowired 使用 @Resource
只要使用了 RedisTemplate<String, Object> 一定要使用 @Resource这个注解。
希望能帮助到你。
2024-2-4 18:22 写完了 下班回家。
------------------------------------------------------------------------------------------------------------------
昨晚着急下班,今天补充一下测试。
最后要感谢QuickRedis 下载地址:QuickRedis 发行版 - Gitee
使用 RedisTemplate<String, Object> 报错:“Unsatisfied dependency expressed through field 'redisTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisTemplate' defined in class path resource [com/cx/sasmc/redisobj/RedisBeanConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: RedisConnectionFactory is required
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:659) ~[spring-beans-5.3.23.jar:5.3.23]”
我的解决办法 1.Maven:
<!-- Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--连接池-->
<dependency>
<groupId>org.apachemons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
2.新建一个配置类:内容可以到网上找,有好多。
package com.cx.sasmc.redisobj;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
/**
* <p>
* 配置Redis
* </p>
*
* @author Lch
* @dateTime 2024/2/1 16:47
*/
@Configuration
public class RedisBeanConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
System.out.println("开始redis序列化.....");
//创建RedisTempLate对象
RedisTemplate<String, Object> template = new RedisTemplate<>();
//设置连接工厂
template.setConnectionFactory(connectionFactory);
//创建JSON序列化工具
GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
//设置Key的序列化(UTF_8格式)
template.setKeySerializer(RedisSerializer.string());
template.setHashKeySerializer(RedisSerializer.string());
//设置value的序列化
template.setValueSerializer(jsonRedisSerializer);
template.setHashValueSerializer(jsonRedisSerializer);
// 开启事务
template.setEnableTransactionSupport(true);
//返回
return template;
}
}
3. 不要使用 @Autowired 使用 @Resource
只要使用了 RedisTemplate<String, Object> 一定要使用 @Resource这个注解。
希望能帮助到你。
2024-2-4 18:22 写完了 下班回家。
------------------------------------------------------------------------------------------------------------------
昨晚着急下班,今天补充一下测试。
最后要感谢QuickRedis 下载地址:QuickRedis 发行版 - Gitee
本文标签: 报错objectRedisTemplateStringUnsatisfied
版权声明:本文标题:关于 RedisTemplate<String, Object> 报错Unsatisfied dependency expressed through field ‘redisTempla 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/jiaocheng/1755026978a2755138.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论