Annotation configuration of Spring AOP

Annotation configuration of Spring AOP

(1) Brief description

Last time we talked about the implementation principle of spring AOP, we found that actually implementing this function through our own programming is still very cumbersome. Spring therefore provides us with a corresponding configuration method, which helps us simplify the development process, so that our attention can be fully focused on business logic.

Before understanding the annotation configuration of AOP, we must first pay attention to some professional terms in AOP:

Joinpoint(连接点):所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点。
Pointcut(切入点):所谓切入点是指我们要对哪些Joinpoint进行拦截的定义。
Advice(通知/增强):所谓通知是指拦截到Joinpoint之后所要做的事情就是通知,通知分为:前置通知,后置通知,异常通知,最终通知,环绕通知。
Introduction(引介):引介是一种特殊的通知在不修改类代码的前提下,Introduction可以在运行期为类动态地添加一些方法或Field。
Target(目标对象):代理的目标对象。
Weaving(织入):是指把增强应用到目标对象来创建新的代理对象的过程。spring采用动态代理织入,而AspectJ采用编译期织入和类装载期织入。
Proxy(代理):一个类被AOP织入增强后,就产生一个代理类。
Aspect(切面): 是切入点和通知的结合。

Let's take a look at these concepts, we only need to remember a few points. The first is the concept of aspect, which is the A (Aspect) in AOP. It is a combination of entry point and notification. In layman's terms, it is the additional function of the program and the position of this function. The point of entry is the position of the notification of the entry, and the connection point is the position that can be used as the entry point (but not necessarily be cut in). Notice, a better understanding should call him enhancement, which is the function we want to add (such as the function of writing logs), which is the core of aspect-oriented programming. Let's sort out the key points we just talked about, connection points, entry points, notifications, and aspects.

Insert picture description here

In this article, we will talk about the annotation configuration method based on @Aspect instead of the traditional XML configuration method. Because the XML configuration is too cumbersome and difficult to understand, the annotation configuration method is more easy to understand. However, the premise of understanding is to fully understand the above concepts, because this will be closely related to the choice of annotations later.

(2) Simple examples

Let's take a look at a simple example to study how AOP should be configured. First of all, we need to import dependencies, we have to add dependencies in the pom.xml file:

<dependency>
	<groupId>org.aspectj</groupId>
  	<artifactId>com.springsource.org.aspectj.weaver</artifactId>
  	<version>1.6.4.RELEASE</version>
</dependency>

This is the dependency needed to use the @Aspect annotation. Of course, some spring framework dependencies are also needed. I won't write them here.

Then you have to write the configuration file, here is mainly to add the AOP namespace and constraints, and open the @AspectJ annotation configuration method:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bean="http://www.springframework.org/schema/context"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd"
       xmlns:aop="http://www.springframework.org/schema/aop">

    <aop:aspectj-autoproxy/>
    <context:component-scan base-package="com.test"/>
</beans>

Write the core method, which is the method we will be cut into:

package com.test;

import org.springframework.stereotype.Component;

@Component("Hello")
public class Hello {
    
    
    void sayHello(String s){
    
    
        System.out.println("hello " + s);
    }
}

Then write the aspect, which is the most critical part. As mentioned earlier, the aspect is divided into two parts, one is the entry point and the other is the notification. First, we need to add @Aspect annotations to the aspect class to indicate that this is an aspect (note: only Bean objects that have been included in the IOC container can be annotated with @Aspect). Then we write our notification method, that is, the additional functions we need, in this case, the before and after methods. A method alone is definitely not enough. We need to add an annotation to the method to let spring know that this is a notification. There are five notes describing the notification type:

Insert picture description here
These five annotations correspond to five notification methods: pre-notification, post-notification, final notification, exception notification, and surround notification. These are actually the timings for notifications, so everyone can try to find out what they do. The timing of the notification is not enough, and the specific entry point (point of entry) is also required. This is the role of the string in the annotation. The technical term for this string is called indicator. Let’s take a look at how many types of indicators there are:

Insert picture description here

The most commonly used indicator is the execution() indicator, which represents the execution of the method. The parameters of the indicator must be filled in with the full class name + method name to determine a method, and return values ​​and parameters can also be added if necessary. What we fill in this case is the full class name + method name, so that we can uniquely locate a method in a project. This also clearly shows a principle: only methods can be used as entry points, in other words all methods are connection points that can be entered.

package com.test;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component("Logger")
public class Logger {
    
    
    @Before("execution(* com.test.Hello.sayHello(..))")
    void before(){
    
    
        System.out.println("before hello");
    }

    @After("execution(* com.test.Hello.sayHello(..))")
    void after(){
    
    
        System.out.println("after hello");
    }
}

In this way, we can summarize the formula of AOP configuration: XX notification (one of the five notification types, configured with annotations) cuts into the target method at XX (the string in the notification type annotation), and executes the XX function (notification method, arrogant). Try to correspond with the above code to see if this is the case.

July 30, 2020

Guess you like

Origin blog.csdn.net/weixin_43907422/article/details/107655195