What is aspect oriented programming (aop) in spring (examples) ?

Aspect Oriented programming in spring

The aspect oriented programming (aop) is very important module of spring framework. As the nomenclature suggests that programming is driven from aspect. In aspect oriented programming we are concerned about the basic, independent logical unit. These logical unit (or concerns) are not part of business application. These logical unit are span across different application modules or layers hence called the cross cutting concerns.

example aspect oriented programming
Fig 1: Example of aspects

Let us take an example to understand it further. We will discuss about the logging aspect as shown in Fig 1. we need to have logging in place, Aspect modularize, the logging of application under particular Aspect. It means that all logging of application can be handled in logging Aspect, we need not write the logging in any of the classes (In Application). As logging is can be a logical unit which can be used across all the modules of applications, we will make it as an aspect.

How can we achieve aspect oriented programming ?

To make a class or logical unit as an aspect, we need to follow below steps.

  1. Declare the Class with @Aspect Annotation
  2. Mark the class with @Component Annotation.
  3. Enable Aspectj autoproxy (we can use either of below options)
    1. Add in servlet-context.xml     or
    2. Annotate the aspect with @EnableAspectJAutoProxyannotation
  4. Declare pointcut expression annotated with @Pointcut annotation.
  5. Advice the methods of aspect ( or join point)

What is pointcut in aop?

The set of join points where advice shall be executed. Let us see the example of pointcut expression.

@Pointcut("within(@org.springframework.stereotype.Controller *)")
public void controller() {
}

We are interested in all the resources which are annotated with @Controller annotation. So any call to @Controller resource will be intercepted.

What is advice in aop?

action taken by an aspect at a particular join point. We have shown the different kind of advices and their corresponding description as follows:

S. No.Advice Description
1before Executes before the execution of method
2after executes after the method execution.
3around Its more or less combination of before and after advice. It execute around particular method
4after-returning Advice will executes if method executed successfully
5after-throwing Advice will be executed if method throws an exception.

Example of around advice method:

Advice that executes before and after execution of a join point. Let us declare a pointcut expression demonstrate the executions of all methods in application.
Pointcut expression is:

@Pointcut("execution(* *.*(..))")
protected void allMethod() {
}

Let us apply the pointcut expression to around advice:

@Around("allMethod() ")

Apply around advice to pointcut allMethod declared earlier.

@Around("allMethod() ")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
Logging aspect around advice
Fig 2: Around Advice
  1. Around advice will be executed before execution of any method (suppose foo() ) in application
  2. Method execution will be starts and completes (foo() )
  3. Around advice will be executed after execution of method (foo())

We have demonstrated the around advice execution in Fig 2. Suppose foo() method is in any class of web application.

Examples of pointcuts expression & applying pointcut to advices.

controller: controller Pointcut to intercept any call to resource annotated with @Controller annotation

@Pointcut("within(@org.springframework.stereotype.Controller *)")
public void controller() { 
}

allMethod pointcut will intercepts the all method execution in any application.

@Pointcut("execution(* *.*(..))")
protected void allMethod() {
}

logAnyFunctionWithinResource pointcut will intercepts any method execution in particular package ( we have declare package org.learn.log package.)

@Pointcut("within(org.learn.log..*)")
private void logAnyFunctionWithinResource() {
}

Before advice in spring aop:  

Let us apply the pointcuts expression (discussed above) to before advice. Advice that executes before the execution of a join point.

@Before(“controller() && allMethod() && args(..,request)”)
public void logBefore(JoinPoint joinPoint, HttpServletRequest request) {

So, logBefore advice will be executed when

  1. Any method of classes which is annotated with @Controller annotation
  2. AND
  3. All methods of application
  4. AND
  5. Method which is taking last parameter of type HttpServletRequest type.

Similarly we can apply pointcut expressions to rest of advices. We have applied all the discussed pointcut expression Spring AOP Logging aspect post.

Scroll to Top