使用@Autowired 注入RedisTemplate報錯的問題及解決
@Autowired 注入RedisTemplate報錯
先看報錯信息
Field redisTemplate in xxx.xxx required a bean of type 'org.springframework.data.redis.core. RedisTemplate' that could not be found.
The injection point has the following annotations:
? @org.springframework.beans.factory.annotation.Autowired(required=true)
下面是Redis配置類
@Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) { // 設(shè)置序列化 Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); // 配置redisTemplate RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(lettuceConnectionFactory); RedisSerializer<?> stringSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringSerializer);// key序列化 redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);// value序列化 redisTemplate.setHashKeySerializer(stringSerializer);// Hash key序列化 redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);// Hash value序列化 redisTemplate.afterPropertiesSet(); return redisTemplate; } }
下面是注入方式
@Autowired private RedisTemplate<String, Object> redisTemplate;
解決方法一
將@Autowired改為 @Resource
@Resource private RedisTemplate<String, Object> redisTemplate;
解決方法二
去掉泛型
@Autowired private RedisTemplate redisTemplate;
下面咱們來看一下原因
首先我們需要了解@Autowired和@Resource的區(qū)別
他們的共同點
@Resource和@Autowired都是做bean的注入時使用的。
不同點
Autowired
默認(rèn)是按照類型裝配注入的,默認(rèn)情況下它要求依賴對象必須存在Resource
默認(rèn)是按照名稱來裝配注入的,只有當(dāng)找不到與名稱匹配的bean才會按照類型來裝配注入
由于Autowired是根據(jù)類型來裝配注入的,所以泛型也被考慮進去了,所以它會去spring容器中找 RedisTemplate<String, Object> 這個bean ;
String[] beanDefinitionNames = run.getBeanDefinitionNames(); System.out.println(Arrays.toString(beanDefinitionNames));
通過代碼獲取到spring容器中所有的bean ,控制臺打印redisTemplate 所以無法找到對應(yīng)的bean
@Autowired 注入RedisTemplate為null
最近使用Springboot引入redis,直接在Service里使用@Autowired 注入RedisTemplate,但是啟動時發(fā)現(xiàn)注入的@RedisTemplate為null
@Service public class CacheRedisImpl implements Cache { @Autowired(required = false) private RedisTemplate<String,Object> redisTemplate; .... }
上述代碼運行啟動之后斷點,如下圖,無論如何都是null
最后去查閱spring的源碼 RedisAutoConfiguration(在org.springframework.boot.autoconfigure.data.redis包)
@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; } ...
終于發(fā)現(xiàn)問題了,spring注入的是RedisTemplate<Object, Object>
而我注入的泛型是RedisTemplate<String,Object>。
解決
通過上述排查分析,解決方案有如下
1.@Autowired 時使用RedisTemplate<Object, Object> 或RedisTemplate
@Service public class CacheRedisImpl implements Cache { @Autowired(required = false) private RedisTemplate redisTemplate;
運行效果如下圖:
2.如一定要使用RedisTemplate<String,Object>,寫一個Configuration自行手動注入@Bean
@Configuration public class RedisAutoConfig { @Bean(name = "redisTemplate4String") public RedisTemplate<String,Object> redisTemplate4String(RedisConnectionFactory redisConnectionFactory){ RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); return redisTemplate; } }
效果如下:
PS: 其實細(xì)心的童鞋可能會發(fā)現(xiàn)我的代碼里@Autowired配置是(required = false)。如果去掉的話,springboot 啟動里會直接報錯找不到bean。解決方案與本文類似。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- springboot使用redisTemplate操作lua腳本
- SpringBoot整合Redis使用@Cacheable和RedisTemplate
- 使用redisTemplate從redis獲取所有數(shù)據(jù)
- Java使用RedisTemplate如何根據(jù)前綴獲取key列表
- 關(guān)于RedisTemplate之opsForValue的使用說明
- SpringBoot使用RedisTemplate.delete刪除指定key失敗的解決辦法
- Redis使用RedisTemplate模板類的常用操作方式
- RedisTemplate中opsForValue和opsForList方法的使用詳解
- Redis Template使用詳解示例教程
相關(guān)文章
ThreadPoolExecutor核心線程數(shù)和RocketMQ消費線程調(diào)整詳解
這篇文章主要介紹了ThreadPoolExecutor核心線程數(shù)和RocketMQ消費線程調(diào)整詳解,Rocketmq 消費者在高峰期希望手動減少消費線程數(shù),通過DefaultMQPushConsumer.updateCorePoolSize方法可以調(diào)用內(nèi)部的setCorePoolSize設(shè)置多線程核心線程數(shù),需要的朋友可以參考下2023-10-10SpringCloud+nacos部署在多ip環(huán)境下統(tǒng)一nacos服務(wù)注冊ip(親測有效)
在部署SpringCoud項目的時候分服務(wù)器部署注冊同一個nacos服務(wù),但是在服務(wù)器有多個ip存在的同時(內(nèi)外網(wǎng)),就會出現(xiàn)注冊服務(wù)ip不同的問題,導(dǎo)致一些接口無法連接訪問,經(jīng)過多次排查終于找到問題并找到解決方法,需要的朋友可以參考下2023-04-04

解決Spring?Boot應(yīng)用打包后文件訪問問題