Repository navigation
Expand file tree
/
Copy pathResultAspect.java
More file actions
84 lines (61 loc) · 2.1 KB
/
Copy pathResultAspect.java
File metadata and controls
84 lines (61 loc) · 2.1 KB
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
81
82
83
84
/**
*
*/
package com.test.other;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* @author aubrey
* @date 下午4:13:55
* 自定义注解配置
*/
//@Component
//@Aspect
public class ResultAspect {
// @Pointcut("execution(* com.test.controller.*.*(..))")
// @Pointcut("@annotation(com.test.web.aop.LogAnnotation)")
public void logtest() {};
@Before("logtest()")
public void before(JoinPoint joinPint) {
/*// joinPint.getSignature()
*/ System.out.println("aspect开始执行方法:================");
}
@After("logtest()")
public void after() {
System.out.println("aspect结束执行方法:================");
}
@Around("logtest()")
public Object arround(ProceedingJoinPoint pjp) throws Throwable {
long a = System.currentTimeMillis();
Object obj = null;
OutputJson out = null;
try {
obj = pjp.proceed();//执行真正的方法
out = new OutputJson(200, "success", obj);
}catch (Exception e) {
out = new OutputJson(500, e.getMessage(), obj);
}
long b = System.currentTimeMillis();
System.out.println("方法执行时间:" + (b-a));
return out.toString();
}
@AfterThrowing(pointcut="logtest()",throwing="e")
public void afterThrowing(JoinPoint pjp, Throwable e) throws Throwable {
OutputJson out = new OutputJson(500, e.getMessage(), "");
System.out.println(out.toString());
}
@AfterReturning(pointcut="logtest()" , returning="result")
public Object afterReturning(JoinPoint pj,Object result) throws Throwable {
System.out.println("afterreturing..........");
OutputJson out = new OutputJson(200, "success", result);
return out.toString();
}
}