步骤拦截器:MethodInterceptor
方法拦截器: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
注:注释掉的那部分,有问题,一定要用try{......}finally{......}的方式来使用,否则可能会出问题。比如注释的那部分,只能打印第一个system.out.println(),不能打印第二个的!
applicationContext.xml
测试例子:
结果显示:
Proceed start time:Mon Jul 18 10:15:27 CST 2011
doAnotherThing-->
Proceed end time:Mon Jul 18 10:15:27 CST 2011
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 stub System.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 stub System.out.println("\nProceed start time:"+(new Date())); try { Object result = invoke.proceed(); return result; }finally{ System.out.println("Proceed end time:"+(new Date())); } } }
注:注释掉的那部分,有问题,一定要用try{......}finally{......}的方式来使用,否则可能会出问题。比如注释的那部分,只能打印第一个system.out.println(),不能打印第二个的!
applicationContext.xml
<bean id="myMethodInterceptor" class="com.app.aop.MyMethodInterceptor"></bean> <bean id="timeHandlerAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice"> <ref bean="myMethodInterceptor"/> </property> <property name="patterns"> <value>com.app.aop.BizProcessImpl.*</value> </property> </bean>
测试例子:
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 } } }
结果显示:
Proceed start time:Mon Jul 18 10:15:27 CST 2011
doAnotherThing-->
Proceed end time:Mon Jul 18 10:15:27 CST 2011