方法拦截器:MethodInterceptor
MethodInterceptor --> org.springframework.aop.support.RegexpMethodPointcutAdvisor
ThrowsAdvice --> org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator
http://hi.baidu.com/loving102/blog/item/bf1395d3d9ddbd093bf3cfaf.html
实现MethodInterceptor接口,在调用目标对象的方法时,就可以实现在调用方法之前、调用方法过程中、调用方法之后对其进行控制。
MethodInterceptor接口可以实现MethodBeforeAdvice接口、AfterReturningAdvice接口、ThrowsAdvice接口这三个接口能够所能够实现的功能,但是应该谨慎使用MethodInterceptor接口,很可能因为一时的疏忽忘记最重要的MethodInvocation而造成对目标对象方法调用失效,或者不能达到预期的设想。
关于含有Advice的三种对目标对象的方法的增强
例子参考: http://www.360doc.com/content/07/0827/10/18042_697579.shtml
源代码
---------------------------------------------
MyMethodInterceptor.java
package com.app.aop;import java.util.Date;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;public class MyMethodInterceptor implements MethodInterceptor {/*public Object invoke(MethodInvocation invoke) throws Throwable {// TODO Auto-generated method stubSystem.out.println("Proceed start time:"+(new Date()));Object result = invoke.proceed();System.out.println("Proceed end time:"+(new Date()));return result;}*/public Object invoke(MethodInvocation invoke) throws Throwable {// TODO Auto-generated method stubSystem.out.println("\nProceed start time:"+(new Date()));try {Object result = invoke.proceed();return result;}finally{System.out.println("Proceed end time:"+(new Date()));}}}
<bean id="myMethodInterceptor" name="code">package com.app.aop;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class AopTest {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");//IBizProcessImpl t = ctx.getBean("bizOne",IBizProcessImpl.class);IBizProcessImpl t = ctx.getBean("bizOneTarget",IBizProcessImpl.class);try {t.doAnotherThing();} catch (Exception e) {// TODO: handle exception}}}