How to work with Spring AOP and AspectJ, Aspect, Advice, Pointcut, JoinPoint, Annotation | Spring Boot

Aspect-Oriented Programming (AOP) helps us cut across many methods, objects and type within the application without embedding it in the business logic. With Spring AOP and AspectJ, you can add additional behaviour to existing code without modifying the code itself.

The tutorial guides you how to work with Spring AOP and AspectJ using Spring Boot.

I. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.4.0.RELEASE

II. Overview
1. Goal

To build an application that uses Aspect on 4 services:
CustmerAspect on CustomerService: with simple expression
CustmerAspectPointcut on CustomerAnotherService: with @Pointcut Annotation and implement Aspect on its own funtions
CustmerAdvanceAspect on CustomerAdvanceService: with @Around – most powerful kind of advice, and more usages of JoinPoint for getting argument information
CustmerAspectAnnotation on CustomerLoggableService: implement Aspect by creating custom annotation @Loggable

2. Project Structure

springaop-aspectj-project-structure

3. Step to do

– Create Spring Boot project & add Dependencies
– Create a DataModel class
– Create Services
– Create Aspect Classes and Inferface
– Create Spring Bean Configuration File
– Create a Web Controller
– Run Spring Boot Application & Enjoy Result

4. Demo Video

III. Practice
1. Create Spring Boot project & add Dependencies

– Open Spring Tool Suite, on Menu, choose File -> New -> Spring Starter Project, then fill each fields:
springaop-aspectj-startproject

Click Next, in Web: choose Web:
springaop-aspectj-configweb

Click Finish. Spring Boot project will be created successfully.

– Open pom.xml, add dependencies for Spring Context, Spring AOP and AspectJ:

2. Create a DataModel class

Under package model, create class Customer.
Content of Customer.java:

3. Create Services

All Service Classes will be created under package service.

CustomerService.java:

Content of CustomerAnotherService.java and CustomerAdvanceService.java are the same as CustomerService.java.

CustomerAnotherService.java:

CustomerAdvanceService.java:

Content of CustomerLoggableService.java is similar to CustomerService.java, but it has annotation @Loggable for method getCustomerById:

4. Create Aspect Classes and Interface

All Aspect Classes will be created under package aspect

CustomerAspect.java:

In class above, we use an AOP Advice Types: Before Advice, there are some types:
Before Advice: runs before the execution of join point methods. Annotation: @Before.
After Advice: runs after the join point method finishes executing. Annotation: @After.
After Returning Advice: runs when the method execution returns normally. Annotation: @AfterReturning.
After Throwing Advice: runs when the method execution exits by throwing an exception. Annotation: @AfterThrowing.
Around Advice: can run both before and after the method executes, and to specify when, how and what the method actually gets to execute. Annotation: @Around.

execution is used for matching method execution join points.

CustomerAspectPointcut.java:

If we want to use Pointcut expression at many places (in Aspect Class) or implement Aspect on its own funtions, separate Pointcut and Advice Type, we should create an empty method with annotation @Pointcut, then consider it as expression in advice.

within (a Pointcut Designator) is used to limit execution within certain types.
We also have other Pointcut Designator:
this limits execution of methods where the bean reference (Spring AOP proxy) is an instance of the given type.
target limits execution of methods where the target object (application object being proxied) is an instance of the given type.
args limits the execution of methods where the arguments are instances of the given types.

CustomerAspectJoinPoint.java:

Calling proceed() executes the joinpoint method. We can get return value of the method by catching the return value of method proceed().

CustomerAspectAnnotation.java:

Another approach for Aspect is creating a custom annotation and annotate the methods. That’s why we annotate method getCustomerById() of CustomerLoggableService with @Loggable.

Loggable.java:

5. Create Spring Bean Configuration File

Under src/main/resources, create bean.xml:

6. Create a Web Controller

Under package controller, create class WebController.

7. Run Spring Boot Application & Enjoy Result

– Config maven build:
clean install
– Run project with mode Spring Boot App
– Check results:

Request 1
http://localhost:8080/aspectnormal
springaop-aspectj-req1
System shows:

Request 2
http://localhost:8080/aspectpointcut
springaop-aspectj-req2
System shows:

Request 3
http://localhost:8080/aspectjoinpoint
springaop-aspectj-req3
System shows:

Request 4
http://localhost:8080/aspectloggable
springaop-aspectj-req4
System shows:

IV. Source Code

SpringAOP-AspectJ


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*