sql >> Base de Datos >  >> NoSQL >> Redis

Cómo se conectó automáticamente RedisTemplate

Stacktrace sugiere que no ha definido el Bean que le gustaría usar para inyectar en RedisTemplate .Puedes resolverlo creando un archivo de configuración, Ej.

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.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

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

 @Bean
 RedisTemplate< String, Long > redisTemplate() {
  final RedisTemplate< String, Long > template =  new RedisTemplate< String, Long >();
  template.setConnectionFactory( jedisConnectionFactory() );
  template.setKeySerializer( new StringRedisSerializer() );
  template.setHashValueSerializer( new GenericToStringSerializer< Long >( Long.class ) );
  template.setValueSerializer( new GenericToStringSerializer< Long >( Long.class ) );
  return template;
 }
}

Una vez que tenga el archivo de configuración, debe pasarlo a SpringApplication.run Por ejemplo

Object[] sources = {AppConfig.class};
ApplicationContext ctx = SpringApplication.run(sources, args);