1. 错误描述:

在springboot项目中采用redis进行数据缓存时,RedisTemplate调用方法时报空指针异常错误:
c.t.d.c.utils.exception.ExceptionUtil : java.lang.NullPointerException

2. 原因分析:

RedisTemplate的注入得分情况:Spring注入和非Spring注入。

  • spring注入:就是指在类上边有@service,@controller等注解,这样类就交给spring管理了,在这些类中进行RedisTemplate注入调用时,没有问题;
  • 非Spring注入:在上述之外的情况,进行RedisTemplate注入调用时,就报错空指针异常。
3. 解决方案:

写一个redis工具类RedisUtil,封装RedisTemplate使用时调用的各种方法。在工具类上加上@component注解,这样在没有被spring管理的类中,采用 RedisUtils 工具类,调用操作redis的get和set方法,而不是直接采用非spring注入方式调用,就成功避免了该问题。

代码如下:

package com.vh.rocket.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Author: jxm
 * @Description:
 * @Date: 2021/5/17 23:11
 * @Version: 1.0
 */
@Component
public class RedisUtils {

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static RedisUtils redisUtils;

    /**
     * 初始化
     */
    @PostConstruct
    public void init() {
        redisUtils = this;
        redisUtils.redisTemplate = this.redisTemplate;
    }

    /**
     * 查询key,支持模糊查询
     *
     * @param key
     */
    public static Set<String> keys(String key) {
        return redisUtils.redisTemplate.keys(key);
    }

    /**
     * 获取值
     *
     * @param key
     */
    public static Object get(String key) {
        return redisUtils.redisTemplate.opsForValue().get(key);
    }

    /**
     * 设置值
     *
     * @param key
     * @param value
     */
    public static void set(String key, String value) {
        redisUtils.redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 设置值,并设置过期时间
     *
     * @param key
     * @param value
     * @param expire 过期时间,单位秒
     */
    public static void set(String key, String value, Integer expire) {
        redisUtils.redisTemplate.opsForValue().set(key, value, expire, TimeUnit.SECONDS);
    }

    /**
     * 删出key
     *
     * @param key
     */
    public static void delete(String key) {
        redisUtils.redisTemplate.opsForValue().getOperations().delete(key);
    }

    /**
     * 设置对象
     *
     * @param key     key
     * @param hashKey hashKey
     * @param object  对象
     */
    public static void hset(String key, String hashKey, Object object) {
        redisUtils.redisTemplate.opsForHash().put(key, hashKey, object);
    }

    /**
     * 设置对象
     *
     * @param key     key
     * @param hashKey hashKey
     * @param object  对象
     * @param expire  过期时间,单位秒
     */
    public static void hset(String key, String hashKey, Object object, Integer expire) {
        redisUtils.redisTemplate.opsForHash().put(key, hashKey, object);
        redisUtils.redisTemplate.expire(key, expire, TimeUnit.SECONDS);
    }

    /**
     * 设置HashMap
     *
     * @param key key
     * @param map map值
     */
    public static void hset(String key, HashMap<String, Object> map) {
        redisUtils.redisTemplate.opsForHash().putAll(key, map);
    }

    /**
     * key不存在时设置值
     *
     * @param key
     * @param hashKey
     * @param object
     */
    public static void hsetAbsent(String key, String hashKey, Object object) {
        redisUtils.redisTemplate.opsForHash().putIfAbsent(key, hashKey, object);
    }

    /**
     * 获取Hash值
     *
     * @param key
     * @param hashKey
     * @return
     */
    public static Object hget(String key, String hashKey) {
        return redisUtils.redisTemplate.opsForHash().get(key, hashKey);
    }

    /**
     * 获取key的所有值
     *
     * @param key
     * @return
     */
    public static Object hget(String key) {
        return redisUtils.redisTemplate.opsForHash().entries(key);
    }

    /**
     * 删除key的所有值
     *
     * @param key
     */
    public static void deleteKey(String key) {
        redisUtils.redisTemplate.opsForHash().getOperations().delete(key);
    }

    /**
     * 判断key下是否有值
     *
     * @param key
     */
    public static Boolean hasKey(String key) {
        return redisUtils.redisTemplate.opsForHash().getOperations().hasKey(key);
    }

    /**
     * 判断key和hasKey下是否有值
     *
     * @param key
     * @param hasKey
     */
    public static Boolean hasKey(String key, String hasKey) {
        return redisUtils.redisTemplate.opsForHash().hasKey(key, hasKey);
    }

}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