com.sun.proxy.$Proxy10
The methodadd begins...[3, 6]
The method add ends...
The method add ends with 9
result:9
The methoddiv begins...[6, 3]
The method div ends...
The method div ends with 2
result:2
publicclassMain{publicstaticvoidmain(String[] args){
ApplicationContext ctx =newClassPathXmlApplicationContext("classpath*:applicationContext.xml");
ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class);
System.out.println(arithmeticCalculator.getClass().getName());int result = arithmeticCalculator.add(3,6);
System.out.println("result:"+ result);
result = arithmeticCalculator.div(6,3);
System.out.println("result:"+ result);
result = arithmeticCalculator.div(6,0);
System.out.println("result:"+ result);}}
测试结果:
com.sun.proxy.$Proxy11
The methodadd begins...[3,6]
The method add ends...
The method add ends with 9
result:9
The methoddiv begins...[6,3]
The method div ends...
The method div ends with 2
result:2
The methoddiv begins...[6,0]
The method div ends...
The method div occurs exception: java.lang.ArithmeticException:/ by zero
Exception in thread "main" java.lang.ArithmeticException:/ by zero
at com.fafa.spring.aop.impl.ArithmeticCalculatorImpl.div(ArithmeticCalculatorImpl.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:43)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:58)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy11.div(Unknown Source)
at com.fafa.spring.aop.impl.Main.main(Main.java:19)
com.sun.proxy.$Proxy8
The method add begins
The method add ends with 9
The method add ends
result:9
The method div begins
The method div ends with 2
The method div ends
result:2
可见环绕通知和动态代理是非常类似的,可以控制到方法是否执行,方法的返回值。
切面的优先级
此时我们再定义一个切面类ValidationAspect:
@Component@AspectpublicclassValidationAspect{@Before("execution(public int com.fafa.spring.aop.impl.ArithmeticCalculator.*(int,int))")publicvoidvalidateArgs(JoinPoint joinPoint){
System.out.println("-->validate:"+Arrays.asList(joinPoint.getArgs()));}}
注释掉LoggingAspect类中的aroundMethd方法
执行main方法:
com.sun.proxy.$Proxy10
The methodadd begins...[3, 6]
-->validate:[3, 6]
The method add ends...
The method add ends with 9
result:9
The methoddiv begins...[6, 3]
-->validate:[6, 3]
The method div ends...
The method div ends with 2
result:2
com.sun.proxy.$Proxy11
-->validate:[3, 6]
The methodadd begins...[3, 6]
The method add ends...
The method add ends with 9
result:9
-->validate:[6, 3]
The methoddiv begins...[6, 3]
The method div ends...
The method div ends with 2
result:2