无法注入JedisUtil

来源:5-2 Shiro会话管理2

彼岸花123

2019-06-20 14:56

@Component
public class JedisUtil {

    @Autowired
    private JedisPool jedisPool;

    private Jedis getResource(){
        return jedisPool.getResource();
    }

    public byte[] set(byte[] key, byte[] value) {
        Jedis jedis=getResource();
        try {
            jedis.set(key,value);
            return value;
        } finally {
            jedis.close();
        }
    }

    public void expire(byte[] key, int value) {
        Jedis jedis=getResource();
        try {
            jedis.expire(key,value);
        } finally {
            jedis.close();
        }
    }

    public byte[] get(byte[] key) {
        Jedis jedis=getResource();
        try {
            return jedis.get(key);
        } finally {
            jedis.close();
        }
    }

    public void del(byte[] key) {
        Jedis jedis=getResource();
        try {
            jedis.del(key);
        } finally {
            jedis.close();
        }
    }

    public Set<byte[]> keys(String shiro_session_perfix) {
        Jedis jedis=getResource();
        try {
            return jedis.keys((shiro_session_perfix+"*").getBytes());
        } finally {
            jedis.close();
        }
    }
}
public class RedisSessionDao extends AbstractSessionDAO {

    private final String SHIRO_SESSION_PERFIX = "shiro-session";

    @Autowired
    private JedisUtil jedisUtil;

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.study.shiro.util.JedisUtil' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

写回答 关注

3回答

  • Java攻城猿
    2020-03-22 20:52:26

    我也遇到过,我的解决方法是springboot项目启动时候,把 jedis连接池初始化为bean就可以了

  • 慕设计3305918
    2019-09-24 16:46:50

    我也遇到了,找了半天原因是因为@Component没有被扫描

    在spring配置文件最前面加上这个:

    <context:component-scan base-package="com.kl.shiro.util"/>



  • 幻化成孤岛
    2019-09-04 08:54:06

    怎么解决的?老哥

Shiro安全框架入门

从零入门Shiro安全框架

48036 学习 · 332 问题

查看课程

相似问题