切面

依赖

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

代码示例

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
@Aspect
@Component
public class SystemLogAspect implements Ordered {

@Autowired
private SystemLogDAO systemLogDAO;
@Autowired
private LoginLogDAO loginLogDAO;

@Autowired
private GlobalProperties globalProperties;

@Override
public int getOrder() {
return this.electricGlobalProperties.getSystemLogAspectOrder();
}

@Pointcut(
value = "@within(org.springframework.stereotype.Controller)||@within(org.springframework.web.bind.annotation.RestController)")
private void systemLogPointcut() {}

//环绕通知
@Around("systemLogPointcut()")
public Object systemLog(ProceedingJoinPoint joinPoint) throws Throwable {
Class<?> classTarget = joinPoint.getTarget().getClass();
if (classTarget.isAssignableFrom(BasicErrorController.class)) {
return joinPoint.proceed();
}
MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
Method objMethod = classTarget.getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
LogIgnore logIgnore = objMethod.getAnnotation(LogIgnore.class);
if (logIgnore != null) {
return joinPoint.proceed();
}
logIgnore = objMethod.getDeclaringClass().getAnnotation(LogIgnore.class);
if (logIgnore != null) {
return joinPoint.proceed();
}
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
if (classTarget.equals(LoginController.class)){
Object result = joinPoint.proceed();
return result;
}
//.....
return joinPoint.proceed();
}
}