您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> Java >> SpringAop中将数据序列化后,反反序列化取数据,数据类型不一致

SpringAop中将数据序列化后,反反序列化取数据,数据类型不一致(1/2)

来源:网络整理     时间:2016/6/28 8:16:23     关键词:

关于网友提出的“ SpringAop中将数据序列化后,反反序列化取数据,数据类型不一致”问题疑问,本网通过在网上对“ SpringAop中将数据序列化后,反反序列化取数据,数据类型不一致”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: SpringAop中将数据序列化后,反反序列化取数据,数据类型不一致
描述:

使用切面进行缓存处理时,不影响现有系统的稳定性,所以将值进行Json序列化后存入缓存,一般对象可以实现解析,但是碰到复杂对象时,就难办了,比如List,这种格式的反序列化后 Person就编程了JsonObject了.

大牛有好的办法解决这个问题不,代码如下

@Pointcut("execution(* com.haoyi.business.registered.service.DoctorInfoService.selectDoctorInfo*(..))")
  public void registerDataPoint() {}

  @SuppressWarnings("unchecked")
  @Around("registerDataPoint()")
  public Object aroundPoint(ProceedingJoinPoint thisJoinPoint) {
    logger.debug("进入aroundPoint切面方法.....");
    try {
      MemcachedHelper memcachedHelper = MemcachedHelper.getInstance();
      String  enCacheName =getUniqueSid(thisJoinPoint);
      if (memcachedHelper.keyExists(enCacheName)) {
        logger.debug(enCacheName+",直接取出缓存数据....");
        return JSONObject.parseObject((String)memcachedHelper.get(enCacheName),((MethodSignature) thisJoinPoint.getSignature()).getReturnType());
      }
      Object obj = (Object) thisJoinPoint.proceed();
      logger.debug("记录缓存,Name:"+enCacheName);
      memcachedHelper.set(enCacheName, JSONObject.toJSONString(obj), new Date( 5 * 60 * 1000));
      return obj;
    } catch (Throwable e) {
      logger.error("进入切面抛出异常....");
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 计算缓存唯一名称
   * @param thisJoinPoint
   * @return
   */
  private String getUniqueSid(ProceedingJoinPoint thisJoinPoint){
    String enCacheName = FcConstant.HY_SERVICE_MARK;
    String cacheName = thisJoinPoint.getSignature().toShortString();
    if (null != thisJoinPoint.getArgs()) {
      for (int i = 0; i < thisJoinPoint.getArgs().length; i++) {
        cacheName += "|" + JSONObject.toJSONString(thisJoinPoint.getArgs()[i]);
      }
    }
    enCacheName+= GoodDocMd5Util.getMd5Format32(cacheName);
    return enCacheName;
  }
当碰到List这种数据时,就出问题了
((MethodSignature) thisJoinPoint.getSignature()).getReturnType()

这个获取来的就是单纯的interface java.util.List,没反序列化成功,有完美的解决办法没

相关图片

相关文章