每日bug:redisTemplate注入失败

文章讲述了在后端服务拦截器识别token时遇到redisTemplate注入失败的问题,解决方案是创建一个RedisConfig配置类,设置RedisTemplateBean,并使用JedisConnectionFactory。当@Autowired无法正常工作时,创建一个RedisBean组件,使用@PostConstruct注解初始化redisTemplate。通过这种方式确保redisTemplate对象的可用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

场景:在完成后端服务拦截器中识别token的时候,redisTemplate注入失败,redisTemplate为null。

最终采取的解决办法:

这是设置的redisTemplate的配置。

package com.tsxy.etc.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@Slf4j
public class RedisConfig {

    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        return new JedisConnectionFactory();
    }

    @Bean
    RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, String> template = new RedisTemplate<String, String>();
        //未添加@Configuration注解,导致@Bean之间相互调用出错
        template.setConnectionFactory(jedisConnectionFactory());
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new StringRedisSerializer());
        return template;
    }
}

解决办法就是,编写一个redisBean用来生成模板,用来初始化模板:

package com.tsxy.etc.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 初始化生成一个redisTemplate.
 * 从而解决引入redisTemplate为null的情况
 */
@Component
@Slf4j
public class RedisBean {

    @Autowired
    private RedisTemplate redisTemplate;

    public static RedisTemplate redis;

    /**
     *PostConstruct在构造函数之后执行,init()方法之前执行。
     * 通常我们会是在Spring框架中使用到@PostConstruct注解 该注解的方法在整个Bean初始化中的执行顺序
     * Constructor(构造方法) -> @Autowired(依赖注入) -> @PostConstruct(注释的方法)
     */
    @PostConstruct
    public void getRedisTemplate() {
        redis = this.redisTemplate;
        log.info("初始化-------redisTemplate----");
    }
}

应用:
//使用token去redis的中查看是否有
        RedisTemplate redisTemplate = RedisBean.redis;
        Object value = redisTemplate.opsForValue().get(token);
        System.out.println(value);
        //给token续命
        redisTemplate.expire(token, Duration.ofDays(Constants.TOKEN_TIME));
        if (Objects.isNull(value)) {
            //redis中的token为空,拦截
            response.setStatus(401);
            String jsonString = JSONObject.toJSONString(res);
            response.getWriter().write(jsonString);
            return false;
        }

当@Autowired声明redisTemplate失效的时候,可以采用这种方式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值