Aspect Oriented Programming for Spring Boot project
AOP, Spring Boot
🙌 The Github repository for the application: https://github.com/ashishbania/OpenBankingDemo
This is a quick guidebook for anyone who wants to add AOP(Aspect Oriented Programming) to their project but doesn't know why AOP is added, what components are involved, and most importantly how to add aspects to their project. Below are the links for the longer and far more detailed route.
- https://www.geeksforgeeks.org/spring-boot-aopaspect-oriented-programming/
- https://www.baeldung.com/spring-aop
- https://docs.spring.io/spring-framework/docs/4.2.x/spring-framework-reference/html/aop.html
Background
There are multiple cross-cutting concerns in a project such as Logging, Security, Transaction Management et. al. Every time we write a new piece of code, the developer is expected to do all of the above for a well-functioning code. What happens when the code is already released and then there are newer needs which affect the code? Imagine adding new logger statements to gain more insight and new security features which were not conceptualized when the product was released. The simplest approach is to simply add a new sys out or logger wrapping the inspection points. This is simply a no-brainer.
What if no code change is required to the core logic but externally it can be added? That's the magic of AOP(Aspect Oriented Programming). For the ELI5 crowd, it's like adding a plugin to a core feature and then extending it with extra features. Exactly how you install new apps on top of the iOS to track your steps. The OS doesn't need to be changed. The new app extends that feature to you. This will be more clear as we delve deeper into the code. Between the above explanation and the following code, I am expecting to succinctly communicate the idea.
Why AOP is needed?
The crux of the need arises when any non-core business functionality changes, are added or removed from the project. Instead of constantly making changes to the core system, all the cross-cutting concerns are externalized to the aspect part. It's easier to control the modularized component as a new aspect totally.
Pros and Cons
- AOP enables the clustering of all cross-cutting concerns at a single system.
- Since the base program is not responsible for maintaining cross-cutting concerts, AOP reduces the redundancy and errors in the shared concerns.
- OOPs is used to expand and contract the base software whereas logging, transactions, security remains untouched.
- There is total encapsulation of cross cutting concerns. Developer is confident to not break these common concerns while making changes to the base code. It provides a solid foundation for future feature enhancements and extensions. Whereas without AOP, basic changes can lead to a slippery slope of code bugs and software quagmires.
- On the other hand, AOP adds extra layer of complexity to base code as the cross cutting concerns are externalized.
- Higher levels of complexity usually breeds into difficult to trace errors.
- Developer has to explore additional layer of aspect code and piece together the understanding.
What are the AOP annotations, and libraries needed?
To begin with, another separate class file is needed where @Aspect annotation is added to indicate the class file as an aspect. Further @Before, @After, and @AfterReturning advices can be added to extend the behavior to the service class in question at the pointcuts. The advice will be executed at the join points as per the declaration.
How AOP is finally added to the project?
Once the aspect is created, during the execution, Spring will inject the dependencies needed to add the aspects whenever needed.
Source Code
Controller
Service
ServiceImpl
Aspect Class
Let's execute, getAccountsByID, and find out how the statements are printed.
2022-10-26 17:26:29.144 INFO 26589 --- [ main] c.e.d.OpenBankingDemo.OpenBankingDemo : Started OpenBankingDemo in 10.923 seconds (JVM running for 12.384)
2022-10-26 17:26:35.368 INFO 26589 --- [nio-8081-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-10-26 17:26:35.368 INFO 26589 --- [nio-8081-exec-2] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-10-26 17:26:37.682 INFO 26589 --- [nio-8081-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 2314 ms
This is to be printed before the AccountServiceImpl will be called
This is to be printed afterReturning AccountServiceImpl got called
This is to be printed after the AccountServiceImpl got called
As you notice, we successfully added print statements from the aspect component. This was succinct introduction to AOP in Spring Boot.
Thank you for reading! If you enjoyed it, please clap 👏 for it.