AOP with example
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:
- Web Layer for exposing the services using RESTFul web services.
- Business layer to handle business logic.
- 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.
- At the time of function exception
- Before method execution
- After method execution
- 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
- Lets create a project using Spring Initilizr using link
- Fill Group and Artifact detail as com.javadoubts and practice respectively.
- Add dependencies as Spring Web and Spring boot actuator as mentioned in below screenshot.
- 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
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.