xufenglei
2017-12-13 7a5024966dcce9811f4c1faf694b6b9bf0c2983e
src/main/java/com/moral/common/aop/ControllerAOP.java
@@ -1,14 +1,21 @@
package com.moral.common.aop;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import com.moral.common.bean.ResultBean;
import com.moral.common.exception.BusinessException;
import com.moral.common.exception.ValidateException;
import lombok.extern.log4j.Log4j;
@@ -16,13 +23,15 @@
@Aspect
@Component
public class ControllerAOP {
   @Around("execution(* com.moral.controller.*Controller.*(..))")
   public Object handlerControllerMethod(ProceedingJoinPoint pjp) {
   public Object handlerControllerMethod(ProceedingJoinPoint pjp) throws Exception {
      long startTime = System.currentTimeMillis();
        Map<String, Object> result;
      Object result;
      try {
         result = (Map<String, Object>) pjp.proceed();
         result = pjp.proceed();
         log.info(pjp.getSignature() + "use time:" + (System.currentTimeMillis() - startTime));
      } catch (Throwable e) {
         result = handlerException(pjp, e);
@@ -31,18 +40,40 @@
      return result;
   }
   private Map<String, Object> handlerException(ProceedingJoinPoint pjp, Throwable e) {
      Map<String, Object> result = new HashMap<String, Object>();
      // 已知异常
   @SuppressWarnings("rawtypes")
   private Object handlerException(ProceedingJoinPoint pjp, Throwable e) throws Exception {
      Signature sig = pjp.getSignature();
      MethodSignature msig = null;
      if (!(sig instanceof MethodSignature)) {
         throw new IllegalArgumentException("该注解只能用于方法");
      }
      msig = (MethodSignature) sig;
      Object target = pjp.getTarget();
      Method currentMethod = target.getClass().getMethod(msig.getName(), msig.getParameterTypes());
      Type type = currentMethod.getGenericReturnType();
      String message = "";
      if (e instanceof BusinessException) {
         result.put("msg", e.getLocalizedMessage());
         message = e.getLocalizedMessage();
      } else if (e instanceof ValidateException) {
         message = e.getLocalizedMessage();
      } else {
         log.error(pjp.getSignature() + " error ", e);
         result.put("msg", e.toString());
         //TODO 未知的异常,应该格外注意,可以发送邮件通知等
         message = e.toString();
      }
      return result;
      if (type instanceof ParameterizedType) {
         Type rawType = ((ParameterizedType) type).getRawType();
         if (rawType == ResultBean.class) {
            ResultBean<?> resultBean = new ResultBean(ResultBean.FAIL);
            resultBean.setMessage(message);
            return resultBean;
         } else if (rawType == Map.class) {
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("msg",message);
            return result;
         }
      }
      return null;
   }
}