-
Notifications
You must be signed in to change notification settings - Fork 0
/
JoinPointBaseLoggingAspect.java
96 lines (73 loc) · 3.6 KB
/
JoinPointBaseLoggingAspect.java
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
85
86
87
88
89
90
91
92
93
94
95
96
package com.ysingh.springaop.joinpoint.aspect;
import java.util.List;
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.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.ysingh.springaop.joinpoint.dao.EquipmentDAO;
@Aspect
@Order(3)
@Component
public class JoinPointBaseLoggingAspect {
@Pointcut("execution(void com.ysingh.springaop.joinpoint.dao.*.*(..))")
private void forDaoPackage() {}
@Pointcut("execution(void com.ysingh.springaop.joinpoint.dao.*.get*(..))")
private void getter() {}
@Pointcut("execution(void com.ysingh.springaop.joinpoint.dao.*.set*(..))")
private void setter() {}
@Pointcut("forDaoPackage() && !(getter() || setter())")
private void forDaoPackageNoGetterSetter() {}
@Before("forDaoPackageNoGetterSetter()")
public void beforeAddEquipmentAdvice(JoinPoint joinPoint) {
System.out.println("\n=====>>> JoinPointBaseLoggingAspect - beforeAddEquipmentAdvice - Executing @Before Advice on method");
MethodSignature methodSig = (MethodSignature) joinPoint.getSignature();
System.out.println("\n=====>>> JoinPointBaseLoggingAspect - beforeAddEquipmentAdvice - "+methodSig);
Object[] args = joinPoint.getArgs();
for(Object arg : args) {
if(arg instanceof EquipmentDAO) {
System.out.println("\n=====>>> JoinPointBaseLoggingAspect - beforeAddEquipmentAdvice - ID : "+((EquipmentDAO) arg).getId());
System.out.println("\n=====>>> JoinPointBaseLoggingAspect - beforeAddEquipmentAdvice - Name : "+((EquipmentDAO) arg).getName());
} else {
System.out.println("\n=====>>> JoinPointBaseLoggingAspect - beforeAddEquipmentAdvice - Premium Indicator : "+arg);
}
}
}
@AfterReturning(
pointcut="execution(* com.ysingh.springaop.joinpoint.dao.EquipmentDAO.findEquipments(..))",
returning="result")
public void afterReturningGetEquipmentAdvice(JoinPoint joinPoint, List<EquipmentDAO> result) {
System.out.println("\n=====>>> JoinPointBaseLoggingAspect - afterReturningGetEquipmentAdvice:");
result.stream().forEach(System.out::println);
EquipmentDAO equipmentDAO = new EquipmentDAO();
equipmentDAO.setId("ID-004");
equipmentDAO.setName("Adapter");
result.add(equipmentDAO);
}
@AfterThrowing(
pointcut="execution(* com.ysingh.springaop.joinpoint.dao.EquipmentDAO.deleteEquipment(..))",
throwing="ex")
public void afterThrowingDeleteEquipmentAdvice(JoinPoint joinPoint, Throwable ex) {
System.out.println("\n=====>>> JoinPointBaseLoggingAspect - afterThrowingDeleteEquipmentAdvice:");
System.out.println("The exception is : "+ex);
}
@After("execution(* com.ysingh.springaop.joinpoint.dao.EquipmentDAO.deleteEquipment(..))")
public void afterDeleteEquipmentAdvice() {
System.out.println("\n=====>>> JoinPointBaseLoggingAspect - afterDeleteEquipmentAdvice: RELEASING CONNECTIONS");
}
@Around("execution(* com.ysingh.springaop.around.service.*.getFortune(..))")
public Object aroundGetFortune(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
long start = System.currentTimeMillis();
Object proceed = proceedingJoinPoint.proceed();
long end = System.currentTimeMillis();
System.out.println("\n=====>>> Duration: " + (end - start)/1000.0 + " seconds");
return proceed;
}
}