SpringBoot启动微服务报错Consider defining a bean of type ‘org.springframework.data.redis.core.RedisTemplate

编程知识 更新时间:2023-05-02 05:26:08

最近遇到一个问题,项目之前使用了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.RELEASE

2、检查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;
    }
}

更多推荐

SpringBoot启动微服务报错Consider defining a bean of type ‘org.springframework.data.redi

本文发布于:2023-04-26 06:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/9052fd249c4b7269ad1ad5c6805904f1.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:报错   bean   type   SpringBoot   defining

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 104412文章数
  • 26214阅读数
  • 0评论数