Spring AOP 的使用详解

Spring AOP 的使用详解

1、AOP简介

​ AOP (Aspect Orient Programming),直译过来就是 面向切面编程。通俗地说,aop就是在不影响业务代码的情况下,实现对现有业务的一些改造,或者在现有功能的基础上新增一些附加的功能,例如:日志打印、异常处理等。

2、有关名词释义

  • 通知(Advice): AOP 框架中的增强处理。通知描述了切面何时执行以及如何执行增强处理。
  • 连接点(join point): 连接点表示应用执行过程中能够插入切面的一个点,这个点可以是方法的调用、异常的抛出。在 Spring AOP 中,连接点总是方法的调用。
  • 切点(PointCut): 可以插入增强处理的连接点。
  • 切面(Aspect): 切面是通知和切点的结合。
  • 引入(Introduction):引入允许我们向现有的类添加新的方法或者属性。
  • 织入(Weaving): 将增强处理添加到目标对象中,并创建一个被增强的对象,这个过程就是织入。

3、通知类型

通知类型接口描述
前置通知org.springframework.aop.MethodBeforeAdvice在目标方法执行前实施增强。
后置通知org.springframework.aop.AfterReturningAdvice在目标方法执行后实施增强。
后置返回通知org.springframework.aop.AfterReturningAdvice在目标方法执行完成,并返回一个返回值后实施增强。
环绕通知org.aopalliance.intercept.MethodInterceptor在目标方法执行前后实施增强。
异常通知org.springframework.aop.ThrowsAdvice在方法抛出异常后实施增强。
引入通知org.springframework.aop.IntroductionInterceptor在目标类中添加一些新的方法和属性。

4、Demo 编写

package com.example.aopdemo;

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.lang.reflect.Method;

@Slf4j
@Aspect
@Component
public class LogAspect {

    @Pointcut("execution (* com.example.aopdemo.TestController.*(..))")
    public void printLog(){

        System.out.println("我是一个切点");
    }


    /*使用aop 进行请求拦截,获取请求头和请求参数并打印*/
    @Before("printLog()")
    public void before(JoinPoint joinPoint){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        log.info("URL参数={}",requestAttributes.getRequest().getQueryString());

        String header = requestAttributes.getRequest().getHeader("userId");

        System.out.println("header is : "+header);
    }


    @After("printLog()")
    public void after(){
        System.out.println("我是一个after");
    }

    /*使用aop 进行入参的拦截,修改入参,修改返回值*/
  @Around("printLog()")
      public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
      System.out.println("--------Around方法开始执行");

      Gson gson = new Gson();
      Object[] args = proceedingJoinPoint.getArgs();

      System.out.println("参数:"+args[0]);
      // 将获取的值进行修改
      args[0] = "我是你爸爸";        //修改参数值
      // 然后继续执行该接口(这里修改参数)
      Object ret = proceedingJoinPoint.proceed(args);  //将新的参数传入

      return args[0];  //返回新的返回值,类型与方法原来的返回值相同

  }

}

注意:ProceedingJoinPoint 只能在Around 类的通知类型中使用。

5、切点表达式

  1. 在Spring AOP中,连接点始终代表方法的执行。切入点是与连接点匹配的,切入点表达语言是以编程方式描述切入点的方式。
  2. 切入点(Poincut)是定义了在“什么地方”进行切入,哪些连接点会得到通知。显然,切点一定是连接点
  3. 切点是通过@Pointcut注解和切点表达式定义的。@Pointcut注解可以在一个切面内定义可重用的切点。

execute表达式

*代表匹配任意修饰符及任意返回值,参数列表中..匹配任意数量的参数

可以使用&&、||、!、三种运算符来组合切点表达式,表示与或非的关系

  1. 拦截任意公共方法execution(public * *(..))
  2. 拦截以set开头的任意方法execution(* set*(..))
  3. 拦截类或者接口中的方法
//拦截AccountService(类、接口)中定义的所有方法
execution(* com.xyz.service.AccountService.*(..))
  1. 拦截包中定义的方法,不包含子包中的方法
//拦截com.xyz.service包中所有类中任意方法,不包含子包中的类
execution(* com.xyz.service.*.*(..))
  1. 拦截包或者子包中定义的方法
//拦截com.xyz.service包或者子包中定义的所有方法
execution(* com.xyz.service..*.*(..))

6、JoinPoint和ProceedingJoinPoint对象

  1. JoinPoint对象封装了SpringAop中切面方法的信息,在切面方法中添加JoinPoint参数,就可以获取到封装了该方法信息的JoinPoint对象.
  2. ProceedingJoinPoint对象是JoinPoint的子接口,该对象只用在@Around的切面方法中
方法名功能
Signature getSignature();获取封装了署名信息的对象,在该对象中可以获取到目标方法名,所属类的Class等信息
Object[] getArgs();获取传入目标方法的参数对象
Object getTarget();获取被代理的对象
Object getThis();获取代理对象
@Aspect
@Component
public class aopAspect {
    /**
     * 定义一个切入点表达式,用来确定哪些类需要代理
     * execution(* aopdemo.*.*(..))代表aopdemo包下所有类的所有方法都会被代理
     */
    @Pointcut("execution(* aopdemo.*.*(..))")
    public void declareJoinPointerExpression() {}

