我想要测试自己的异常通知是否能够正常运行的方法(也可以说异常运行的过程) 1:首先是xml配置 <bean id="accountService" class="com.itheima.Service.Impl.AccountServiceImpl"></bean> <!--配置logger类 --> <bean id="Logger" class="com.itheima.logger.Logger"> </bean> <aop:config> <!--配置切点--> <aop:pointcut id="pt" expression="execution(* com.itheima.Service.Impl.*.*(..))"></aop:pointcut> <!-- 切面--> <aop:aspect id="adviceLog" ref="Logger"><!----> <aop:around method="aroundPrintLog" pointcut-ref="pt"></aop:around> </aop:aspect> </aop:config>
xml配置中切入点是在Impl包下所有的实现类的所有方法,所以当执行arountPrintLog方法,就会执行我的Log中环绕通知的代码
spring框架提供了一个接口:ProceedingJoinPoint.该接口有一个方法proceed();,此方法就相当于明确调用切入点方法。该接口可以作为环绕通知的方法参数,在程序执行时,spring框架会为我们提供该接口的实现类供使用
public Object aroundPrintLog(ProceedingJoinPoint pjp){
Object value = null;
try {
Object[] args = pjp.getArgs();//获取方法执行所需的参数
System.out.println("通知日志通知执行了---前置");
value= pjp.proceed(args);
System.out.println("通知日志通知执行了---后置");
return value;
} catch (Throwable throwable) {
System.out.println("通知日志通知执行了---异常");
throw new RuntimeException(throwable);
}finally {
System.out.println("通知日志通知执行了---最终");
}
}
所以想要测试异常通知是否执行就在切入点(Service)的方法(save)中添加一个异常再执行 如:
public void saveAccount() { int i = 10/0; System.out.println("保存。。。。。"); }
这是一个很明显的运行时异常Exception in thread "main" java.lang.RuntimeException: java.lang.ArithmeticException: / by zero
从运行结果可知异常通知正常,添加一个小知识:异常通知和后置通知永远只能运行一个,不能同时运行
运行结果:
十月 17, 2018 11:05:50 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2d38eb89: startup date [Wed Oct 17 11:05:50 CST 2018]; root of context hierarchy
十月 17, 2018 11:05:50 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean.xml]
Exception in thread "main" 通知日志通知执行了---前置
通知日志通知执行了---异常
通知日志通知执行了---最终
java.lang.RuntimeException: java.lang.ArithmeticException: / by zero
at com.itheima.logger.Logger.aroundPrintLog(Logger.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:643)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:632)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy4.saveAccount(Unknown Source)
at com.itheima.Test.LogTest.main(LogTest.java:17)
Caused by: java.lang.ArithmeticException: / by zero
at com.itheima.Service.Impl.AccountServiceImpl.saveAccount(AccountServiceImpl.java:9)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:338)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:197)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:101)
at com.itheima.logger.Logger.aroundPrintLog(Logger.java:27)
... 13 more