ZhuDongming
2020-04-28 264ae12101bff5dc71f864f72028bb1f1db17831
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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;
 
@Log4j
@Aspect
@Component
public class ControllerAOP {
 
    @Around("execution(* com.moral.controller.*Controller.*(..))")
    public Object handlerControllerMethod(ProceedingJoinPoint pjp) throws Exception {
 
        long startTime = System.currentTimeMillis();
        Object result;
 
        try {
            result = pjp.proceed();
            // log.info(pjp.getSignature() + "use time:" + (System.currentTimeMillis() - startTime));
        } catch (Throwable e) {
            result = handlerException(pjp, e);
        }
 
        return result;
    }
 
    @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 = 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();
        }
    }
}