基于JDK动态代理实现拦截器
1.目标类接口:
package net.seehope.springioc.proxy;
public interface Targe {
public void sayHallo();
}
2.目标类:
public class TargeImpl implements Targe{
public void sayHallo() {
System.out.println("我是明星我很大牌");
}
}
public interface Interceptor {
/**
*
* @param target 目标类
* @param proxy 代理类
* @param method 方法
* @param ags 方法参数
* @return
*/
public boolean before(Object target,Object proxy,Method method,Object[] ags);
/**
*
* @param target 目标类
* @param proxy 代理类
* @param method 方法
* @param ags 方法参数
* @return
*/
public void around(Object target,Object proxy,Method method,Object[] ags);
/**
*
* @param target 目标类
* @param proxy 代理类
* @param method 方法
* @param ags 方法参数
* @return
*/
public void after(Object target,Object proxy,Method method,Object[] ags);
}
4.拦截器实现类:
拦截器1:
package net.seehope.springioc.proxy;
import java.lang.reflect.Method;
public class TargetInterceptor implements Interceptor{
public boolean before(Object target, Object proxy, Method method, Object[] ags) {
System.out.println("前置拦截-- 1");
return false;
}
public void around(Object target, Object proxy, Method method, Object[] ags) {
System.out.println("环绕拦截 -- 1");
}
public void after(Object target, Object proxy, Method method, Object[] ags) {
System.out.println("后置拦截 -- 1");
}
}
拦截器2:
import java.lang.reflect.Method;
public class TargetInterceptor2 implements Interceptor{
public boolean before(Object target, Object proxy, Method method, Object[] ags) {
System.out.println("前置拦截--2");
return true;
}
public void around(Object target, Object proxy, Method method, Object[] ags) {
// TODO Auto-generated method stub
System.out.println("环绕拦截--2");
}
public void after(Object target, Object proxy, Method method, Object[] ags) {
// TODO Auto-generated method stub
System.out.println("后置拦截 --2");
}
}
5、jdk动态代理的代码实现
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class JDKProxyInterceptor implements InvocationHandler{
//目标类
private Object target = null;
//拦截器全限定名
private String interceptorClass = null;
public JDKProxyInterceptor() {
super();
// TODO Auto-generated constructor stub
}
public JDKProxyInterceptor(Object target, String interceptorClass) {
super();
this.target = target;
this.interceptorClass = interceptorClass;
}
public Object bind(Object target,String interceptorClass){
return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new JDKProxyInterceptor(target,interceptorClass));
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if(interceptorClass == null){
System.out.println("不需要拦截器");
return method.invoke(target, args);
}
Object result = null;
Interceptor interceptor = (Interceptor) Class.forName(interceptorClass).newInstance();
if(interceptor.before(target, proxy, method, args)){
result = method.invoke(target, args);
}else{
interceptor.around(target, proxy, method, args);
}
interceptor.after(target, proxy, method, args);
return result;
}
}
6.测试类:
import org.junit.Test;
public class ProxyText {
@Test
public void JdkProxyText(){
JDKProxyInterceptor example = new JDKProxyInterceptor();
Targe proxyTarge = (Targe) example.bind(new TargeImpl(), "net.seehope.springioc.proxy.TargetInterceptor");
Targe proxyTarge2 = (Targe) example.bind(proxyTarge, "net.seehope.springioc.proxy.TargetInterceptor2");
proxyTarge2.sayHallo();
}
}
结果分析:
首先,传入了目标类和拦截器1地址生成了代理类protyTarge,然后再将protyTarge和拦截器2地址生成代理类protyTarge2,protyTarge2的代理增强方法invoke()判断拦截器2before()返回true, 执行 代理类protyTarge.invoke(), protyTarge的invoke()判断拦截器1.before()返回false,执行拦截器1.around(),然后执行拦截器1.after(),返回目标类执行结果,拦截器2执行after()方法,返回目标类执行结果
CGlib代理实现拦截器与JDK实现类似,不再做专门介绍