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; } }

  

posted on 2018-12-06 16:27  永不宕机  阅读(1026)  评论(0编辑  收藏  举报

导航