A key component of the Spring framework is the Aspect Oriented Programming (AOP) framework. Aspect oriented programming needs to decompose program logic into different parts called so-called concerns. Functions that span multiple points of an application are called crosscutting concerns, which are conceptually independent of the application’s business logic. There are various common examples of good aspects, such as logging, auditing, declarative transactions, security, and caching.
In OOP, key unit modularity is a class, while in AOP, unit modularity is an aspect. Dependency injection helps you decouple application objects from each other and AOP helps you decouple crosscutting concerns from the objects they affect. AOP is like the trigger of programming language, such as Perl NET, Java or others.
The Spring AOP module provides interceptors to intercept an application. For example, when executing a method, you can add additional functions before or after the method execution.
AOP terminology
Before we begin to work with AOP, let’s familiarize ourselves with AOP concepts and terminology. These terms are not specific to Spring, but are related to AOP.
term | describe |
---|---|
Aspect | A module has a set of APIs that provide crosscutting requirements. For example, a log module will be called by the AOP aspect to record logs. An application can have any number of aspects, depending on the requirements. |
Join point | It represents a point in your application, and you can do it in terms of plug-in AOP. You can also say that it is in the actual application, and one of the operations will use the Spring AOP framework. |
Advice | This is the method of implementation before or after the actual action. This is the code that is actually called through the Spring AOP framework during program execution. |
Pointcut | This is a group of one or more connection points, and the notification should be executed. You can use expressions or patterns to specify pointcuts, as we will see in the AOP example. |
Introduction | References allow you to add new methods or properties to existing classes. |
Target object | The object notified by one or more aspects is always a proxy object. Also known as the notified object. |
Weaving | Weaving connects aspects to other application types or objects and creates a notified object. This can be done at compile time, class load time, and run time. |
Type of notification
Spring can use the following five notifications:
Advice | describe |
---|---|
Pre notification | Before a method is executed, the notification is executed. |
Post notification | After a method is executed, regardless of its outcome, execute the notification. |
Notification after return | After a method is executed, notifications can only be executed when the method is successfully completed. |
Notify after throwing an exception | After a method is executed, notifications can only be executed when the method exits and throws an exception. |
Surround notification | Execute notifications before and after suggesting method calls. |
Implement custom aspects
Spring supports @ AspectJ annotation style and pattern based methods to implement custom aspects. These two methods have been explained in detail in the following two subsections.
method | describe |
---|---|
XML Schema based | The aspect is implemented using regular classes and configuration based XML. |
@AspectJ based | @AspectJ references a declarative style as a regular Java class annotation with Java 5 annotations. |