shiro整合redis做缓存遇到的一个坑

1 篇文章 0 订阅

一、问题提出

通过注入的方式注入redisTemplate,dubug调试发现redisTemplate注入的为null,运行报空指针错误!!
但是通过自定义工厂工具类ApplicationContextUtils注入redisTemplate却能成功!!
不明白为什么!!??

自定义工厂工具类ApplicationContextUtils(用于获取redisTemplate)

package com.yjh.shirospringbootjsp.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextUtils implements ApplicationContextAware {

    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context=applicationContext;
    }

    //根据bean名字获取工厂指定bean对象
    public static Object getBean(String beanName){
        return context.getBean(beanName);
    }
}

自定义redis缓存的实现类

package com.yjh.shirospringbootjsp.shiro.cache;

import com.yjh.shirospringbootjsp.utils.ApplicationContextUtils;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Collection;
import java.util.Set;

//自定义redis缓存的实现
@Component
public class RedisCache<k,v> implements Cache<k,v> , Serializable {

    @Autowired
    private RedisTemplate redisTemplate;


    private String cacheName;

    public RedisCache(){}

    public RedisCache(String cacheName){
        this.cacheName=cacheName;
    }


    @Override
    //@Autowired
    public v get(k k) throws CacheException {
        System.out.println("get key"+k);
        System.out.println("=============================get==========================");
        //return (v) getRedisTemplate().opsForValue().get(k.toString());
        return (v) getRedisTemplate().opsForHash().get(this.cacheName,k.toString());
    }

    @Override
    public v put(k k, v v) throws CacheException {//@Component  自动注入  (工厂工具类获取beanName:redisTemplate对象)
        System.out.println("put key"+k);
        System.out.println("put value"+v);
        System.out.println("=============================put==========================");
        //getRedisTemplate().opsForValue().put(k.toString(),v);
        getRedisTemplate().opsForHash().put(this.cacheName,k.toString(),v);

        return null;
    }

    @Override
    public v remove(k k) throws CacheException {
        System.out.println("=============================remove==========================");
        return (v) getRedisTemplate().opsForHash().delete(this.cacheName,k.toString());
    }

    @Override
    public void clear() throws CacheException {
        System.out.println("=============================clear==========================");
        getRedisTemplate().delete(this.cacheName);
    }

    @Override
    public int size() {
        System.out.println("=============================size==========================");
        return getRedisTemplate().opsForHash().size(this.cacheName).intValue();
    }

    @Override
    public Set<k> keys() {
        System.out.println("=============================keys==========================");
        return getRedisTemplate().opsForHash().keys(this.cacheName);
    }

    @Override
    public Collection<v> values() {
        System.out.println("=============================values==========================");
        return getRedisTemplate().opsForHash().values(this.cacheName);
    }


    public RedisTemplate  getRedisTemplate() {

        //RedisTemplate redisTemplate = (RedisTemplate) ApplicationContextUtils.getBean("redisTemplate");
        //发现通过ApplicationContextUtils注入可以,但是通过@Autowired private RedisTemplate redisTemplate;注入会报空指针异常

        RedisTemplate redisTemplate = (RedisTemplate) ApplicationContextUtils.getBean("redisTemplate");//这样是可以的
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        return redisTemplate;
    }

}

二、问题解决

暂时还没有解决!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值