Spring2.0中的AOP使用(配置applicationContext.xml核心文件中)
1、Spring AOP部分使用JDK动态代理或者CGLIB来为目标对象创建代理。(创建推荐尽量使用JDK动态代理)如果被代理的目标对象实现了至少一个接口,则会使用JDK动态代理。所有目标类型实现的接口都被代理。若改目标对象没有实现任何接口,则创建一个CGLIB代理。
2、Spring2.0中的Pointcut定义?
Pointcut是JoinPoint的集合,它是程序中需要注入Advice的位置的集合,在Spring2.0中,Pointcut的定义包括两个部分:Pointcut表达式(expression)和Pointcut签名(signature)。
3、基于XML Schema的前置通知
1、Java代码如下:
package com.zuxia.advice;import org.aspectj.lang.JoinPoint;public class FrontLogAdvice {//JoinPoint(连接点)连接点是应用程序执行过程中插入Aspect的地点,这个地点可以方法调用,//异常抛出等时刻public void printLog(JoinPoint joinPoint) {System.out.print("*************");System.out.print(joinPoint.getTarget().toString());System.out.print(" ");System.out.print(joinPoint.getSignature().getName());System.out.print("(");Object[] args =joinPoint.getArgs();for (int i = 0; i < args.length; i++) {System.out.print(args[i]); if(i<args.length - 1){ System.out.print(","); }}System.out.print(")");System.out.println(" *************");}}
<bean id="frontLogAdvice" ref="frontLogAdvice"> <!-- 配置切点,指定将通知注入到哪些类的哪些方法上 --> <aop:pointcut id="serviceMethod" expression="execution(* com.zuxia.service.*.*(..))"/> <!-- 配置一个前置通知--> <aop:before method="printLog" pointcut-ref="serviceMethod"/> </aop:aspect> </aop:config>
package com.zuxia.advice;import org.aspectj.lang.JoinPoint;public class AfterLogAdvice {//JoinPoint(连接点)连接点是应用程序执行过程中插入Aspect的地点,这个地点可以方法调用,//异常抛出等时刻public void printAfterLog(JoinPoint joinPoint) {System.out.print("*************");System.out.print(joinPoint.getTarget().toString());System.out.print(" ");System.out.print(joinPoint.getSignature().getName());System.out.print("(");Object[] args =joinPoint.getArgs();for (int i = 0; i < args.length; i++) {System.out.print(args[i]); if(i<args.length - 1){ System.out.print(","); }}System.out.print(")");System.out.println(" *************");}}
<bean id="afterLogAdvice" ref="AfterLogAdvice"> <!-- 配置切点,指定将通知注入到哪些类的哪些方法上 --> <aop:pointcut id="serviceMethod" expression="execution(* com.zuxia.service.*.*(..))"/> <!-- 配置一个后置通知--> <aop:after method="printAfterLog" pointcut-ref="serviceMethod"/> </aop:aspect> </aop:config>
package com.zuxia.advice;import org.aspectj.lang.ProceedingJoinPoint;public class ProceedAdvice {public void printProceedLog(ProceedingJoinPoint joinpoint){System.out.print("***************** ");System.out.print(joinpoint.getTarget().toString());System.out.print(" ");System.out.print(joinpoint.getSignature().getName());System.out.print("(");Object[] args = joinpoint.getArgs();for (int i = 0; i < args.length; i++) {System.out.print(args[i]);if (i < args.length - 1) {System.out.print(", ");}}System.out.print(")");System.out.println(" ***************** begin");try {joinpoint.proceed();} catch (Throwable e) {e.printStackTrace();}System.out.print("***************** ");System.out.print(joinpoint.getTarget().toString());System.out.print(" ");System.out.print(joinpoint.getSignature().getName());System.out.print("(");for (int i = 0; i < args.length; i++) {System.out.print(args[i]);if (i < args.length - 1) {System.out.print(", ");}}System.out.print(")");System.out.println(" ***************** end");}}
<bean id="proceedAdvice" ref="proceedAdvice"> <!-- 配置切点,指定将通知注入到哪些类的哪些方法上 --> <aop:pointcut id="serviceMethod" expression="execution(* com.zuxia.service.*.*(..))"/> <!-- 配置一个环绕型通知--> <aop:after method="printProceedLog" pointcut-ref="serviceMethod"/> </aop:aspect> </aop:config>
package com.zuxia.advice;import org.aspectj.lang.JoinPoint;public class ExceptionAdvice {public void printExceptionLog(JoinPoint joinpoint){//不推荐使用Throwable对象,没必要打印堆栈System.out.print("***************** ");System.out.print(joinpoint.getTarget().toString());System.out.print(" ");System.out.print(joinpoint.getSignature().getName());System.out.print("(");Object[] args = joinpoint.getArgs();for (int i = 0; i < args.length; i++) {System.out.print(args[i]);if (i < args.length - 1) {System.out.print(", ");}}System.out.print(")");System.out.println(" *****************");//打印异常堆栈信息 //System.out.println(throwable.getStackTrace());}}
<bean id="exceptionAdvice" ref="exceptionAdvice"> <!-- 配置切点,指定将通知注入到哪些类的哪些方法上 --> <aop:pointcut id="serviceMethod" expression="execution(* com.zuxia.service.*.*(..))"/> <!-- 配置一个异常通知--> <aop:after method="printExceptionLog" pointcut-ref="serviceMethod"/> </aop:aspect> </aop:config>