如何自动连接RedisTemplate

如何自动连接RedisTemplate,第1张

如何自动连接RedisTemplate

stacktrace建议您尚未定义要用于注入的Bean

RedisTemplate
。可以解决该问题,创建一个配置文件,例如

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;@Configurationpublic 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; }}

一旦有了配置文件,则需要将其传递

SpringApplication.run
Eg

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


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5441934.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存