AOP

20 May 2023 . spring .

AOP is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does this by adding additional behavior to existing code without modifying the code itself.

Concepts

Business Object

public class SampleAdder {
	public int add(int a, int b) {
		return a + b;
	}
}

Aspect

An aspect is a modularization of a concern that cuts across multiple classes. Unified logging can be an example of such cross-cutting concern.

public class AdderAfterReturnAspect {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    public void afterReturn(Object returnValue) throws Throwable {
        logger.info("value return was {}",  returnValue);
    }
}

JointPoint

A Joinpoint is a point during the execution of a program, such as the execution of a method or the handling of an exception.

Pointcut

A Pointcut is a predicate that helps match an Advice to be applied by an Aspect at a particular JoinPoint.

We often associate the Advice with a Pointcut expression, and it runs at any Joinpoint matched by the Pointcut.

Advice

An Advice is an action taken by an aspect at a particular Joinpoint.

  1. Before: These types of advices execute before the joinpoint methods and are configured using @Before annotation mark.
  2. After returning: These types of advices execute after the joinpoint methods completes executing normally and are configured using @AfterReturning annotation mark.
  3. After throwing:  These types of advices execute only if joinpoint method exits by throwing an exception and are configured using @AfterThrowing annotation mark.
  4. After (finally): These types of advices execute after a joinpoint method, regardless of the method’s exit whether normally or exceptional return and are configured using @After annotation mark.
  5. Around: These types of advices execute before and after a joinpoint and are configured using @Around annotation mark.

Configuration

<bean id="sampleAdder" class="org.baeldung.logger.SampleAdder" />
<bean id="doAfterReturningAspect" 
  class="org.baeldung.logger.AdderAfterReturnAspect" />
<aop:config>
    <aop:aspect id="aspects" ref="doAfterReturningAspect">
       <aop:pointcut id="pointCutAfterReturning" expression=
         "execution(* org.baeldung.logger.SampleAdder+.*(..))"/>
       <aop:after-returning method="afterReturn"
         returning="returnValue" pointcut-ref="pointCutAfterReturning"/>
    </aop:aspect>
</aop:config>
  • We defined a simple bean called simpleAdder, which represents an instance of a Business Object.
  • We created an instance of an Aspect called AdderAfterReturnAspect.
  • Within the config tag, we define the class that represents an aspect. Then we give it a reference of doAfterReturningAspect, an aspect bean that we created.
  • Next we define a Pointcut using the pointcut tag. The pointcut used in the example above is execution(* org.baeldung.logger.SampleAdder+.*(..)), which means apply an advice on any method within the SampleAdder class that accepts any number of arguments and returns any value type.
  • Then we define which advice we want to apply. In the above example, we applied the after-returning advice. We defined this in our Aspect AdderAfterReturnAspect by executing the afterReturn method that we defined using the attribute method.