SpringBoot拦截器注入stringredistemplate出现Consider defining a bean of type 'org.springframework.data.redis.core.RedisTemplate' in your configuration.问题
问题是注入StringRedisTemplate后启动后端时报错:Consider defining a bean of type 'org.springframework.data.redis.core.RedisTemplate' in your configuration
问题出在没有配置RedisConfig类,考虑在config包中添加RedisConfig类
public class RedisConfig {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate redisTemplate = new RedisTemplate();
//设置redis连接工厂对象
redisTemplate.setConnectionFactory(redisConnectionFactory);
//创建Json序列化工具
GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
//redis中的key序列化器
redisTemplate.setKeySerializer(RedisSerializer.string());
redisTemplate.setHashKeySerializer(RedisSerializer.string());
//redis中的Object序列化器
redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
return redisTemplate;
}
}
再次运行发现还是报该错误,继续往下看了看
在springboot启动类上添加注解 @ComponentScan(basePackages = { “XXX” }),引号里面代表扫描方法,启动后还是报错
发现项目的父类pom引入的redis是
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-data-redis</artifactId> </dependency>
将子类引入的pom改为了
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
再次启动就成功了,希望可以帮助大家。