spring 方法拦截器 MethodInterceptor接口

来源:互联网 发布:linux socket.h 编辑:程序博客网 时间:2024/06/05 06:28

前言

关于spring AOP的一些概念和配置这里就不详解了,网上也有很多相关的教程。spring里面配置AOP的方法很多,关于接口层面的AOP拦截器,汇总起来大概有如下4种:

  •  BeforeAdvice:前置通知, 对应spring的接口 MethodBeforeAdvice, 其对应切点为指定方法调用前
  •  AfterAdvice: 后置通知, 对应spring接口AfterReturningAdvice, 对应切点为指定方法调用后
  •  AroundAdvice:环绕通知, 对应spring接口MethodInterceptor, 对应的切点方法调用前,调用后,调用中,整个方法调用前后均可用,而且,还可以改变方法的行为
  •  ThrowsAdvice:  异常通知, 对应spring接口ThrowsAdvice,切点为目标方法抛出异常时调用
通过上述表述,可以看到,MethodInterceptor 最为特殊,它在方法整个作用前后都是有效的,下面我们来看一个实例。

实例

    实际业务中,我们有很多接口,我们需要记录接口日志,如果显示的在代码中调用log.info 这种做法去记录日志的话,会使代码变得很杂乱,业务代码逻辑中混杂着非业务逻辑,所以记录日志的场景非常适合实用AOP的方式去拦截记录。
1. 首先实现定义接口
<pre name="code" class="java">/** * 定义接口InterfaceA * @author Administrator * */public interface InterfaceA {public String methodA(String argsA, String argsB);}

2. 给一个实现
<pre name="code" class="java">/** * 实现类 * @author Administrator * */public class InterfaceAImpl implements InterfaceA {public String methodA(String argsA, String argsB) {System.out.println("have two args: " + argsA + " and " + argsB);String result = "have a test";return result;}}

3. 定义个通知,实现MethodInterceptor
import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;import org.apache.log4j.Logger;/** * 定义个拦截器,实现功能: * 1. 获取参数入参,并拿到执行结果,最后记录日志 * @author Administrator * */public class TestInterceptor implements MethodInterceptor {private static Logger logger =  Logger.getLogger(TestInterceptor.class); /** * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation) */ public Object invoke(MethodInvocation invocation) throws Throwable {String methodinfo = invocation.getMethod().getDeclaringClass() + "."+ invocation.getMethod().getName() + "()";Object result=null;try {/* 真正调用方法的过程,这个步骤切记不可以丢掉 */result = invocation.proceed();} finally { StringBuffer sb = new StringBuffer(); sb.append("{" + methodinfo + "}"); sb.append("[result: "); sb.append(result); sb.append("]");  logger.info(sb.toString());} return result;}}


4. 接下来在spring配置文件里面定义切点和切面即可
<beans xmlns="http://www.springframework.org/schema/beans"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xmlns:p="http://www.springframework.org/schema/p"      xmlns:sofa="http://img.alipay.net/dtd/schema/service"      xmlns:context="http://www.springframework.org/schema/context"      xmlns:webflow="http://www.springframework.org/schema/webflow-config"      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd           http://img.alipay.net/dtd/schema/service http://img.alipay.net/dtd/schema/service/sofa-service.xsd           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd           http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd"      default-autowire="byName">            <bean id="beanA" class="xiuzhu.testaop.InterfaceAImpl"/>                  <bean id="myInterceptor" class="xiuzhu.testaop.TestInterceptor"/><bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"><property name="beanNames"><list><value>beanA</value></list></property><property name="interceptorNames"><list><value>myInterceptor</value></list></property></bean>      </beans>  

至此,就全部结束了,当然这里日志我只写了记录method的名称和返回结果,其实如果作为日志,还可以记录很多别的东西,例如接口耗时,入参,这些都是可以在拦截器里面去做的。


0 1
原创粉丝点击