Aspect Oriented Programming Notes #3 – Some Additional Info

Reuse Aspect Pointcut Definitions

@Pointcut annotation is used for defining a pointcut independently to be reused in multiple advices.

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
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;

@Aspect
@Component
public class ReuseCalculatorLoggingAspect {

	@Pointcut("execution(* *.*(..))")
	private void loggingOperation() {
		
	}
	
	@Before("loggingOperation()")
	public void logBefore(JoinPoint joinPoint) {
	
	}
	
	@AfterReturning(pointcut = "loggingOperation()", returning = "result")
	public void logAfterReturning(JoinPoint joinPoint, Object result) {
	
	}
	
	@AfterThrowing(pointcut = "loggingOperation()",	throwing = "e")
	public void logAfterThrowing(JoinPoint joinPoint, IllegalArgumentException e) {
	
	}

	@Around("loggingOperation()")
	public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
		return null;
	}

}

Generally, if our pointcuts are shared between multiple aspects, it is better to centralize them in a common class. In this case, they must be declared as public. When we refer to this pointcut, we have to include the class name as well. If the class is not located in
the same package as the aspect, we have to include the package name also.

Use Custom Annotation for Matching Join Point

Sometimes we may not be able to find any common characteristics (e.g., modifiers, return types, method name patterns, or arguments) for the methods we want to match. In such cases, we can consider providing a custom annotation for them. For instance, defining the following marker annotation. This annotation can be applied to both method level and type level.

@Target( { ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LoggingRequired {

}
@LoggingRequired
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
   public double add(double a, double b) {
    ...
   }
   public double sub(double a, double b) {
    ...
   }
}

Then we can write a pointcut expression to match a class or methods with the @LoggingRequired annotation using the annotation keyword on the @Pointcut annotation.

@Aspect
public class CalculatorPointcuts {
    @Pointcut("annotation(com.aopexample.LoggingRequired)")
    public void loggingOperation() {

    }
}

Use Type Signature Patterns

Another kind of pointcut expression matches all join points within certain types. When applied to Spring AOP, the scope of these pointcuts will be narrowed to matching all method executions within the types.

The following pointcut matches all the method execution join points within the com.aopexample package:

within(com.aopexample.*)

To match the join points within a package and its subpackage, we have to add one more dot before the wildcard.

within(com.aopexample..*)

The following pointcut expression matches the method execution join points within a particular class:

within(com.aopexample.ArithmeticCalculatorImpl)

If the target class is located in the same package as this aspect, the package name can be omitted:

within(ArithmeticCalculatorImpl)

We can match the method execution join points within all classes that implement the
ArithmeticCalculator interface by adding a plus symbol.

within(ArithmeticCalculator+)

Combine (Conditional) Pointcut Expressions

In AspectJ, pointcut expressions can be combined with the operators && (and), || (or), and ! (not).

@Aspect
public class CalculatorPointcuts {
	@Pointcut("within(ArithmeticCalculator+)")
	public void arithmeticOperation() {
	}

	@Pointcut("within(UnitCalculator+)")
	public void unitOperation() {
	}

	@Pointcut("arithmeticOperation() || unitOperation()")
	public void loggingOperation() {
	}
}

About Aliyar Güneş

I’am Aliyar Güneş, a learner and software developer from Istanbul, Turkey. I write C# and Java.
This entry was posted in Spring Framework, Works and tagged , , , , . Bookmark the permalink.

Leave a Reply