>> 命名切点
下面是一个命名切点定义类
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/