【问题标题】:RedisTemplate not fetching data in Spring BootRedisTemplate 未在 Spring Boot 中获取数据
【发布时间】:2020-04-19 09:28:21
【问题描述】:

为 Redis 配置一个单独的配置文件。

package com.xyz.abc.webapp.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@EnableRedisRepositories
public class RedisConfig {


  @Bean
  public JedisConnectionFactory jedisConnectionFactory() {
    return new JedisConnectionFactory();
  }

  /**
   * client for redis operations.
   * @return RedisTemplate
   */
  @Bean()
  public RedisTemplate<String, Object> redisTemplate() {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(jedisConnectionFactory());
    return template;
  }

}

redisTemplate.keys("*") 返回空集。

我不明白是什么问题。

但是当 Bean 在 Component 中声明时可以正常工作。

【问题讨论】:

    标签: java spring spring-boot redis jedis


    【解决方案1】:

    设置后生效

    template.setDefaultSerializer(new StringRedisSerializer());

     @Bean()
      public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());
        template.setDefaultSerializer(new StringRedisSerializer());
        return template;
      }
    

    【讨论】:

    • 非常感谢!经过几个小时的挖掘和拔头发,我终于偶然发现了这个。
    猜你喜欢
    • 1970-01-01
    • 2020-08-17
    • 2015-08-07
    • 1970-01-01
    • 2021-06-10
    • 2017-06-17
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    相关资源
    最近更新 更多