Spring AOP(2)

来源:互联网 发布:ibm软件 编辑:程序博客网 时间:2024/06/05 04:02
Before advice
 
<aop:aspect id="myAspect" ref="aBean"
<aop:before
pointcut-ref="dataBaseOperation" method="doSomeThing"  />针对当前的这个切入点, 前置通知会调用哪一个方法去执行,这个方法时是aBean里面的
</aop:aspect>

下面写的好处是直接可以在before,after都可以直接指定不同的方法;上面写的好处是免去了每次都去修改
<aop:aspect id="myAspect" ref="aBean"
<aop:before
pointcut="exception(*.com.zit.xyz.service..(..))" method="doSomeThing"  />
</aop:aspect>

After returning advice

<aop:aspect id="myAspect" ref="aBean"
<aop:after-returning
pointcut-ref="dataBaseOperation" method="doSomeThing"  />针对当前的这个切入点, 前置通知会调用哪一个方法去执行,这个方法时是aBean里面的
</aop:aspect>

<aop:aspect id="myAspect" ref="aBean"
<aop:after-returning
pointcut="exception(*.com.zit.xyz.service..(..))" returning="retVal" method="doSomeThing"  />限制返回值表达式是retVal
</aop:aspect> 方法执行完执行这个

After throwing advice

<aop:aspect id="myAspect" ref="aBean"
<aop:after-throwing
pointcut-ref="dataBaseOperation" method="doSomeThing"  />针对当前的这个切入点, 前置通知会调用哪一个方法去执行,这个方法时是aBean里面的
</aop:aspect>

还有throwing属性来指定可被传递的异常的参数名称
<aop:aspect id="myAspect" ref="aBean"
<aop:after-throwing
pointcut="exception(*.com.zit.xyz.service..(..))" throwing="datadada" method="doSomeThing"  />
</aop:aspect> 正常不执行,抛出异常后执行,抛出一个异常后,After returning就不执行了

After (finally) advice

<aop:aspect id="myAspect" ref="aBean"
<aop:after
pointcut-ref="dataBaseOperation" method="doSomeThing"  />针对当前的这个切入点, 前置通知会调用哪一个方法去执行,这个方法时是aBean里面的
</aop:aspect> 最后执行,打印的最后一个;即使是抛出异常after也执行(无论方法是否正常结束,after都执行)

Around advice

<aop:aspect id="myAspect" ref="aBean"
<aop:around
pointcut-ref="dataBaseOperation" method="doSomeThing"  />针对当前的这个切入点, 前置通知会调用哪一个方法去执行,这个方法时是aBean里面的
</aop:aspect> 
通知方法的的第一个参数必须是ProceedingJoinPoint类型
public Object doSomeThing(ProceedingJoinPoint pjp) throws Throwable{
//业务1
Object retVal = pjp.proceed();
//业务2
return retVal;
}

Advice parameters

<aop:aspect id="myAspect" ref="aBean"
<aop:around   参数需要和参数名称一样
pointcut="exception(*.com.zit.xyz.service.init(String,int)) and args(bigName,times)" method="doSomeThing"  />针对当前的这个切入点, 前置通知会调用哪一个方法去执行,这个方法时是aBean里面的
</aop:aspect> 

public Object doSomeThing(ProceedingJoinPoint pjp,String bigName,int times) throws Throwable{
//业务1
Object retVal = pjp.proceed();
//业务2
return retVal;
}
原创粉丝点击