spring日记(六):@AspectJ高级主题

本文详细介绍了AspectJ中切点的概念及其定义方式,包括命名切点的使用及复合切点的创建。同时,深入探讨了如何利用连接点对象获取方法参数、方法签名、目标对象等关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

>> 命名切点

下面是一个命名切点定义类

package com.springzoo.aspectj.advanced;
 
import org.aspectj.lang.annotation.Pointcut;
 
public class TestNamePointcut {
    @Pointcut("within(com.springzoo.*)")
    private void inPackage(){}
 
    @Pointcut("execution(* greetTo(..)))")
    protected void greetTo(){}
 
    @Pointcut("inPackage() and greetTo()")
    public void inPkgGreetTo(){}
}

然后使用这个:

@Aspect
public class TestAspect {
    //-------------复合运算----------
//  @Before("!target(com.springzoo.NaiveWaiter) "+
//          "&& execution(* serveTo(..)))")
//  public void notServeInNaiveWaiter() {
//      System.out.println("--notServeInNaiveWaiter() executed!--");
//  }
//  @After("within(com.springzoo.*) "
//          + " && execution(* greetTo(..)))")
//  public void greeToFun() {
//      System.out.println("--greeToFun() executed!--");
//  }
//  
//  @AfterReturning("target(com.springzoo.Waiter) || "+
//                  " target(com.springzoo.Seller)")
//  public void waiterOrSeller(){
//      System.out.println("--waiterOrSeller() executed!--");
//  }
     
//  //------------引用命名切点----------//
//  @Before("TestNamePointcut.inPkgGreetTo()")
//  public void pkgGreetTo(){
//      System.out.println("--pkgGreetTo() executed!--");
//  }
//
//  @Before("!target(com.springzoo.NaiveWaiter) && "
//          +"TestNamePointcut.inPkgGreetTo()")
//  public void pkgGreetToNotNaiveWaiter(){
//      System.out.println("--pkgGreetToNotNaiveWaiter() executed!--");
//  }
//
    //------------访问连接点对象----------//
    @Around("execution(* greetTo(..)) && target(com.springzoo.NaiveWaiter)")
    public void joinPointAccess(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("------joinPointAccess-------");
        System.out.println("args[0]:"+pjp.getArgs()[0]);        
        System.out.println("signature:"+pjp.getTarget().getClass());
        pjp.proceed();
        System.out.println("-------joinPointAccess-------");
    }
//  
//  //------------绑定连接点参数----------//
//  @Before("target(com.springzoo.NaiveWaiter) && args(name,num,..)")
//  public void bindJoinPointParams(int num,String name){
//     System.out.println("----bindJoinPointParams()----");
//     System.out.println("name:"+name);
//     System.out.println("num:"+num);
//     System.out.println("----bindJoinPointParams()----");
//  }
 
  //------------绑定代理对象----------//
//  @Before("execution(* greetTo(..)) && this(waiter)")
//  @Before("this(waiter)")
//  public void bindProxyObj(Waiter waiter){
//     System.out.println("----bindProxyObj()----");
//     System.out.println(waiter.getClass().getName());
//     System.out.println("----bindProxyObj()----");
//  }
     
      //------------绑定类标注对象----------//
//  @Before("@within(m)")
//  public void bindTypeAnnoObject(Monitorable m){
//     System.out.println("----bindTypeAnnoObject()----");
//     System.out.println(m.getClass().getName());
//     System.out.println("----bindTypeAnnoObject()----");
//  }
    //------------绑定抛出的异常----------//
//  @AfterReturning(value="target(com.springzoo.SmartSeller)",returning="retVal")
//  public void bingReturnValue(int retVal){
//     System.out.println("----bingReturnValue()----");
//     System.out.println("returnValue:"+retVal);
//     System.out.println("----bingReturnValue()----");
//  }
     
//    //------------绑定抛出的异常----------//
//  @AfterThrowing(value="target(com.springzoo.SmartSeller)",throwing="iae")
//  public void bindException(IllegalArgumentException iae){
//     System.out.println("----bindException()----");
//     System.out.println("exception:"+iae.getMessage());
//     System.out.println("----bindException()----");
//  }   
}

>> 访问连接点信息

AspectJ使用org.aspcetj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强,使用org.aspectj.lang.ProceedingJoinPoint表示连接点对象。

有几个主要方法:

* java.lang.Object[] getArgs():获取连接点方法运行时的入参列表

* Signature getSignature():获取连接点的方法签名对象

* java.lang.Object getTarget():获取连接点所在的目标对象

* java.lang.Object getThis():获取代理对象本身

而ProceedingJoinPoint继承自JoinPoint,它新增两个用于执行连接点方法的方法:

* java.lang.Object proceed() throws Throwable:通过反射执行目标对象的连接点处的方法

* java.lang.Object proceed(java.lang.Object[] args) throws Throwable:通过反射执行连接点处方法,不过传入了新的参数。

 

本人博客已搬家,新地址为:http://yidao620c.github.io/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值