[Spring AOP] term analysis + AspectJ annotation usage

AOP (terminology)

1. Connection point: the method that can be enhanced is called connection point

2. Pointcuts: the methods that are actually enhanced are called pointcuts

3. Notification (enhanced): the logical part that is actually enhanced.

There are several types of notifications:

l pre notification: notification before method execution

l post notification: notification after method execution

l surround notification: notification before and after method execution

l exception notification: notify when an exception occurs

l final notice: finally

4. Aspect: apply notifications to pointcuts

AOP preparation

1. Spring frameworks generally implement AOP operations based on AspectJ

AspectJ is not part of Spring. It is independent of the AOP framework and is generally used together for AOP operations

2. Implementation of AOP operation based on AspectJ

(1) Implementation of configuration file based on xml

(2) Annotation based implementation

3. Introduce AOP related dependencies into project engineering

[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-k5x9jops-1638706935261)( file:///C:/Users/Lenovo/AppData/Local/Temp/msohtmlclip1/01/clip_image004.jpg )]

4. Pointcut expression

execution([permission modifier] [return type] [class full path] [method name] ([parameter list]))

Example 1: enhance the add method in com.mjc.Spring5.dao.BookDao


Example 2: enhance all methods in com.mjc.Spring5.dao.BookDao


Example 3: enhance all methods of all classes in the com.mjc.Spring5.dao package


AOP AspectJ annotation configuration

1. Create classes and define methods

public class User {
    public void add(){
//Enhance class and add annotation @ Aspect
public class UserProxy {
    public void before(){

2. Turn on AspectJ to generate proxy objects. Look for labels marked with @ AspectJ to automatically generate proxy objects

<!--open AspectJ Generate proxy object-->

3. Configure different types of notifications

In the enhanced class, the annotation of the notification class is added on the method as notification, and the pointcut expression configuration is used


//Before annotation indicates pre notification
@Before(value = "execution(* com.mjc.spring5.aopanno.User.add())")
public void before(){


//Final notice, whether the method is wrong or not
@After(value = "execution(* com.mjc.spring5.aopanno.User.add())")
public void after(){


//Method is executed after it returns
@AfterReturning(value = "execution(* com.mjc.spring5.aopanno.User.add())")
public void afterReturning(){


//Exception notification, executed after an exception occurs
@AfterThrowing(value = "execution(* com.mjc.spring5.aopanno.User.add())")
public void afterThrowing(){


//Around Advice 
@Around(value = "execution(* com.mjc.spring5.aopanno.User.add())")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    System.out.println("before around....");
    System.out.println("after around....");

4. Common pointcut extraction

@Pointcut(value = "execution(* com.mjc.spring5.aopanno.User.add())")
public void pointdemo(){}
@Before(value = "pointdemo()")
public void before(){

5. A method has multiple enhancement classes and sets priorities

Add the annotation @ order (numeric type) on the enhanced class. The smaller the numeric type value, the higher the priority

Tags: Java Spring Back-end

Posted on Sun, 05 Dec 2021 08:00:03 -0500 by seanmayhew