Intro To Spring’s Aspects

What Is A Spring Aspect? ?

Simply put, it’s a reusable part of code (Advice) that is injected into a certain part (joinPoint) of the application (defined by Pointcuts) at runtime.

Why Use it? ?

Address common concerns across our …


This content originally appeared on DEV Community and was authored by Abdulcelil Cercenazi

What Is A Spring Aspect? ?

Simply put, it's a reusable part of code (Advice) that is injected into a certain part (joinPoint) of the application (defined by Pointcuts) at runtime.

Why Use it? ?

Address common concerns across our application.

Like what?

  • Logging ?

  • Transaction management ?

  • Caching ?

  • Security ?

It's used all over the place in Spring framework

  • Aspect Oriented Programming (AOP) is one of the two core concepts that the Spring framework is built on, the second one being Dependency Injection.

Let's Get To Know the agents behind Spring Aspects ??

Join point ?

  • A joinpoint is a candidate point in the Program Execution of the application where an aspect can be plugged in.

  • This point could be a method being called, an exception being thrown, or even a field being modified.

  • These are the points where your aspect’s code can be inserted into the normal flow of your application to add new behavior.

Advice ?

  • The code that addresses system wide concerns (logging, security checks, etc...) representing the action to perform at a joinpoint specified by a pointcut.

Pointcut ?

  • A pointcut is an expression that defines at what joinpoints, the associated Advice should be applied.

Aspect ?

  • Aspect is the class that defines the aspect’s behavior and a pointcut defining where the aspect should be executed.

Ready For Some Coding? ?

Let's write a simple use-case of logging something to the console on the call of a certain method.

We'll start by adding the maven dependency for AOP

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

Then, let's create a class and annotate it with @Aspect to mark that this class will contain Advice methods and Pointcuts.
We also need to annotate it with @Component so it becomes managed by Spring.

This class will handle the simple task of logging to the console.

@Component
@Aspect
public class LoggingAspect {
    @Pointcut("@annotation(Loggable)")
    public void logAllMethodCallsPointcut(){    
    }
    @Before("logAllMethodCallsPointcut()")
    public void logAllMethodCallsAdvice(){
        System.out.println("From Aspect");
    }
}

We need to create the loggable annotation

@Target(ElementType.METHOD)  
@Retention(RetentionPolicy.RUNTIME)  
public @interface Loggable {  
}

What did we just write? ?

  • @Pointcut("@annotation(Loggable)") declares the logAllMethodCallsPointcut method as a pointcut for all method annotated with Loggable.
  • @Before("logAllMethodCallsPointcut()") declares logAllMethodCallsAdvice advice which will be called before any method annotated with Loggable.

Now let's create a service that will act as our join point ?

@Service  
public class HomeService {  
  @Loggable
  // this here is what's called a join point  
  public void homePage(){  
        System.out.println("From Service");  
    }  
}

Now when we call the homePage method, the logAllMethodCallsAdvice advice method will be called before it.

So we will see the following logs ?

From Aspect
From Service

Remind Me Why Are We doing This? ?

This example seems useless for the simple case we have, however imagine having a system with hundreds of methods that you want to log something about them.

  • Would you call the same code before each and everyone? ?
  • What happens if you want to change the implementation of the logging process?
    • You need to search for all the usages and change them ?
  • Or, you can create a service and call it from all those places, but imagine having more things other than logging.
    • You need to create and call separate service for everyone ?

Aspects are a life saver as you can now see. ??

Conclusion ?

  • Aspects is one of the most important concepts of the Spring framework.
  • It's quite handy and provides a mean for clean reusable code.
  • It has a wide range of use-cases (We only discuss logging).

For more details you can check this rich resource ?.

Source code on GitHub ?.


This content originally appeared on DEV Community and was authored by Abdulcelil Cercenazi


Print Share Comment Cite Upload Translate Updates
APA

Abdulcelil Cercenazi | Sciencx (2021-08-14T15:48:21+00:00) Intro To Spring’s Aspects. Retrieved from https://www.scien.cx/2021/08/14/intro-to-springs-aspects/

MLA
" » Intro To Spring’s Aspects." Abdulcelil Cercenazi | Sciencx - Saturday August 14, 2021, https://www.scien.cx/2021/08/14/intro-to-springs-aspects/
HARVARD
Abdulcelil Cercenazi | Sciencx Saturday August 14, 2021 » Intro To Spring’s Aspects., viewed ,<https://www.scien.cx/2021/08/14/intro-to-springs-aspects/>
VANCOUVER
Abdulcelil Cercenazi | Sciencx - » Intro To Spring’s Aspects. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/14/intro-to-springs-aspects/
CHICAGO
" » Intro To Spring’s Aspects." Abdulcelil Cercenazi | Sciencx - Accessed . https://www.scien.cx/2021/08/14/intro-to-springs-aspects/
IEEE
" » Intro To Spring’s Aspects." Abdulcelil Cercenazi | Sciencx [Online]. Available: https://www.scien.cx/2021/08/14/intro-to-springs-aspects/. [Accessed: ]
rf:citation
» Intro To Spring’s Aspects | Abdulcelil Cercenazi | Sciencx | https://www.scien.cx/2021/08/14/intro-to-springs-aspects/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.