Spring Boot AOP with example

Imran Khan
4 min readNov 15, 2022

Aspect Oriented Programming (AOP) is one of the key feature of Spring framework. AOP breaks down complete code into multiple modules.

Spring boot application mainly divided in to three layers:

  1. Web Layer for exposing the services using RESTFul web services.
  2. Business layer to handle business logic.
  3. Data Layer for data persistence logic.

Each layer is having different responsibility and there are some common aspects which gets apply to all layers. e.g. Logging, Security, Validation, etc. Common aspects are also called as cross-cutting concerns.

Below is the pom.xml dependency to support Spring AOP

AOP is similar to OOPS concept where it breaks code into multiple reusable modules.

AOP provides the capability to dynamically add module at runtime.

Cross-cutting concerns are parts of a program that rely on or must affect many other parts of the system. This is like injecting complete module(logging, cahcing, etc ) in Spring framework dynamically.

Logging, caching, security, monitoring, etc. are some of the examples cross cutting concern from AOP. At any point of time there modules can be added dynamically.

Spring AOP has interceptors which can intercept application and its methods. This is to perform some extra action at the time of property initiation, method initialization or destroy.

AOP terminologies

Below are some of the important terminologies to understand more about AOP.

Aspect: Aspect is a concern which is require to implement in Spring framework or application. An application can have multiple aspects such as logging, security, monitoring.

Join Point: The is a point where concern or Aspect gets plugin.

Advice: It is an actual implementation of aspect. Advice is to suggest at what moment we need to execute code, either before or after method execution.

Advice can be execute at below point of time.

  1. At the time of function exception
  2. Before method execution
  3. After method execution
  4. Method throws as exception

Pointcut: Join points where an advice should be executed. Single aspect can have multiple pointcut to define where it needs to be executed.

Introduction: Allows you to add new methods or attributes to the existing classes.

Target object: This is an application object on which the advice will be applied.

Weaving: It is a technique to make a connection in between aspects at load time, compile time or runtime.

Let’s create sample project to understand the Spring boot aspect oriented programming.

Example

  1. Lets create a project using Spring Initilizr using link
  2. Fill Group and Artifact detail as com.javadoubts and practice respectively.
  3. Add dependencies as Spring Web and Spring boot actuator as mentioned in below screenshot.
  4. information as mentioned in below screenshot. Add dependencies as Spring Web and Spring boot actuator.

3. Unzip the downloaded file and import in Spring Tool Suite. add below below dependencies as part of pom.xml

4. Create UserController.java with below content:

@RestController annotation used to created RESTful web services which can be accessed using http methods like GET, POST, etc. and it is the combination of @Controller and @ResponseBody.

@GetMapping used to map HTTP GET request. It mainly used to fetch/get data from server.

5. Create one more UserServiceAspect.java class. Declare @Aspect and @Component annotation on top of the class.

value=”execution(* com.javadoubts.practice..*(..))”
Above syntax will make sure the advice will get call for every class method inside com.javadoubts.practice package.

@Aspect component is to declare this class as an Aspect.

@Before advice will get call before every function execution.

@After advice will get call after every function execution.

@AfterReturning advice will run as soon as function returns its value.

@Around gets executes before and after joint point.

joinPoint.proceed() is important to call subsequent advice.

6. To consume AOP in Spring boot application it is necessary to declare below highlighted annotation on top of main class as shown below:

6. Right click inside PracticeApplication.java file and Run it as Spring Boot App.

7. Hit below URL and check for console to see the function calling sequence

http://localhost:8080/users

I hope you found out this article interesting and informative. Please share it with your friends to spread the knowledge.

You can follow me for upcoming blogs follow.
Thank you!

--

--