A complete Guide to Aspect Oriented Programming (AOP) in Spring Framework.
This is a compete guide to Aspect Oriented Programming. Aspect Oriented Programming (AOP) is not a competitor or a replacement to Object Oriented Programming (OOP) but it enhances and compliments OOP. The concept of OOP is based on objects, while the concepts of AOP are based on Aspects. Both classes and aspects bring modularity and divide responsibility by modularising the structure.
In this guide, we will emphasis on acquiring some basic understanding of the Aspect Oriented Programming. It begins by knowing why AOP framework is important and what problems it solves. Then we will get some basic and conceptual understanding of AOP framework and some of the most common AOP terminologies. Next we will learn a brief about Spring AOP support and various Advices supported by Spring AOP. Finally we will jump to the examples and tutorials demonstrating various aspects of Spring AOP.
Why Object Oriented Programming (AOP)?
One of the principles of OOP states that, each class should have a Single Responsibility. Thus we create multiple classes to collectively process a task, where each class is responsible for taking care of a particular functionality. However, there are some concerns, that breaks these principle and AOP helps us solving these problems.
Single Responsibility Principle
In a good OOP application, we try to follow the SOLID principles, including Single Responsibility. However, in a practical application, we can follow the Single Responsibility Principle up to an extent, but not completely. That is because, we cannot have classes doing one and only one thing. Reason for that is the secondary concerns the classes have to handle. Such secondary concerns may contain logging, transaction management, or exception handling.
Cross Cutting Concerns
Another problem we face is the cross cutting concerns. Cross cutting concerns are the secondary concerns which are applicable across multiple classes. In other words, it is a type of secondary concerns which all classes need to execute. Thus, when we test the classes for their primary functionality, we also need to test them for the secondary concerns.
Repeating the Cross Cutting concerns across the classes also brings in redundancy. When at an application level we decide to change the the cross cutting concerns, we need to change them in multiple classes. For example, each class catches exceptions generated by its own functionality and throws application specific runtime exception. When we change the runtime exception, we need to go and change class such classes.
What is Aspect Oriented Programming (AOP)?
The Aspect Oriented Programming is a programming paradigm that bring in modularity by centralising cross cutting concerns. It helps imposing additional behaviours on classes without modifying classes. Also, it provides means of dynamically selecting classes, to which the additional behaviour is applied.
The Aspect Oriented Programming helps the classes concentrate on their core responsibilities, while the AOP components taking care of the secondary concerns. Using AOP we can centralise the cross cutting concerns, in an Aspect, instead of repeating them across different classes.
An aspect is a common place where such cross cutting concerns are placed. An aspect segregates such cutting concerns into advices. The advices also defines Pointcut, which is an expression or a predicate that is used to select classes, on which the advice needs to be applied. For now, the AOP terms used here may sound unfamiliar, but in the next section we will understand their meanings.
In this section we will focus on different components and concepts in Aspect Oriented Programming.
An aspect is a concern that is applicable to multiple classes. Aspect helps to segregate such concerns into a dedicated class, that is annotated with @Aspect.
A Join Point is a point during an execution of a program. Although, a Join Point can be any point during the execution flow, Spring AOP supports only a method execution as a Join Point.
Advice is the action taken by the Aspect when a particular Join Point is reached. Advices comes in different types such as around method execution, before method execution or after a method execution. Spring AOP applies the defined advices in the form of intercepters. Also, if there are multiple advices, Spring will create a chain of interceptors.
Advices define a Pointcut, which is a predicate or an expression that decides the Join Points for the advices. Spring AOP prepares all the Join Points that matches the Pointcut expression and apply the advice on each of them.
This is the object on which an Aspect applies an advice. We can also call it Advised Object.
For each of the target objects, Spring AOP creates proxy classes. Thus, when we auto wire a Target Object, spring actually injects the proxy for that object. On a method execution, the proxy passes the method call to the actual object via going through chain of advices.
In this section we have learned all important AOP terminologies. If they still sound confusing, we suggest you to leave it for now. Once you are well versed on AOP and practice writing Aspects and Advices, you can comeback and re-read. They will surely sound familiar then.
Introduction to Spring AOP
Although, Aspect Oriented Programming is a generic concept, Spring provides an excellent support for it. The Spring AOP support goes well with Spring IoC container.
The AOP support in Spring, is not as perfect as some of the other AOP frameworks. However, Spring claims that they covers all the important aspects of AOP, whiling keeping the framework simple. The Spring AOP emphasises more on keeping the framework simple and work well with the Spring IoC container than providing a perfect AOP framework which is requires additional implementations. Spring doesn’t enforce you to use or implement an AOP related components but we can add if we want to use Spring AOP feature.
The Spring AOP uses AspectJ internally and builds the framework based on the IoC and Dependency Injection paradigm. Some of the most common Spring features like, declarative transaction management are based on the Spring AOP feature. Spring AOP let’s use use annotations to declare an Aspect or an advice.
At the base of Spring AOP framework is the concept of dynamic proxies. Spring AOP creates dynamic proxy implementations for the target methods (or Join Point). These proxies are standard JDK dynamic proxies generated from the interface that the target class implements. If a target class does not implement any interface, then Spring uses CGLIB to generate proxies from the target class it self. When an AOP enabled target class gets auto wired, Spring IoC container actually injects a dynamic proxy for that class.
Next, we will see different types of Advices supported by Spring AOP.
Types of Advices in Spring AOP
This section lists down all the different types of advices that Spring AOP supports. As stated above, Spring AOP supports method execution as a Join Point. Thus, the advices are bound to the method executions.
A Before advice runs before a method execution. The advice cannot stop the target method execution, except when the advice throws an exception.
After Returning Advice
As the name tells, an After advice runs after the execution of the target method is finished. Note that, this advice won’t run if the target join poin finishes because of an execution.
An Around advice surrounds a join point execution. Which means first the advice is executed and that target method is executed from within the advice. Because of that, the execution flows from advice to the target method and comes back to the advice when the target method is finished. The Around Advice has the control to stop the target method execution.
An After advice runs after the join point, even of the Join point results into an exception. Thus, this advice will always execute even if the target method returns successfully, or ends by throwing exception.
An After Throwing advice runs only when a join point ends by throwing exception. It won’t run in the scenarios when the target method returns successfully.
Spring AOP Examples and Tutorials
This guide provided a detailed Introduction to Spring Aspect Oriented Programming (AOP). We began by getting the conceptual understanding of the AOP concept and learned why it is important. Then, we focused on various features of the AOP and understood how Spring AOP supports the aspect oriented programming. Also, we learned various concepts and terminologies used in the AOP. Finally, we learned a different types of advices that are supported by Spring.
For more focused practical oriented learning of Spring AOP, go through the individual tutorials that are linked in the previous section.
For more on Spring and Spring Boot, please visit Spring Tutorials.