찾다

 >  Q&A  >  본문

spring - 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")
@Component
public class RedisUtil {
    @SuppressWarnings("rawtypes")
    
    @Autowired
    private RedisTemplate redisTemplate;
    
    public void test() {
         redisTemplate.opsForValue().set("q", 123);//这里的redisTemplate=null 不知为何?                          
    }
}

调用test()方法 报java.lang.NullPointerException 求指点明灯

黄舟黄舟2776일 전2087

모든 응답(1)나는 대답할 것이다

  • PHP中文网

    PHP中文网2017-04-26 09:03:40

    springboot 통합을 사용하고 있지만, 단위 테스트는 junit입니다. 물론 문제가 있습니다.

    회신하다
    0
  • 취소회신하다