admin管理员组文章数量:1794759
SpringBoot启动微服务报错Consider defining a bean of type ‘org.springframework.data.redis.core.RedisTemplate
最近遇到一个问题,项目之前使用了Redis,可正常启动,然后最近对Redis部分进行了一些微调,再启动服务提示如下错误:
Consider defining a bean of type 'org.springframework.data.redis.core.RedisTemplate' in your configuration.
出现该错误的原因可能有很多,记录下几种解决办法。
1、SpringBoot版本问题:
pom.xml中如果有配置
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> springboot的版本可能有问题,修改Springboot版本为如下版本即可
<version>1.5.17.RELEASE</version>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.9.RELEASE</version> </parent> 将2.0.9.RELEASE改为1.5.17.RELEASE2、检查Redis配置是否正常,如果没有配置Redis,可在项目下新建一个config的包,在该包下新建RedisConfig类,类的内容如下
@Configuration public class RedisConfig { @Bean public RedisTemplate<String, ?> getRedisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, ?> template = new RedisTemplate<>(); template.setKeySerializer(new StringRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); template.setEnableTransactionSupport(true); template.setConnectionFactory(factory); return template; } }3、SpringBoot启动扫码包的配置问题,springBoot启动时候,会自动扫描Application所在包路径下的所有bean,检查你所注入RedisTemplate的这个类所在目录,是否再启动类的目录结构之下
4、注意RedisTemplate的注入注解,看看是Autowired还是Resource,如果是Autowired,可以改为Resource
@Resource RedisTemplate<String, Set<String>> redisTemplateUA;这里需要注意,如果RedisTemplate注入的变量名为redisTemplate,此时即使没有进行RedisConfig的配置,依然可以正常启动项目,原因为SpringBoot自动帮我们在容器中生成了一个RedisTemplate和一个StringRedisTemplate。但是,这个RedisTemplate的泛型是<Object,Object>,写代码不方便,需要写好多类型转换的代码;我们需要一个泛型为<String,Object>形式的RedisTemplate。并且,这个RedisTemplate没有设置数据存在Redis时,key及value的序列化方式:
@Configuration @ConditionalOnClass(RedisOperations.class) @EnableConfigurationProperties(RedisProperties.class) @Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class }) public class RedisAutoConfiguration { @Bean @ConditionalOnMissingBean(name = "redisTemplate") public RedisTemplate<Object, Object> redisTemplate( RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { RedisTemplate<Object, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); return template; } @Bean @ConditionalOnMissingBean public StringRedisTemplate stringRedisTemplate( RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; } }本文标签: 报错beantypeSpringBootdefining
版权声明:本文标题:SpringBoot启动微服务报错Consider defining a bean of type ‘org.springframework.data.redis.core.RedisTemplate 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686555007a81331.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论