[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

execution(*com.mjc.Spring5.dao.BookDao.add(...))

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

execution(com.mjc.Spring5.dao.BookDao.(...))

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

execution(com.mjc.Spring5.dao..*(...))

AOP AspectJ annotation configuration

1. Create classes and define methods

@Component
public class User {
    public void add(){
        System.out.println("add...");}}
//Enhance class and add annotation @ Aspect
@Component
@Aspect
public class UserProxy {
    public void before(){
        System.out.println("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-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

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

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

@After

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

@AfterReturning

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

@AfterThrowing

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

@Around

//Around Advice 
@Around(value = "execution(* com.mjc.spring5.aopanno.User.add())")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    System.out.println("before around....");
    proceedingJoinPoint.proceed();
    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(){
    System.out.println("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