I. knowledge points
When writing aspectj aspect, You can embed the entry point expression in the notification annotation directly. However, the same entry point expression may be repeated in multiple notifications.
Like many other AOP implementations, aspectj allows you to define the entry point independently and reuse it in multiple notifications.
Ii. Sample Code
In aspectj aspect, the entry point can be declared as a simple method with @ pointcut annotation. The method body of the entry point is usually empty, because it is unreasonable to mix the entry point definition with the application logic. The access modifier of the Entry Method controls the visibility of the entry. Other notifications can reference this entry point with the method name.
package com.codeproject.jackie.springrecipesnote.springaop;import java.util.Arrays;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;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;/** * @author jackie * */@Aspectpublic class CalculatorLoggingAspect {private Log log = LogFactory.getLog(this.getClass());@Pointcut("execution(* *.*(..))")private void loggingOperation() {}@Before("loggingOperation()")public void logBefore(JoinPoint joinPoint) {log.info("The method " + joinPoint.getSignature().getName()+ "() begins with " + Arrays.toString(joinPoint.getArgs()));}@After("loggingOperation()")public void logAfter(JoinPoint joinPoint) {log.info("The method " + joinPoint.getSignature().getName() + "() ends");}@AfterReturning(pointcut = "loggingOperation())", returning = "result")public void logAfterReturning(JoinPoint joinPoint, Object result) {log.info("The method " + joinPoint.getSignature().getName() + "() ends with " + result);}@AfterThrowing(pointcut = "loggingOperation())", throwing = "e")public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {log.error("An exception " + e + " has been thrown in " + joinPoint.getSignature().getName() + "()");}@Around("loggingOperation()")public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {log.info("The method " + joinPoint.getSignature().getName() + "() begins with " + Arrays.toString(joinPoint.getArgs()));try {Object result = joinPoint.proceed();log.info("The method " + joinPoint.getSignature().getName() + "() ends with " + result);return result;} catch (IllegalArgumentException e) {log.error("Illegal argument " + Arrays.toString(joinPoint.getArgs()) + " in " + joinPoint.getSignature().getName() + "()");throw e;}}}
Generally, if the entry point is shared among multiple aspect instances, it is best to centralize them into a public class. In this case, they must be declared as public.
package com.codeproject.jackie.springrecipesnote.springaop;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Pointcut;/** * @author jackie * */@Aspectpublic class CalculatorPointcuts {@Pointcut("execution(* *.*(..))")public void loggingOperation() {}}
This entry point must contain the class name. If the class is not in the same package as the aspect, the package name must also be included.
@Before("CalculatorPointcuts.loggingOperation()")public void logBefore(JoinPoint joinPoint) {log.info("The method " + joinPoint.getSignature().getName()+ "() begins with " + Arrays.toString(joinPoint.getArgs()));}@After("CalculatorPointcuts.loggingOperation()")public void logAfter(JoinPoint joinPoint) {log.info("The method " + joinPoint.getSignature().getName() + "() ends");}@AfterReturning(pointcut = "CalculatorPointcuts.loggingOperation())", returning = "result")public void logAfterReturning(JoinPoint joinPoint, Object result) {log.info("The method " + joinPoint.getSignature().getName() + "() ends with " + result);}@AfterThrowing(pointcut = "CalculatorPointcuts.loggingOperation())", throwing = "e")public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {log.error("An exception " + e + " has been thrown in " + joinPoint.getSignature().getName() + "()");}@Around("CalculatorPointcuts.loggingOperation()")public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {log.info("The method " + joinPoint.getSignature().getName() + "() begins with " + Arrays.toString(joinPoint.getArgs()));try {Object result = joinPoint.proceed();log.info("The method " + joinPoint.getSignature().getName() + "() ends with " + result);return result;} catch (IllegalArgumentException e) {log.error("Illegal argument " + Arrays.toString(joinPoint.getArgs()) + " in " + joinPoint.getSignature().getName() + "()");throw e;}}