(5) AOP(面向切面编程),AspectJ

1 AOP概述

  1. AOP(Aspect-Oriented Programming,面向切面编程):是一种新的方法论,是对传统 OOP(Object-Oriented Programming,面向对象编程)的补充。是一种通过动态代理实现程序功能扩展和统一维护的一种技术。
  2. 利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
  3. AOP编程操作的主要对象是切面(aspect),而切面用于模块化横切关注点(公共功能)。
  4. AOP的好处:
    1. 每个事物逻辑位于一个位置,代码不分散,便于维护和升级
    2. 业务模块更简洁,只包含核心业务代码

2  AOP术语

  • 横切关注点

从每个方法中抽取出来的同一类非核心业务。

  • 切面(Aspect)

封装横切关注点信息的类,每个关注点体现为一个通知方法。

  • 通知(Advice)

切面必须要完成的各个具体工作

  • 目标(Target)

被通知的对象

  • 代理(Proxy)

向目标对象应用通知之后创建的代理对象

  • 连接点(Joinpoint)

横切关注点在程序代码中的具体位置

  • 切入点(pointcut)

定位连接点的方式。每个类的方法中都包含多个连接点,所以连接点是类中客观存在的事物。如果把连接点看作数据库中的记录,那么切入点就是查询条件——AOP可以通过切入点定位到特定的连接点。切点通过org.springframework.aop.Pointcut 接口进行描述,它使用类和方法作为连接点的查询条件。

3  AspectJ

     1.简介

Java社区里最完整最流行的AOP框架。

在Spring2.0以上版本中,可以使用基于AspectJ注解或基于XML配置的AOP。

     2.@Aspect注解

在Spring中声明切面使用@Aspect注解,而且切面也需要交给IOC容器管理,即切面上也需要添加@Component注解。

当在Spring IOC容器中初始化AspectJ切面之后,Spring IOC容器就会为那些与 AspectJ切面相匹配的bean创建代理。

      在AspectJ注解中,切面只是一个带有@Aspect注解的Java类,它往往要包含很多通知,通知是标注有某种注解的简单的Java方法。

  • AspectJ支持5种类型的通知注解:
    1. @Before:前置通知,在方法执行之前执行
    2. @After:后置通知,在方法执行之后执行
    3. @AfterRunning:返回通知,在方法返回结果之后执行
    4. @AfterThrowing:异常通知,在方法抛出异常之后执行
    5. @Around:环绕通知,围绕着方法执行,相当于动态代理的全过程

 4  在Spring中使用AOP的步骤:

1. 在Spring核心包的基础上添加以下jar包

        spring-aspects-5.3.9.jar

        com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

2. 创建Spring的配置文件beans-aop.xml,配置自动扫描的包和AspectJ注解支持

<?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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置自动扫描的包-->
    <context:component-scan base-package="com.spring.aop"></context:component-scan>
<!--    开启AspectJ注解支持Spring回去自动帮我们创建代理对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

 3. 在切面中添加通知方法

package com.spring.aop;

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

@Component//在类上添加@Component注解将该类交给IOC容器管理
@Aspect//在类上添加@Aspect注解将该类标识为切面
public class LoggingAspect {

    //前置通知,在方法执行之前执行
    @Before(value = "execution(public int com.spring.aop.Calculator.*(int , int ))")
    public void beforeAdvice() {
        System.out.println("在方法执行之前执行!");
    }
}

Calculator类见上篇博客: 使用JDK动态代理实现日志的打印_qq_43555873的博客-CSDN博客动态代理Aophttps://blog.csdn.net/qq_43555873/article/details/123601697?spm=1001.2014.3001.5501

 4.测试

package com.spring.test;

import com.spring.aop.Calculator;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AOPTest {

    ApplicationContext ioc = new ClassPathXmlApplicationContext("beans-aop.xml");

    /*
       测试AOP
    */
    @Test
    public void testAOP(){
        Calculator calculator = (Calculator) ioc.getBean("calculator");
        calculator.add(10,2);
        calculator.sub(10,2);
        calculator.mul(10,2);
        calculator.div(10,2);
    }
}

5 AOP相关细节

1 切入点表达式

        1.作用

通过表达式的方式定位一个或多个具体的连接点

        2.语法格式:

execution([权限修饰符] [返回值类型] [简单类名/全类名] [方法名]([参数列表]))

        3. 举例说明:

表达式

execution(*com.spring.aop.Calculator.*(..))

含义

Calculator接口中声明的所有方法。

第一个“*”代表任意修饰符及任意返回值。

第二个“*”代表任意方法。

“..”匹配任意数量、任意类型的参数。

若目标类、接口与该切面类在同一个包中可以省略包名。

表达式

execution(public * Calculator.*(..))

含义

Calculator接口中声明的所有公有方法

表达式

execution(public int Calculator.*(..))

含义

Calculator接口中声明的返回值为int类型的公有方法

表达式

execution(public int Calculator.*(int, ..))

含义

第一个参数为int类型的共有方法。

“..” 匹配任意数量、任意类型的参数。

表达式

execution(public int Calculator.*(int, int))

含义

参数类型为int,int类型的方法

 4 在AspectJ中,切入点表达式可以通过 “&&”、“||”、“!”等操作符结合起来

