简体   繁体   English

RedisTemplate 未在 Spring Boot 中获取数据

[英]RedisTemplate not fetching data in Spring Boot

Having a Separate Config file for Redis Config.有一个单独的 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("*") returning empty set. redisTemplate.keys("*")返回空集。

I don't get what is the problem.我不明白有什么问题。

But works fine when Bean are decalared inside Component.但是当 Bean 在 Component 中被 decalared 时工作正常。

Worked after setting设置后工作

template.setDefaultSerializer(new StringRedisSerializer()); 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;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM