首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > ASP >

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

2013-04-09 
spring日记(六):@AspectJ高级主题 命名切点下面是一个命名切点定义类1234567891011121314package com.sp

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

>> 命名切点

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

1234567891011121314package 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(){}}

然后使用这个:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283@Aspectpublic 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:通过反射执行连接点处方法,不过传入了新的参数。

热点排行