RedisTemplate為Null報java.lang.NullPointerException
問題描述
pom.xml:
<!-- 添加redis支持 --> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-redis</artifactId> </dependency> <dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.6.2</version> </dependency> <dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>1.5.0.RELEASE</version> </dependency>
RedisCacheConfig.java:
@Bean(name="jedisConnectionFactory") JedisConnectionFactory jedisConnectionFactory() {JedisConnectionFactory factory = new JedisConnectionFactory();return factory; } @Bean RedisTemplate<String, Object> redisTemplate() {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();redisTemplate.setConnectionFactory(jedisConnectionFactory());return redisTemplate; }
工具類:
@SuppressWarnings("unchecked")@Componentpublic class RedisUtil { @SuppressWarnings("rawtypes")@Autowired private RedisTemplate redisTemplate;public void test() { redisTemplate.opsForValue().set("q", 123);//這里的redisTemplate=null 不知為何? }}
調用test()方法 報java.lang.NullPointerException 求指點明燈
問題解答
回答1:你用的是springboot集成,單元測試卻是junit的,沒有納入springboot的環境,當然是有問題的了。
相關文章:
1. javascript - sublime快鍵鍵問題2. javascript - immutable配合react提升性能?3. vue.js - Vue 如何像Angular.js watch 一樣監聽數據變化4. 實現bing搜索工具urlAPI提交5. javascript - 移動端上不能實現拖拽布局嗎?6. 如何解決Centos下Docker服務啟動無響應,且輸入docker命令無響應?7. javascript - nodejs關于進程間發送句柄的一點疑問8. thinkPHP5中獲取數據庫數據后默認選中下拉框的值,傳遞到后臺消失不見。有圖有代碼,希望有人幫忙9. java - 使用fastjson對HashMap進行序列化錯誤10. index.php錯誤,求指點

網公網安備