java aop xml配置_spring AOP之基于xml配置文件的方式来配置AOP

packagecom.gong.spring.aop.impl2;importjava.util.Arrays;importjava.util.List;importjavax.management.RuntimeErrorException;importorg.aspectj.lang.JoinPoint;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.After;importorg.aspectj.lang.annotation.AfterReturning;importorg.aspectj.lang.annotation.AfterThrowing;importorg.aspectj.lang.annotation.Around;importorg.aspectj.lang.annotation.Aspect;importorg.aspectj.lang.annotation.Before;importorg.aspectj.lang.annotation.Pointcut;importorg.springframework.stereotype.Component;public classLoggingAspect {public voidbeforeMethod(JoinPoint joinPoint) {

String methodName=joinPoint.getSignature().getName();

List args =Arrays.asList(joinPoint.getArgs());

System.out.println(methodName+" begin with "+args);

}public voidafterMethod(JoinPoint joinPoint) {//获取名字

String methodName =joinPoint.getSignature().getName();//获取参数

List args =Arrays.asList(joinPoint.getArgs());

System.out.println(methodName+" end with "+args);

}public voidafterReturning(JoinPoint joinPoint,Object result) {

String methodName=joinPoint.getSignature().getName();

List args =Arrays.asList(joinPoint.getArgs());

System.out.println("在afterReturning得到返回值:"+result);

System.out.println(methodName+" end with "+args);

}public voidafterThrowing(JoinPoint joinPoint,Exception ex) {

String methodName=joinPoint.getSignature().getName();

System.out.println(methodName+" occurs exception:"+ex);

}publicObject aroundMethod(ProceedingJoinPoint pjd) {

Object result= null;

String methodName=pjd.getSignature().getName();//执行目标方法

try{//前置通知

System.out.println(methodName+" begin with "+Arrays.asList(pjd.getArgs()));//执行目标方法

result =pjd.proceed();//后置通知

System.out.println("在aroundMethod中得到值:"+result);

}catch(Throwable e) {//TODO Auto-generated catch block//异常通知

System.out.println("the method occurs exception:" +e);throw newRuntimeException(e);

}//后置通知

System.out.println(methodName+" end with "+Arrays.asList(pjd.getArgs()));returnresult;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值