一、前言
基于aopalliance提供AOP的拦截处理机制定义依赖包(aopalliance-1.0.jar下载)来自定义处理拦截器(org.aopalliance.intercept.MethodInterceptor),用于实现面向切面的编程比如事务日志、通用逻辑方法及复杂对象化过程的解耦等
二、代码示例
1.自定义MyMethodInterceptor拦截器
import org.aopalliance.intercept.MethodInterceptor;@b@import org.aopalliance.intercept.MethodInvocation;@b@import org.springframework.stereotype.Component;@b@@b@@Component@b@public class MyMethodInterceptor implements MethodInterceptor {@b@@b@	@Override@b@	public Object invoke(MethodInvocation invocation) throws Throwable {@b@			Object[] object = invocation.getArguments();@b@		 try{@b@		    System.out.println("信息:["+this.getClass().getName()+"] 拦截器准备执行...");@b@		   @b@		    Object returnObject = invocation.proceed();@b@		   @b@		    System.out.println("信息:["+this.getClass().getName()+"] 拦截器执行完成! ");@b@		   @b@		    return returnObject;@b@		   }catch(Throwable throwable){@b@		    throwable.printStackTrace();@b@	     }@b@		   @b@		   return object;@b@@b@	}@b@	@b@}2. 用户服务层代码IUserService及实现类
public interface IUserService {@b@	@b@   void  login() throws Exception;@b@@b@}import org.springframework.stereotype.Service;@b@@b@@Service@b@public class UserServiceImpl implements IUserService {@b@@b@	@Override@b@	public void login() throws Exception {@b@		System.out.println("用户登录成功!");@b@	}@b@	@b@}3. spring配置文件
<?xml version="1.0" encoding="UTF-8"?>@b@<beans xmlns="http://www.springframework.org/schema/beans"@b@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"@b@ xmlns:context="http://www.springframework.org/schema/context"@b@ xsi:schemaLocation="@b@ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd@b@ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">@b@ @b@ <!-- 自动扫描Action加入Spring上下文 Scope:prototype -->@b@ <context:component-scan base-package="com.xwood.test" scope-resolver="org.springframework.context.annotation.Jsr330ScopeMetadataResolver"/>@b@ @b@ <bean id="userService" class="org.springframework.aop.framework.ProxyFactoryBean" >@b@ <property name="target">@b@ <ref bean="userServiceImpl" />@b@ </property>@b@ @b@ <property name="interceptorNames">@b@ <list>@b@ <value>myMethodInterceptor</value>@b@ </list>@b@ </property>@b@ @b@ </bean> @b@ @b@</beans>
4. 测试用例类SpringMethodInterceptorTest
import org.junit.Test;@b@import org.springframework.beans.factory.annotation.Autowired;@b@import org.springframework.test.context.ContextConfiguration;@b@import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;@b@@b@@ContextConfiguration(locations={"classpath*:spring-*.xml"})@b@public class SpringMethodInterceptorTest extends  AbstractJUnit4SpringContextTests {@b@	@b@	@Autowired@b@	private  IUserService userService;@b@	@b@	@Test@b@    public  void   test() throws Exception{@b@		userService.login();@b@    }@b@@b@}控制台日志
信息:[com.xwood.test.interceptor.MyMethodInterceptor] 拦截器准备执行...@b@用户登录成功!@b@信息:[com.xwood.test.interceptor.MyMethodInterceptor] 拦截器执行完成!