org.aspectj.lang.JoinPoint-中文简要API

 

AspectJ使用org.aspectj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强时,使用org.aspectj.lang.ProceedingJoinPoint表示连接点对象,该类是JoinPoint的子接口。任何一个增强方法都可以通过将第一个入参声明为JoinPoint访问到连接点上下文的信息。我们先来了解一下这两个接口的主要方法: 
1)JoinPoint 
 java.lang.Object[] getArgs():获取连接点方法运行时的入参列表; 
 Signature getSignature() :获取连接点的方法签名对象; 
 java.lang.Object getTarget() :获取连接点所在的目标对象; 
 java.lang.Object getThis() :获取代理对象本身; 
2)ProceedingJoinPoint 
ProceedingJoinPoint继承JoinPoint子接口,它新增了两个用于执行连接点方法的方法: 
 java.lang.Object proceed() throws java.lang.Throwable:通过反射执行目标对象的连接点处的方法; 
 java.lang.Object proceed(java.lang.Object[] args) throws java.lang.Throwable:通过反射执行目标对象连接点处的方法,不过使用新的入参替换原来的入参。 
package step2; import java.util.Arrays; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; //添加组件和切面注解 //===============begin=============== //================end================ public class LoggingAspect { @Before("execution(public int step2.ArithmeticCalculator.*(..))") public void beforeMethod(JoinPoint joinPoint){ String methodName = joinPoint.getSignature().getName(); Object[] args = joinPoint.getArgs(); System.out.println("INFO: The method " + methodName + " begin with " + Arrays.asList(args)); } @After("execution(public int step2.ArithmeticCalculator.*(..))") public void afterMethod(JoinPoint joinPoint){ String methodName = joinPoint.getSignature().getName(); System.out.println("INFO: The method " + methodName + " end."); } //添加返回通知,切入点表达式为step2.ArithmeticCalculator下的所有方法,带返回值; //通知信息为“INFO: The method 方法名 return result 返回值” //===============begin=============== //================end================ } package step2; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext cxt = new ClassPathXmlApplicationContext("spring-aop.xml"); //IoC容器中获取ArithmeticCalculatorImpl类型对象,调用add(10,20)方法 //===============begin=============== //================end================ } }
04-01
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值