• springboot中帮助类中获取bean


    @Component
    public class OperationUtil implements ApplicationContextAware {
       // 获取redis接口
        private static RedisService redisService;
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            if(redisService == null){
           // 从applicationContext中获取bean OperationUtil.redisService = (RedisService)applicationContext.getBean("redisService"); } } public static Operation saveOperation(Long hid, Long oid, Long pid, String content){ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); Operation op = null; String token = request.getHeader("token"); if (StringUtils.isNotEmpty(token)) { JWToken jwToken = new JWToken(); //获取解析token中的值 HashMap<String, Object> map = jwToken.parseJWT(token); Long uid = null; String account = null; if (map != null) { if (map.get("uid") != null && map.get("uid").toString() != "") { uid = Long.valueOf(map.get("uid").toString()); /*User user = userDao.getUserByUid(uid); if(user != null && user.getSid() != null){ sid = user.getSid(); }*/ if((hid == null || hid <= 0)){ if(redisService.get("hospital" + uid) != null){ hid = Long.valueOf((Integer)redisService.get("hospital" + uid)); } } } if (map.get("account") != null) { account = (String) map.get("account"); } op = new Operation(hid, oid, pid, uid, account, content); } }else{ op = new Operation(null, null, null, null, null, content); } return op; } public static Long getUserId(){ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); String token = request.getHeader("token"); JWToken jwToken = new JWToken(); //获取解析token中的值 HashMap<String,Object> map = jwToken.parseJWT(token); Long uid = null; if(map != null) { if (map.get("uid") != null && map.get("uid").toString() != "") { uid = Long.valueOf(map.get("uid").toString()); } } return uid; } }

      

  • 相关阅读:
    person
    汽车.
    矩形
    设计模式
    汽车
    三角形
    银行
    西游记
    面向对象
    随机生成4位验证码,输入验证码与生成的比较,最多输入5次
  • 原文地址:https://www.cnblogs.com/zhlblogs/p/10077435.html
Copyright © 2020-2023  润新知