Could not autowire. No beans of ‘RedisConnectionFactory‘ type found.已解决

springboot2.7.8   redis3.2.100

在springboot中

使用RedisConnectionFactory
出现这样的错误Could not autowire. No beans of ‘RedisConnectionFactory‘ type found.

只需要在pom.xml中加入



   org.springframework.boot
   spring-boot-starter-cache


  org.springframework.boot
  spring-boot-starter-data-redis

2.2.5.RELEASE



  redis.clients
  jedis
  3.8.0

MyRedisConfig.java
package com.ums.xadmin.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
@Configuration
public class MyRedisConfig {
@Resource
private RedisConnectionFactory connectionFactory;
@Bean
public JedisConnectionFactory redisConnectionFactory() {
    return new JedisConnectionFactory();
}
@Bean
public RedisTemplate redisTemplate(){
    RedisTemplate redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(connectionFactory);

    // 设置键值序列化
    redisTemplate.setKeySerializer(new StringRedisSerializer());

    Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
    redisTemplate.setValueSerializer(serializer);

    // 序列化,死代码
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    om.setTimeZone(TimeZone.getDefault());
    om.configure(MapperFeature.USE_ANNOTATIONS, false);
    om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance ,ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
    om.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    serializer.setObjectMapper(om);

    return redisTemplate;
} 
  

}

你可能感兴趣的:(spring,boot,spring,mybatis)