spring Aop MethodInterceptor接口

 

前言

关于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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
org.aopalliance.aop是一个Java领域的开源项目,是AspectJ和其他AOP框架之间的一个接口标准化项目。AOP(面向切面编程)是一种编程范式,它允许开发人员将通用的业务逻辑与应用程序的特定功能进行分离,从而提高代码的可重用性和可维护性。 org.aopalliance.aop项目的主要目的是定义一组接口,用于描述AOP框架的核心概念,例如切面(Aspect)、连接点(Join point)和通知(Advice)等。这些接口提供了一种标准的方式来表示和操作切面逻辑,从而使不同的AOP框架之间能够互相兼容。 要下载org.aopalliance.aop项目,可以访问其官方网站或开源代码托管平台,如GitHub等。从官方网站下载通常会提供编译好的二进制文件(JAR包)以及相关的文档。如果需要查看或参与开发,也可以访问项目的代码托管平台获取源代码。 下载org.aopalliance.aop项目后,可以将其导入Java开发环境,并添加到自己的项目依赖中。通过使用org.aopalliance.aop提供的接口,开发人员可以轻松地定义和应用切面逻辑,以实现应用程序中的横切关注点(cross-cutting concerns)。这样可以大大简化代码,提高系统的灵活性和可重用性。 总之,org.aopalliance.aop项目是一个重要的AOP接口标准化项目,通过定义一组接口,实现不同AOP框架之间的兼容性,从而帮助开发人员更加方便地使用AOP编程范式,提高代码质量和开发效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值