    /**
     * 前置方法,在目标方法执行前执行
     * @param joinPoint 封装了代理方法信息的对象,若用不到则可以忽略不写
     */
    @Before("declareJoinPointerExpression()")
    public void beforeMethod(JoinPoint joinPoint){
        System.out.println("目标方法名为:" + joinPoint.getSignature().getName());
        System.out.println("目标方法所属类的简单类名:" +        joinPoint.getSignature().getDeclaringType().getSimpleName());
        System.out.println("目标方法所属类的类名:" + joinPoint.getSignature().getDeclaringTypeName());
        System.out.println("目标方法声明类型:" + Modifier.toString(joinPoint.getSignature().getModifiers()));
        //获取传入目标方法的参数
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            System.out.println("第" + (i+1) + "个参数为:" + args[i]);
        }
        System.out.println("被代理的对象:" + joinPoint.getTarget());
        System.out.println("代理对象自己:" + joinPoint.getThis());
    }

    /**
     * 环绕方法,可自定义目标方法执行的时机
     * @param pjd JoinPoint的子接口,添加了
     *            Object proceed() throws Throwable 执行目标方法
     *            Object proceed(Object[] var1) throws Throwable 传入的新的参数去执行目标方法
     *            两个方法
     * @return 此方法需要返回值,返回值视为目标方法的返回值
     */
    @Around("declareJoinPointerExpression()")
    public Object aroundMethod(ProceedingJoinPoint pjd){
        Object result = null;

        try {
            //前置通知
            System.out.println("目标方法执行前...");
            //执行目标方法
            //result = pjd.proeed();
            //用新的参数值执行目标方法
            result = pjd.proceed(new Object[]{"newSpring","newAop"});
            //返回通知
            System.out.println("目标方法返回结果后...");
        } catch (Throwable e) {
            //异常通知
            System.out.println("执行目标方法异常后...");
            throw new RuntimeException(e);
        }
        //后置通知
        System.out.println("目标方法执行后...");

        return result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOPSpring框架中的一个重要模块,它通过动态代理实现了面向切面编程的思想。下面是Spring AOP的XML配置详解: 1. 配置命名空间和约束 在使用Spring AOP之前,需要在XML配置文件中声明Spring AOP的命名空间和约束,如下所示: ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> ``` 2. 配置切面 在Spring AOP中,切面由切点和通知组成。切点定义了在哪些方法上进行拦截,通知定义了在拦截时要执行的逻辑。下面是一个切面的配置示例: ```xml <bean id="myAspect" class="com.example.MyAspect"> <property name="transactionManager" ref="txManager"/> </bean> <aop:config> <aop:aspect ref="myAspect"> <aop:pointcut id="serviceMethods" expression="execution(* com.example.Service.*(..))"/> <aop:before pointcut-ref="serviceMethods" method="beginTransaction"/> <aop:after-returning pointcut-ref="serviceMethods" method="commitTransaction"/> <aop:after-throwing pointcut-ref="serviceMethods" method="rollbackTransaction"/> </aop:aspect> </aop:config> ``` 上面的例子中,声明了一个名为“myAspect”的切面,并定义了一个名为“serviceMethods”的切点,拦截com.example.Service包中所有方法的执行。在拦截时,分别执行了beginTransaction()、commitTransaction()和rollbackTransaction()方法。 3. 配置通知 通知是切面中的一个组成部分,它定义了在什么时候执行切面的逻辑。Spring AOP支持五种类型的通知:前置通知、后置通知、返回通知、异常通知和环绕通知。下面是通知的配置示例: ```xml <aop:before pointcut="execution(* com.example.Service.*(..))" method="beforeAdvice"/> <aop:after-returning pointcut="execution(* com.example.Service.*(..))" method="afterReturningAdvice"/> <aop:after-throwing pointcut="execution(* com.example.Service.*(..))" method="afterThrowingAdvice"/> <aop:around pointcut="execution(* com.example.Service.*(..))" method="aroundAdvice"/> ``` 上面的例子中,分别配置了前置通知、返回通知、异常通知和环绕通知,它们都拦截com.example.Service包中的所有方法。在拦截时,分别执行了beforeAdvice()、afterReturningAdvice()、afterThrowingAdvice()和aroundAdvice()方法。 4. 配置引入 引入是Spring AOP中的一个特殊功能,它允许将额外的方法和属性添加到现有的类中,而不需要修改原始类的代码。下面是引入的配置示例: ```xml <aop:config> <aop:aspect ref="myAspect"> <aop:declare-parents types-matching="com.example.Service+" implement-interface="com.example.Transactional" default-impl="com.example.TransactionalImpl"/> </aop:aspect> </aop:config> ``` 上面的例子中,声明了一个名为“myAspect”的切面,并引入了接口com.example.Transactional,将其实现类指定为com.example.TransactionalImpl。这样,在运行时,com.example.Service类就自动实现了com.example.Transactional接口。 以上就是Spring AOP的XML配置详解。在实际开发中,可以根据业务需求和实际情况选择合适的配置方式来实现切面编程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值