表达式

execution (* *.add(int,..)) || execution(* *.sub(int,..))

含义

任意类中第一个参数为int类型的add方法或sub方法

表达式

!execution (* *.add(int,..))

含义

匹配不是任意类中第一个参数为int类型的add方法

  5  通知细节

  1. 概述
    1. 通知即在具体的连接点上要执行的操作。
    2. 一个切面可以包括一个或者多个通知。
    3. 通知所使用的注解的值往往是切入点表达式。
  2. 前置通知
    1. 使用@Before注解标识
    2. 在方法执行之前执行的通知。
    //前置通知
    @Before(value = "execution(public int com.spring.aop.Calculator.*(int , int ))")
    public void beforeAdvice(JoinPoint joinPoint) {
        //获取方法名
        String methodName = joinPoint.getSignature().getName();
        //获取参数
        Object[] args = joinPoint.getArgs();
        System.out.println("Logging: The method "+methodName+" begins with "+ Arrays.toString(args));
    }
    
    
  3. 后置通知
    1. 后置通知是在连接点完成之后执行的,即连接点返回结果或者抛出异常的时候。
    2. 使用@After注解标识
//后置通知
@After("execution(public int Calculator.*(int , int ))")
public void afterAdvice(JoinPoint joinPoint) {
    //获取方法名
    String methodName = joinPoint.getSignature().getName();
    System.out.println("Logging: The method "+methodName+" ends");
}

     4.返回通知

  •  使用@AfterReturning注解标识
  •         无论连接点是正常返回还是抛出异常,后置通知都会执行。如果只想在连接点返回的时候记录日志,应使用返回通知代替后置通知。
    • 原始的切点表达式需要出现在pointcut属性中
    • 必须在通知方法的签名中添加一个同名参数。在运行时Spring AOP会通过这个参数传递返回值
    • 在返回通知中,通过@AfterReturning注解的returning属性获取连接点的返回值。该属性的值即为用来传入返回值的参数名称
//返回通知
@AfterReturning(pointcut = "execution(* Calculator.*(int , int ))",returning = "result")
public void returningAdvice(JoinPoint joinPoint , Object result){
    //获取方法名
    String methodName = joinPoint.getSignature().getName();
    //获取参数
    Object[] args = joinPoint.getArgs();
    System.out.println("Logging: The method "+methodName+" returns "+result);
}

  • 5.异常通知

    1. 使用@AfterThrowing注解标识
    2. 在异常通知中,通过@AfterThrowing注解的throwing属性获取异常信息。Throwable是所有错误和异常类的顶级父类,所以在异常通知方法可以捕获到任何错误和异常。
    3. 如果只对某种特殊的异常类型感兴趣,可以将参数声明为其他异常的参数类型。然后通知就只在抛出这个类型及其子类的异常时才被执行。
/异常通知
@AfterThrowing(pointcut = "execution(* Calculator.*(..))",throwing = "e")
public void throwingAdvice(JoinPoint joinPoint,Throwable e){
    //获取方法名
    String methodName = joinPoint.getSignature().getName();
    System.out.println("Logging: The method "+methodName+" occurs "+e);
}

      6.环绕通知 

  • 通过@Around注解标识
    • 环绕通知是所有通知类型中功能最为强大的,能够全面地控制连接点,甚至可以控制是否执行连接点。
    • 对于环绕通知来说,连接点的参数类型必须是ProceedingJoinPoint。它是 JoinPoint的子接口,允许控制何时执行,是否执行连接点。
    • 在环绕通知中需要明确调用ProceedingJoinPoint的proceed()方法来执行被代理的方法。如果忘记这样做就会导致通知被执行了,但目标方法没有被执行。
    • 注意:环绕通知的方法需要返回目标方法执行之后的结果,即调用 joinPoint.proceed()的返回值,否则会出现异常

//环绕通知
@Around("execution(* Calculator.*(..))")
public Object aroundAdvice(ProceedingJoinPoint proceedingJoinPoint){
    //获取方法名
    String methodName = proceedingJoinPoint.getSignature().getName();
    //获取参数
    Object[] args = proceedingJoinPoint.getArgs();
    Object result=null;
    try {
        //前置通知
        System.out.println("★Logging: The method "+methodName+" begins with "+ Arrays.toString(args));
        //执行目标方法
        result = proceedingJoinPoint.proceed();
        //返回通知
        System.out.println("★Logging: The method "+methodName+" returns "+result);
    } catch (Throwable throwable) {
        //异常通知
        System.out.println("★Logging: The method "+methodName+" occurs "+throwable);
        throwable.printStackTrace();
    }finally {
        //后置通知
        System.out.println("★Logging: The method "+methodName+" ends");
    }
    return result;
}

6 设置切面的优先级

  1. 在同一个连接点上应用不止一个切面时,除非明确指定,否则它们的优先级是不确定的。
  2. 切面的优先级可以通过实现Ordered接口或利用@Order注解指定。
  3. 实现Ordered接口,getOrder()方法的返回值越小,优先级越高。
  4. 若使用@Order注解,通过注解的value属性指定优先级,值越小优先级越高。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_43555873

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值