今天看啥  ›  专栏  ›  牛哄哄的柯南

Spring基于AspectJ实现AOP操作

牛哄哄的柯南  · CSDN  ·  · 2021-01-19 12:15

Spring 框架一般都是基于 AspectJ 实现 AOP 操作。
需要注意的是 :AspectJ 不是 Spring 组成部分,独立 AOP 框架,一般把 AspectJ 和 Spirng 框架一起使用,进行 AOP 操作。

基于 AspectJ 实现 AOP 操作有两种方式:
(1)基于 xml 配置文件实现
(2)基于注解方式实现(普遍使用)

准备工作

在项目工程里面引入 AOP 相关依赖。

需要以下这些依赖:
在这里插入图片描述

学会使用切入点表达式

切入点表达式作用(通俗的讲):知道对哪个类里面的哪个方法进行增强。
语法结构: execution([权限修饰符] [返回类型] [类全路径] [方法名称] ([参数列表]) )

例 1:对 com.atguigu.dao.BookDao 类里面的 add 进行增强
execution( * com.atguigu.dao.BookDao.add(…))

例 2:对 com.atguigu.dao.BookDao 类里面的所有的方法进行增强
execution( * com.atguigu.dao.BookDao. * (…))

例 3:对 com.atguigu.dao 包里面所有类,类里面所有方法进行增强
execution( * com.atguigu.dao. * . * (…))

AOP 操作(AspectJ 注解)

1、创建类,在类里面定义方法

User类:

package com.Keafmd.spring5.aopanno;

import org.springframework.stereotype.Component;

/**
 * Keafmd
 *
 * @ClassName: User
 * @Description: 基于注解  被增强的类
 * @author: 牛哄哄的柯南
 * @date: 2021-01-18 13:54
 */
@Component
public class User {

