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 - 百度坐标,返回目标附近有哪些坐标2. html5 - 只用CSS如何实现input框的宽度随框里输入的内容长短自动适应?3. javascript - 关于jsp中嵌入js代码的问题4. PHP类中的$this5. html - 急求,能否用flex或者高端点的CSS来布局这个图呢?不想用浮动或定位了6. java - 微信开发遇到一个 bug。。access_token 有关的7. Python操作sqlserver是异步的吗?8. python 正则表达式提取9. python - 这句是什么错误?10. angular.js - angularjs 百度统计怎么统计

网公网安备