Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

AOP for the Spring framework


May 14, 2021 Spring


Table of contents


AOP for the Spring framework

A key component of the Spring framework is the Aspect-Oriented Programming (AOP) framework. A spect-oriented programming requires breaking down program logic into different parts called so-called concerns. T he functionality of multiple points across an application is called cross-cutting concerns, which are conceptually independent of the application's business logic. There are a variety of common examples of good aspects, such as logging, auditing, declared transactions, security, and caching.

In OOP, the critical unit module degree is the class, while in AOP the cell module degree is the aspect. D ependency injection helps you decouple application objects from each other and AOP helps you decouple cross-cutting concerns from the objects they affect. AOP is a trigger like a programming language, such as Perl, .NET, Java, or something else.

The Spring AOP module provides interceptors to intercept an application, for example, when a method is executed, you can add additional functionality before or after the method executes.

AOP term

Before we get started with AOP, let's familiarize ourselves with AOP concepts and terminology. These terms are not specific to Spring, but are related to AOP.

item Describe
Aspect A module has a set of APIs that provide cross-cutting requirements. F or example, a log module will be called by the AOP side in order to log. Applications can have any number of aspects, depending on the requirements.
Join point In your application it represents a point that you can do with plug-in AOP. You can also say that it is in a real application, and one of the operations will use the Spring AOP framework.
Advice This is the method of implementation before or after actual action. This is the code that is actually called through the Spring AOP framework during program execution.
Pointcut This is a set of one or more connection points, and notifications should be executed. You can specify the entry point using an expression or pattern, as we'll see in the case of AOP.
Introduction References allow you to add new methods or properties to an existing class.
Target object An object that is notified by one or more aspects, and is always a proxy object. Also known as a notified object.
Weaving Weaving connects aspects to other application types or objects and creates an object that is notified. These can be done at compile time, class load time, and runtime.

The type of notification

Spring can work with the five notifications mentioned below:

Notice Describe
Pre-notifications The notification is executed before a method is executed.
Post-notification After a method is executed, the notification is executed without taking into account its results.
Notify when you return After a method is executed, notifications can only be executed if the method completes successfully.
Notification after an exception is thrown After a method executes, the notification can only be executed if the method exit throws an exception.
Wrap around notifications The notification is executed before and after the proposed method call.

Implement custom aspects

Spring supports @AspectJ customization aspects using the annotation style approach and the pattern-based approach. Both methods are explained in detail in the following two subse sections.

Method Describe
XML Schema based Aspects are implemented using general classes and configuration-based XML.
@AspectJ based @AspectJ refers to a declaration-side style as a regular Java class comment with Java 5 comments.