From be9fe39e90412b4b46c8348bea94d0edaa1b19ae Mon Sep 17 00:00:00 2001
From: xufenglei <xufenglei>
Date: Mon, 12 Nov 2018 16:55:32 +0800
Subject: [PATCH] update
---
src/main/java/com/moral/common/aop/ControllerAOP.java | 64 ++++++++++++++++++++++++--------
1 files changed, 48 insertions(+), 16 deletions(-)
diff --git a/src/main/java/com/moral/common/aop/ControllerAOP.java b/src/main/java/com/moral/common/aop/ControllerAOP.java
index bd542c7..9e7c357 100644
--- a/src/main/java/com/moral/common/aop/ControllerAOP.java
+++ b/src/main/java/com/moral/common/aop/ControllerAOP.java
@@ -1,14 +1,22 @@
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.AppData;
+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 +24,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 +41,40 @@
return result;
}
- private Map<String, Object> handlerException(ProceedingJoinPoint pjp, Throwable e) {
- Map<String, Object> result = new HashMap<String, Object>();
-
- // ������������
- if (e instanceof BusinessException) {
- result.put("msg", e.getLocalizedMessage());
- } else {
- log.error(pjp.getSignature() + " error ", e);
- result.put("msg", e.toString());
- //TODO ������������������������������������������������������������������
+ @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("���������������������������");
}
-
- return result;
+ msig = (MethodSignature) sig;
+ Object target = pjp.getTarget();
+ Method currentMethod = target.getClass().getMethod(msig.getName(), msig.getParameterTypes());
+ Type type = currentMethod.getGenericReturnType();
+ String message = e.getMessage();
+ Map<String, Object> resultMap = new HashMap<String, Object>();
+ if (e instanceof BusinessException) {
+ log.error(e.getMessage());
+ resultMap.put("msg", message);
+ } else if (e instanceof ValidateException) {
+ } else {
+ log.error(pjp.getSignature() + " error: " + e.toString(), e);
+ }
+ if (type instanceof ParameterizedType) {
+ type = ((ParameterizedType) type).getRawType();
+ }
+ if (type == AppData.class) {
+ return new AppData(message, AppData.FAIL);
+ } else if (type == ResultBean.class) {
+ return new ResultBean(e);
+ } else if (type == Map.class) {
+ resultMap.put("msg", message);
+ return resultMap;
+ } else if (type == Void.TYPE) {
+ return null;
+ } else {
+ return type.getClass().newInstance();
+ }
}
}
--
Gitblit v1.8.0