    public void add(){
//        int i = 10/0;
        System.out.println("add....");
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

2、创建增强类(编写增强逻辑),在增强类里面,创建方法,让不同方法代表不同通知类型

UserProxy 类:

package com.Keafmd.spring5.aopanno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * Keafmd
 *
 * @ClassName: UserPtoxy
 * @Description: 增强类
 * @author: 牛哄哄的柯南
 * @date: 2021-01-18 13:56
 */
@Component
@Aspect //生成代理对象
@Order(3)
public class UserProxy {


    //相同的切入点抽取
    @Pointcut(value = "execution(* com.Keafmd.spring5.aopanno.User.add(..))")
    public void pointdemo(){

    }

    //前置通知
    //@Before注解就表示前置通知
    @Before(value = "pointdemo()")
    public void beafor(){
        System.out.println("before...");
    }

    //最终通知
    @After(value = "execution(* com.Keafmd.spring5.aopanno.User.add(..))")
    public void after(){
        System.out.println("after...");
    }

    //后置通知(返回通知)
    @AfterReturning(value = "execution(* com.Keafmd.spring5.aopanno.User.add(..))")
    public void afterReturning(){
        System.out.println("afterReturning...");
    }

    //异常执行
    @AfterThrowing(value = "execution(* com.Keafmd.spring5.aopanno.User.add(..))")
    public void afterThrowing(){
        System.out.println("afterThrowing...");
    }

    //环绕通知
    @Around(value = "execution(* com.Keafmd.spring5.aopanno.User.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("around-before...");

        //被增强的方法执行
        proceedingJoinPoint.proceed();

        System.out.println("around-after...");
    }



}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

3、进行通知的配置

(1)在 spring 配置文件中,开启注解扫描
(2)使用注解创建 User 和 UserProxy 对象
(3)在增强类上面添加注解 @Aspect
(4)在 spring 配置文件中开启生成代理对象

bean2.xml:

<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">


    <!--开启注解扫描-->
    <context:component-scan base-package="com.Keafmd.spring5.aopanno"></context:component-scan>

    <!--开启AspectJ生成代理对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>


</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

4、配置不同类型的通知

在增强类的里面,在作为通知方法上面添加通知类型注解,使用切入点表达式配置。

UserProxy 类:

package com.Keafmd.spring5.aopanno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * Keafmd
 *
 * @ClassName: UserPtoxy
 * @Description: 增强类
 * @author: 牛哄哄的柯南
 * @date: 2021-01-18 13:56
 */
@Component
@Aspect //生成代理对象
@Order(3)
public class UserProxy {


    //相同的切入点抽取
    @Pointcut(value = "execution(* com.Keafmd.spring5.aopanno.User.add(..))")
    public void pointdemo(){

    }

    //前置通知
    //@Before注解就表示前置通知
    @Before(value = "pointdemo()")
    public void beafor(){
        System.out.println("before...");
    }

    //最终通知
    @After(value = "execution(* com.Keafmd.spring5.aopanno.User.add(..))")
    public void after(){
        System.out.println("after...");
    }

    //后置通知(返回通知)
    @AfterReturning(value = "execution(* com.Keafmd.spring5.aopanno.User.add(..))")
    public void afterReturning(){
        System.out.println("afterReturning...");
    }

    //异常执行
    @AfterThrowing(value = "execution(* com.Keafmd.spring5.aopanno.User.add(..))")
    public void afterThrowing(){
        System.out.println("afterThrowing...");
    }

    //环绕通知
    @Around(value = "execution(* com.Keafmd.spring5.aopanno.User.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("around-before...");

        //被增强的方法执行
        proceedingJoinPoint.proceed();

        System.out.println("around-after...");
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

相同的切入点抽取

写个方法然后调用这样就可以把相同接入点抽取出来了。

//相同的切入点抽取
    @Pointcut(value = "execution(* com.Keafmd.spring5.aopanno.User.add(..))")
    public void pointdemo(){

    }

    //前置通知
    //@Before注解就表示前置通知
    @Before(value = "pointdemo()")
    public void beafor(){
        System.out.println("before...");
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

有多个增强类多同一个方法进行增强,设置增强类优先级

在增强类上面添加注解 @Order(数字类型值),数字类型值越小优先级越高。

@Component
@Aspect //生成代理对象
@Order(3)
public class UserProxy {
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

PersonProxy类:

package com.Keafmd.spring5.aopanno;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * Keafmd
 *
 * @ClassName: PersonProxy
 * @Description:  第二个增强类
 * @author: 牛哄哄的柯南
 * @date: 2021-01-18 14:39
 */
@Component
@Aspect
@Order(1) //越小优先级越高
public class PersonProxy {

    //前置通知
    //@Before注解就表示前置通知
    @Before(value = "execution(* com.Keafmd.spring5.aopanno.User.add(..))")
    public void beafor(){
        System.out.println("Person before...");
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

通过@Order设置后PersonProxy 的前置通知就会比UserProxy 的前置通知先执行。

完全使用注解开发

创建配置类,这样就不需要使用 xml 配置文件了。

ConfigAop类:

package com.Keafmd.spring5.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * Keafmd
 *
 * @ClassName: ConfigAop
 * @Description: 配置类,完全注解,替代bean2.xml
 * @author: 牛哄哄的柯南
 * @date: 2021-01-18 15:12
 */
@Configuration
@ComponentScan(basePackages = {"com.Keafmd"})  //开启注解扫描
@EnableAspectJAutoProxy(proxyTargetClass = true)  //开启AspectJ生成代理对象
public class ConfigAop {
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

代码结构

在这里插入图片描述

测试代码

测试代码TestAop:

package com.Keafmd.spring5.test;

import com.Keafmd.spring5.aopanno.User;
import com.Keafmd.spring5.config.ConfigAop;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Keafmd
 *
 * @ClassName: TestAop
 * @Description:
 * @author: 牛哄哄的柯南
 * @date: 2021-01-18 14:15
 */
public class TestAop {

    @Test
    public void testAOPnno(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
        User user = context.getBean("user",User.class);
        user.add();
    }

    //完全注解
    @Test
    public void testAOPnno2(){
        ApplicationContext context = new AnnotationConfigApplicationContext(ConfigAop.class);
        User user = context.getBean("user",User.class);
        user.add();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

输出结果:

Person before...
around-before...
before...
add....
around-after...
after...
afterReturning...

Process finished with exit code 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

testAOPnno和testAOPnno2的结果是相同的testAOPnno2采用是的完全注解开发,代替了xml配置文件。通过运行结果我们可以很轻清楚地看到不同类型通知的执行顺序,以及增强类的优先级。

以上就是基于AspectJ实现AOP操作的全部内容。

看完如果对你有帮助,感谢点赞支持!
如果你是电脑端的话,看到右下角的 “ 一键三连 ” 了吗,没错点它[哈哈]

在这里插入图片描述

加油!

共同努力!

Keafmd




原文地址:访问原文地址
快照地址: 访问文章快照