基于@AspectJ的AOP的使用

来源:互联网 发布:白俄罗斯军力知乎 编辑:程序博客网 时间:2024/05/30 23:02

1.添加Maven依赖

<dependency>    <groupId>org.aspectj</groupId>    <artifactId>aspectjweaver</artifactId>    <version>1.8.10</version></dependency>

2.启用对@AspectJ的支持

Spring默认不支持@AspectJ风格的切面声明,为了支持需要使用如下配置:

<!-- proxy-target-class 默认为false,表示使用jdk动态代理织入增强,当配为<aop:aspectj-autoproxy  poxy-target-class="true"/>时,表示使用CGLib动态代理技术织入增强 --><aop:aspectj-autoproxy proxy-target-class="true"/>

3.声明切面

@AspectJ风格的声明切面非常简单,使用@Aspect注解进行声明:

@Aspectpublic class MyAspect {}

4.声明切点

@AspectJ风格的命名切入点使用org.aspectj.lang.annotation包下的@Pointcut+方法(方法必须是返回void类型)实现

/**     * 定义切点方法 没有实际的意义     */    @Pointcut(value = "execution(* com.platform.service.LoginService.*(..))")    public void myPointCut(){    }

value:指定切入点表达式;
argNames:指定命名切入点方法参数列表参数名字,可以有多个用“,”分隔,这些参数将传递给通知方法同名的参数,同时比如切入点表达式“args(param)”将匹配参数类型为命名切入点方法同名参数指定的参数类型。
pointcutName:切入点名字,可以使用该名字进行引用该切入点表达式

5.声明通知

@AspectJ风格的声明通知也支持5种通知类型:

  • 前置通知:使用org.aspectj.lang.annotation 包下的@Before注解声明
@Before("myPointCut()")  public void authority(JoinPoint jp){      System.out.println("before 增强 进行权限处理");      System.out.println("处理的目标方法为:"+jp.getSignature().getName());      System.out.println("目标方法的参数为:"+Arrays.toString(jp.getArgs()));      System.out.println("目标方法的对象为:"+jp.getTarget());  }
  • 后置返回通知:使用org.aspectj.lang.annotation 包下的@AfterReturning注解声明
@AfterReturning(pointcut="myPointCut()",returning="rvt")  public void doLog(JoinPoint jp,Object rvt){      System.out.println("@AfterReturning 增强 进行日志记录 返回值为:"+rvt);      System.out.println("处理的目标方法为:"+jp.getSignature().getName());      System.out.println("目标方法的参数为:"+Arrays.toString(jp.getArgs()));      System.out.println("目标方法的对象为:"+jp.getTarget());  }

value:指定切入点表达式或命名切入点;
pointcut:同样是指定切入点表达式或命名切入点,如果指定了将覆盖 value属性指定的,pointcut具有高优先级;
argNames:与Schema方式配置中的同义;
returning:与Schema方式配置中的同义

  • 后置异常通知:使用org.aspectj.lang.annotation 包下的@AfterThrowing注解声明;
@AfterThrowing(pointcut="myPointCut()",argNames="exception", throwing="exception")  public void afterThrowing(JoinPoint jp,Exception exception){      System.out.println("@AfterThrowing 异常处理");      System.out.println("处理的目标方法为:"+jp.getSignature().getName());      System.out.println("目标方法的参数为:"+Arrays.toString(jp.getArgs()));      System.out.println("目标方法的对象为:"+jp.getTarget());      System.out.println("异常为:"+exception.getMessage());  }
  • 后置最终通知:使用org.aspectj.lang.annotation 包下的
@After("myPointCut()")    public void release(JoinPoint jp){        System.out.println("@After 增强 进行权限处理");        System.out.println("处理的目标方法为:"+jp.getSignature().getName());        System.out.println("目标方法的参数为:"+Arrays.toString(jp.getArgs()));        System.out.println("目标方法的对象为:"+jp.getTarget());    }
  • 环绕通知:使用org.aspectj.lang.annotation 包下的@Around注解声明;
@Around("myPointCut()")  public Object processTx(ProceedingJoinPoint jp) throws Throwable{      System.out.println("Around 增强 执行目标方法之前");      Object[] args = jp.getArgs();      if(args != null && args.length > 0 & args[0].getClass() == String.class){          args[0] = "被改变的参数";      }      Object rvt = jp.proceed(args);      System.out.println("执行目标方法只会,事务结束");      return rvt;  }

value:指定切入点表达式或命名切入点;
argNames:与Schema方式配置中的同义;