static方法使用@Autowired

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangh92/article/details/84620246

set注入失败 构造器注入成功

@Component
@Slf4j
public class UserCookieInfoUtil {

    private static RedisTemplate<String, String> redisTemplate;

    private static JWTUtils jwtUtils;

    @Autowired
    public UserCookieInfoUtil(JWTUtils jwtUtils, RedisTemplate<String, String> redisTemplate) {
        UserCookieInfoUtil.jwtUtils = jwtUtils;
        UserCookieInfoUtil.redisTemplate = redisTemplate;
    }

    public static Map<String, Object> getCookieInfo(String token) {
        Map<String, Object> map = new HashMap<>();
        try {
            String loginName = jwtUtils.getLoginName(token);
            String info = redisTemplate.opsForValue().get(Constants.TOKEN_USER_INFO + ":" + loginName);
            map = JSONObject.parseObject(info == null ? "" : info, Map.class);
        } catch (Exception e){
            log.error("获取缓存中的登录信息失败:{}", e);
        }
        return map;
    }

}

猜你喜欢

转载自blog.csdn.net/wangh92/article/details/84620246