您的位置:首页技术文章
文章详情页

RedisTemplate为Null报java.lang.NullPointerException

浏览:84日期:2023-08-25 10:42:31

问题描述

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的环境,当然是有问题的了。

标签: java
相关文章: