关于 RedisTemplate<String, Object> 报错Unsatisfied dependency expressed through field ‘redisTempla

使用 RedisTemplate<String, Object> 报错&#xff1a;“Unsatisfied dependency expressed through field redisTemplate; nest

使用 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

发布者:admin,转转请注明出处:http://www.yc00.com/web/1755026977a5228376.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信