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. ionic 项目 ionic build android -release 打包时报错2. python3.x - python3.5.2安装时make报错求助3. javascript - 在使用 vue.js element ui的时候 怎么样保留table翻页后check的值?4. javascript - vue组件通过eventBus通信时,报错a.$on is not a function5. mysql_replication - mysql读写分离时如果单台写库也无法满足性能怎么解决6. mac里的docker如何命令行开启呢?7. angular.js - 如何控制ngrepeat输出的个数8. html - vue项目中用到了elementUI问题9. python - 如何用pandas处理分钟数据变成小时线?10. html5 - weex H5端的使用,怎么跑起来?

网公网安备