欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot整合redis编译出错 redis 

程序员文章站 2022-07-09 20:01:02
...
错误提示:The type org.springframework.data.redis.core.RedisAccessor cannot be resolved. It is indirectly referenced from
required .class files



网上搜了很多文章,大多都说是在pom文件配置的jar有问题,有说spring-boot-starter-data-redis需要加入版本号的,有说需要加入Jedis和commonsPool2的,还有说改成spring-data-redis,但是这些都无法解决编译出错.
其实这个问题很简单,是因为springBoot版本有问题,我试过2.1.X的其他版本,唯独2.1.3这个版本会出现这个问题.


<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
  </parent>















package com.chenli.config;

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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* redis配置类
* @author ZENG.XIAO.YAN
* @date   2018年6月6日
*
*/
@Configuration
public class RedisConfig {

    @Bean
//    @SuppressWarnings("all")
    public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate template = new RedisTemplate();
       template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        // key采用String的序列化方式
       template.setKeySerializer(stringRedisSerializer);

        // hash的key也采用String的序列化方式
       template.setHashKeySerializer(stringRedisSerializer);

        // value序列化方式采用jackson
       template.setValueSerializer(jackson2JsonRedisSerializer);

        // hash的value序列化方式采用jackson
       template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }


}

相关标签: redis