工具类注入需要的service

本文介绍了一个Spring Boot应用如何通过整合Redis来缓存登录员工的详细信息,包括使用@Component注解使类被Spring扫描,@Autowired注入依赖服务,以及@PostConstruct注解初始化静态对象的方法。
/**
 *  从redis获取信息
 * @author yy
 *
 */

@Component//关键一:添加此注解才能被spring扫描到
public class CacheUtil {
    
    private static Logger logger = LoggerFactory.getLogger(CacheUtil.class);
    
    @Autowired
    private JJWTUtils jwt;  //关键二:添加需要的服务类
    public static CacheUtil cacheUtil; //关键三:声明一个当前类的静态对象
    
    
    @PostConstruct  //关键四:通过@PostConstruct注解实现注入
    public void init() {
        cacheUtil = this;
        //cacheUtil.jwtUtils =  this.jwtUtils;
    }
    
    
    /**
     * 从Redis中获取登录的员工详细信息
     * @Title: getRedisEmp
     * @Description:
     * @return
     * @return EmployeVo
     * @author yy  2019-6-26 16:11:31
     */
    @SuppressWarnings("unused")
    private static EmployeVo getRedisEmp(HttpServletRequest request) {
        EmployeVo emp = null;
        JwtBodyBean jwtBody = cacheUtil.jwt.getJwtBody(request);
        if (jwtBody != null) {
            Long uid = jwtBody.getUid();
            Object jsonObj = cacheUtil.jwt.redisService.get(String.valueOf(uid));
            if (jsonObj != null) {
                JSONObject json = JSONObject.parseObject(jsonObj.toString());
                emp = new EmployeVo();
                try {
                    emp.setEmId(json.getString("emId"));
                    emp.setDepartmentId(json.getString("departmentId"));
                    emp.setEmAccount(json.getString("emAccount"));
                    emp.setEmPassword(json.getString("emPassword"));
                    emp.setUserName(json.getString("userName"));
                    emp.setEmPhone(json.getString("emPhone"));
                    emp.setSeatId(json.getString("seatId"));
                    emp.setEmLevel(json.getIntValue("emLevel"));
                    emp.setCrmUserId(json.getString("crmUserId"));
                    emp.setAcAccount(json.getString("acAccount"));
                    emp.setAcPassword(json.getString("password"));
                    emp.setBs(json.getIntValue("bs"));
                    emp.setTs(json.getString("ts"));
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.error("从redis查询员工信息异常!", e.toString());
                }
            }
        }
        return emp;
    }

}

 

转载自 https://blog.youkuaiyun.com/loveer0/article/details/83716040

 

转载于:https://www.cnblogs.com/yangyang2018/p/11091349.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值