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

发布时间:2024年01月24日

springboot2.7.8? ?redis3.2.100

在springboot中

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

只需要在pom.xml中加入

<!-- 整合redis -->
<dependency>
? ?<groupId>org.springframework.boot</groupId>
? ?<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
? <groupId>org.springframework.boot</groupId>
? <artifactId>spring-boot-starter-data-redis</artifactId>

<version>2.2.5.RELEASE</version>

</dependency>
<dependency>
? <groupId>redis.clients</groupId>
? <artifactId>jedis</artifactId>
? <version>3.8.0</version>
</dependency>

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<String, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(connectionFactory);

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

    Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer<Object>(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;
}

}

文章来源:https://blog.csdn.net/bysjlwdx/article/details/135822144
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。