English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Spring AOP

Aspect Oriented Programming(AOP) is a supplement to OOP in a sense because it also provides modular functions. However, the key unit of modularity is aspect rather than class.

AOP divides the program logic into different parts (referred to as focus points). It is used to Interdisciplinary focusto improve modularity.

cross-cutting concernsA concern that will affect the entire application should be as centralized as possible in the code at one location, such as transaction management, authentication, logging, security, etc.


Why use AOP?

It provides a pluggable way to dynamically add additional concerns before, after, or around actual logic. Suppose there is a class with10methods, as shown below:

class A{
public void m1(){...}
public void m2(){...}
public void m3(){...}
public void m4(){...}
public void m5(){...}
public void n1(){...}
public void n2(){...}
public void p1(){...}
public void p2(){...}
public void p3(){...}
}

This method, starting with m has5This method, starting with n has2This method, starting with p has3This method.

Understand the situationI have to maintain logs and send notifications

Problems without AOPWe can call methods starting with m (maintenance logs and sending notifications). In this case, we need to use all5This method writes code.

However, if the customer says later that they do not need to send notifications, then all methods need to be changed. This will cause maintenance issues.

AOP solutionWe do not have to call methods from methods. Now, we can define other concerns in the methods of the class, such as maintaining logs, sending notifications, etc. Its entries are given in the xml file.

In the future, if the customer says they want to remove the notification program feature, we only need to make changes in the xml file. Therefore, maintaining AOP is easy.

Where to use AOP?

AOP is mainly used in the following cases:

It provides declarative enterprise services, such as declarative transaction management. It allows users to implement custom aspects.

AOP concepts and terms

The concepts and terms of AOP are as follows:

Join point Advice Pointcut Introduction Target Object Aspect Interceptor AOP Proxy Weaving

Join point

Join point is any point in the program, such as method execution, exception handling, field access, etc. Spring only supports method execution Join point.

Advice

Advice represents the operation that the aspect takes at a specific Join point. There are different types of Advice:

Before Advice: It executes before the Join point. After Returning Advice: It executes after the joint point is completed normally. After Throwing Advice: It executes if the method exits due to an exception. After (finally) Advice: It will execute after the Join point, regardless of whether the Join point exits normally or through an exception. Around Advice: It executes before and after the Join point.

Pointcut

It is an expression language for AOP that matches with Join point.

Introduction

This means introducing other methods and fields of the type. It allows you to introduce new interfaces to any advised object.

Target Object

This is an object that contains one or more aspect advices. In Spring, it is also called a proxy object because Spring AOP is implemented using runtime proxies.

Aspect

This is a class that contains advice, join points, etc.

Interceptor

This is an aspect that contains only one advice.

AOP Proxy

It is used to implement the aspect contract created by the AOP framework. In the Spring framework, it will be the JDK dynamic proxy or CGLIB proxy.

Weaving

This is the process of linking aspects with other application types or objects to create advised objects. Weaving can be done at compile time, load time, or runtime. Spring AOP performs weaving at runtime.

AOP Implementation

AOP implementation is provided by the following:

AspectJ SpringAOP JBoss AOP

Spring AOP

Spring AOP can be used in the following three ways. However, the widely used method is the Spring AspectJ annotation style. The following is an example of using Spring AOP:3methods:

Using Spring1.2Old style (based on dtd) (in Spring)3is also supported in Chinese) Through AspectJ annotation style Through Spring XML configuration style (based on pattern)