Spring4基础二二-AOP篇-通知(2)-通知详解

上一章节《Spring4基础二一-AOP篇-通知(1)-入门讲解》
如果文中有纰漏,请多多指正!!!
一.通知详解1.jar包引入除了要引入核心jar及日志jar外 , 还需要引入依赖包com.springsource.org.aopalliance-1.0.0.jar这个包是AOP联盟的API包 , 里面包含了针对面向切面的接口 。 通常Spring等其它具备动态织入功能的框架依赖此包 。 否则会报如下错误【ThehierarchyofthetypeMyMethodBeforeAdviceisinconsistent】
//自定义前置通知publicclassMyMethodBeforeAdviceimplementsMethodBeforeAdvice{/***@parammethod目标对象要执行的方法*@paramargs方法的参数*@paramtarget目标对象*@throwsThrowable如果这个对象想要终止调用 , 那么抛出异常 , 异常会返回给方法调用者 , 否则异常会被包装为运行时异常*/@Overridepublicvoidbefore(Methodmethod,Object[]args,Objecttarget)throwsThrowable{System.out.println("前置的通知:[MyMethodBeforeAdvice]");}}3.后置通知:AfterReturningAdvice后置通知,在目标方法执行之后执行 , 不改变目标方法的执行流程 , 可以获取到目标方法的返回结果 , 但无法改变目标方法的结果 。
定义后置通知需要实现接口AfterReturningAdvice 。 该接口中有一个方法afterReturning() , 会在目标方法执行之后执行 。
//自定义后置通知publicclassMyAfterReturningAdviceimplementsAfterReturningAdvice{/***当方法成功返回之后运行*@paramreturnValue如果方法有返回值 , 那么returnValue就是返回值*@parammethod目标对象执行的方法*@paramargs方法的参数*@paramtarget目标对象*@throwsThrowable同上*/@OverridepublicvoidafterReturning(ObjectreturnValue,Methodmethod,Object[]args,Objecttarget)throwsThrowable{System.out.println("后置通知:MyAfterReturningAdvice");}}4.环绕通知:MethodInterceptor环绕通知也叫做方法拦截器 , 可以在目标方法调用之前及之后做处理 , 可以改变目标方法的返回值 , 也可以改变程序执行流程 , 需要实现MethodInterceptor接口 。
//自定义环绕通知publicclassMyMethodInterceptorimplementsMethodInterceptor{/***@paraminvocation封装了目标对象和参数数组*调用它的invocation.proceed()方法即执行目标对象的方法 。 *@return返回增强处理后的方法执行的结果*/@OverridepublicObjectinvoke(MethodInvocationinvocation)throwsThrowable{System.out.println("环绕通知:目标方法执行之前");//执行目标方法Objectresult=invocation.proceed();System.out.println("环绕通知:目标方法执行之后");if(result!=null){//可以改变目标方法的返回值result=((String)result).toUpperCase();}returnresult;}}5.异常通知:ThrowsAdvice异常通知需要实现ThrowsAdvice接口(标识接口) 。 当切点方法抛出异常时 , 异常抛出增强才会被执行 。 该接口没有指定必须要实现的方法 。 但接口源码注释定义指定了四种实现方法 , 可根据需要选择实现 。
publicvoidafterThrowing(Exceptionex)publicvoidafterThrowing(RemoteException)publicvoidafterThrowing(Methodmethod,Object[]args,Objecttarget,Exceptionex)publicvoidafterThrowing(Methodmethod,Object[]args,Objecttarget,ServletExceptionex)【Spring4基础二二-AOP篇-通知(2)-通知详解】自定义异常
//运算异常publicclassMathExceptionextendsException{publicMathException(Stringmsg){super(msg);}}异常通知
//异常通知publicclassMyThrowsAdviceimplementsThrowsAdvice{publicvoidafterThrowing(Exceptionex){if(exinstanceofMathException){System.out.println("捕获...MathException异常");}}}6.xml配置beforeAdviceafterReturningAdvicemethodInterceptorthrowsAdvice7.测试@Testpublicvoidtest1()throwsMathException{ClassPathXmlApplicationContextapplication=newClassPathXmlApplicationContext("applicationContext.xml");//获取代理对象IServiceDemoservice=(IServiceDemo)application.getBean("serviceProxy");Stringstr=service.doWork("doWork 。。。 ");//正常测试//Stringstr1=service.doWork("1");//异常测试System.out.println(str);application.close();//销毁}正常测试结果前置的通知-目标方法执行之前:[MethodBeforeAdvice]环绕通知[MethodInterceptor]:目标方法执行之前环绕通知[MethodInterceptor]:目标方法执行之后后置通知-目标方法执行之后:[AfterReturningAdvice]执行完毕--结果DOWORK 。。。 异常测试结果打印前置的通知-目标方法执行之前:[MethodBeforeAdvice]环绕通知[MethodInterceptor]:目标方法执行之前异常通知[ThrowsAdvice]-捕获...MathException异常异常测试结果控制台报错com.spring.aop.advice.MathException:MathException异常出现了......atcom.spring.aop.demo1.ServiceDemoImpl.doWork(ServiceDemoImpl.java:11)


    推荐阅读