等有空的时候再加上讲解吧,先贴上代码。
1、拦截器Handle类:
package com.shansun.interceptor.test.handler;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import com.shansun.interceptor.test.core.InterceptorAdvisor;
/**
* @author lanbo.xj
*/
public class InterceptorHandler extends AbstractAutoProxyCreator implements ApplicationContextAware {
private static final long serialVersionUID = 8822145972561113575L;
private static ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext aApplicationcontext)
throws BeansException {
applicationContext = aApplicationcontext;
}
public static Object getBean(String beanName) {
return applicationContext.getBean(beanName);
}
@Override
protected Object[] getAdvicesAndAdvisorsForBean(@SuppressWarnings("rawtypes") Class beanClass, String beanName,
TargetSource targetSource) throws BeansException {
if(beanName.equals("springLoader")) return null;
return new InterceptorAdvisor[] { new InterceptorAdvisor(beanName) };
}
}
2、拦截器:在每个方法前后打印一条信息
public class InterceptorAdvisor implements Advisor {
Advice advice = null;
public InterceptorAdvisor(String beanName) {
advice = new InterceptorRoundAdvice(beanName);
}
public Advice getAdvice() {
return advice;
}
public boolean isPerInstance() {
return false;
}
}
3、方法调用包裹类
public class InterceptorRoundAdvice implements MethodInterceptor, Advice {
/**
* 被拦截的BEAN的名称
*/
private String beanName;
public InterceptorRoundAdvice(String aBeanName) {
this.beanName = aBeanName;
}
public Object invoke(MethodInvocation invocation) throws Throwable {
String methodName = this.beanName + "."
+ invocation.getMethod().getName();
// 获取当前调用方法的参数
Object[] arguments = invocation.getArguments();
System.out.println("参数:" + getArguments(arguments));
System.out.println(methodName + ": Before invocation!");
Object result = invocation.proceed();
System.out.println(methodName + ": After invocation!");
return result;
}
private String getArguments (Object[] arguments) {
StringBuilder sb = new StringBuilder();
for(Object arg : arguments) {
sb.append("Type:" + arg.getClass().getSimpleName() + " Value:" + arg.toString());
}
return sb.toString();
}
}
4、工程POM配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>InterceptorTest</groupId> <artifactId>InterceptorTest</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.4</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.1_3</version> </dependency> </dependencies> </project>
5、测试代码
public interface BusinessInterface {
public void doSomething();
}
public class BusinessClass implements BusinessInterface {
public void doSomething() {
System.out.println("业务组件BusinessClass方法调用:doSomething()");
}
}
public class SpringLoader implements ApplicationContextAware{
private BusinessInterface businessInterface;
private ApplicationContext applicationContext;
public void setBusinessInterface(BusinessInterface businessInterface) {
this.businessInterface = businessInterface;
}
public void doSth() {
if(businessInterface == null) {
this.businessInterface = (BusinessInterface) applicationContext.getBean("businessInterface");
}
businessInterface.doSomething();
}
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
}
public static void main(String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"bean.xml ");
SpringLoader sl = (SpringLoader) ctx.getBean("springLoader");
sl.doSth();
}
Spring配置文件:
<?xml version="1.0" encoding="GB2312"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <bean id="businessInterface" class="com.shansun.interceptor.test.busi.impl.BusinessClass"> </bean> <bean id="interceptorHandler" class="com.shansun.interceptor.test.handler.InterceptorHandler"> </bean> <bean id="springLoader" class="com.shansun.interceptor.test.SpringLoader"> </bean> </beans>
测试结果:
参数:
businessInterface.doSomething: Before invocation!
业务组件BusinessClass方法调用:doSomething()
businessInterface.doSomething: After invocation!