这是我对Spring AOP framework的一点点探索 - 一个class和method调用的日志拦截处理,纪录方法的调用顺序。我希望这些可以帮助你理解spring aop 并且写出你自己的拦截器。
在spring 里面用拦截器需要扩展org.aopalliance.intercept.MethodInterceptor 接口的这个方法:
下一步,来看看拦截器的处理...
你可以在以下两行内做你想要做的任何处理:
Object retVal = methodInvocation.proceed();
return retVal;
这段代码的执行顺序是:
作为演示,我们只改变bean的 id
下一步,我们需要在Spring的 ApplicationContent里面附加这个拦截器
最后一步,我们在这个ApplicationContent里面来声明实际的业务对象,我们可以通过它的接口和Spring 的 ProxyFactoryBean来定义他们。
在这段应用代码中改变业务对象的访问顺序,其他什么都不用动
然后,在你的纪录traget(console,file,等),你可以输出相似的日志纪录(time 和不同层次的log)
在spring 里面用拦截器需要扩展org.aopalliance.intercept.MethodInterceptor 接口的这个方法:
代码: |
public Object invoke(MethodInvocation methodInvocation) throws Throwable; |
下一步,来看看拦截器的处理...
代码: |
import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class MyInterceptor implements MethodInterceptor { private final Log logger = LogFactory.getLog(getClass()); public Object invoke(MethodInvocation methodInvocation) throws Throwable { logger.info("Beginning method: " + methodInvocation.getMethod().getDeclaringClass() + "::" + methodInvocation.getMethod().getName()); long startTime = System.currentTimeMillis(); try { Object retVal = methodInvocation.proceed(); return retVal; } finally { logger.info("Ending method: " + methodInvocation.getMethod().getDeclaringClass() + "::" + methodInvocation.getMethod().getName()); logger.info("Method invocation time: " + (System.currentTimeMillis() - startTime) + " msecs."); } } } |
你可以在以下两行内做你想要做的任何处理:
Object retVal = methodInvocation.proceed();
return retVal;
这段代码的执行顺序是:
- 处理 Object retVal = methodInvocation.proceed(); 执行之前的声明
- 执行Object retVal = methodInvocation.proceed();,在拦截器堆栈里将控制权递交给下一个拦截器或者哪个方法。
- 处理return retVal之前的声明
- 执行return retVal,返回控制权给上面的拦截器或退出整个拦截器堆栈
- 下一步是,用我们写的拦截器,作为AOP target用来可以返回业务对象,如:
代码: |
<bean id="SearchBookBeanTarget" class="library.SearchBookBeanImpl" init-method="init" /> |
作为演示,我们只改变bean的 id
下一步,我们需要在Spring的 ApplicationContent里面附加这个拦截器
代码: |
<bean id="myInterceptor" class="library.MyInterceptor" /> |
最后一步,我们在这个ApplicationContent里面来声明实际的业务对象,我们可以通过它的接口和Spring 的 ProxyFactoryBean来定义他们。
代码: |
<bean id="SearchBookBean" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"><value>library.SearchBookBean</value></property> <property name="interceptorNames"> <list> <value>myInterceptor</value> <value>SearchBookBeanTarget</value> </list> </property> </bean> |
- proxyInterfaces: 这是我们业务对象的业务代理接口
- interceptorNames: 这个拦截器的执行顺序,是list中最后的那个这个业务对象的target.记得在业务对象的target一定在list里面声明,否则你的业务对象不会执行;在其他的操作里面获得一个异常,它将告诉你所有拦截器的调用顺序。
在这段应用代码中改变业务对象的访问顺序,其他什么都不用动
然后,在你的纪录traget(console,file,等),你可以输出相似的日志纪录(time 和不同层次的log)
代码: |
Beginning method: interface library.SearchBookBean::searchBook .... (log messages about library.SearchBookBean.searchBook()....) .... Ending method: interface library.SearchBookBean::searchBook Method invocation time: 10 msecs. |