实战CGLib系列之proxy篇(二):回调过滤CallbackFilter

作者shensy及出处http://shensy.iteye.com/blog/1881130

 

实战CGLib系列文章

本篇介绍回调过滤器CallbackFilter。

一、作用

在CGLib回调时可以设置对不同方法执行不同的回调逻辑,或者根本不执行回调。

在JDK动态代理中并没有类似的功能,对InvocationHandler接口方法的调用对代理类内的所以方法都有效。

二、示例

首先定义一个被代理类,包含3个方法:

Java代码 复制代码  收藏代码
  1. public class ConcreteClassNoInterface {   
  2.     public String getConcreteMethodA(String str){   
  3.         System.out.println("ConcreteMethod A ... "+str);   
  4.         return str;   
  5.     }   
  6.     public int getConcreteMethodB(int n){   
  7.         System.out.println("ConcreteMethod B ... "+n);   
  8.         return n+10;   
  9.     }   
  10.     public int getConcreteMethodFixedValue(int n){   
  11.         System.out.println("getConcreteMethodFixedValue..."+n);   
  12.         return n+10;   
  13.     }   
  14. }  
public class ConcreteClassNoInterface {
	public String getConcreteMethodA(String str){
		System.out.println("ConcreteMethod A ... "+str);
		return str;
	}
	public int getConcreteMethodB(int n){
		System.out.println("ConcreteMethod B ... "+n);
		return n+10;
	}
	public int getConcreteMethodFixedValue(int n){
		System.out.println("getConcreteMethodFixedValue..."+n);
		return n+10;
	}
}

其次,定义实现过滤器CallbackFilter接口的类:

Java代码 复制代码  收藏代码
  1. public class ConcreteClassCallbackFilter implements CallbackFilter{   
  2.     public int accept(Method method) {   
  3.         if("getConcreteMethodB".equals(method.getName())){   
  4.             return 0;//Callback callbacks[0]   
  5.         }else if("getConcreteMethodA".equals(method.getName())){   
  6.             return 1;//Callback callbacks[1]   
  7.         }else if("getConcreteMethodFixedValue".equals(method.getName())){   
  8.             return 2;//Callback callbacks[2]   
  9.         }   
  10.         return 1;   
  11.     }   
  12. }  
public class ConcreteClassCallbackFilter implements CallbackFilter{
	public int accept(Method method) {
		if("getConcreteMethodB".equals(method.getName())){
			return 0;//Callback callbacks[0]
		}else if("getConcreteMethodA".equals(method.getName())){
			return 1;//Callback callbacks[1]
		}else if("getConcreteMethodFixedValue".equals(method.getName())){
			return 2;//Callback callbacks[2]
		}
		return 1;
	}
}

其中return值为被代理类的各个方法在回调数组Callback[]中的位置索引(见下文)。

生成代理类

Java代码 复制代码  收藏代码
  1. Enhancer enhancer=new Enhancer();   
  2. enhancer.setSuperclass(ConcreteClassNoInterface.class);   
  3. CallbackFilter filter=new ConcreteClassCallbackFilter();   
  4. enhancer.setCallbackFilter(filter);   
  5.   
  6. Callback interceptor=new ConcreteClassInterceptor();//(1)   
  7. Callback noOp=NoOp.INSTANCE;//(2)   
  8. Callback fixedValue=new ConcreteClassFixedValue();//(3)   
  9. Callback[] callbacks=new Callback[]{interceptor,noOp,fixedValue};   
  10. enhancer.setCallbacks(callbacks);   
  11. ConcreteClassNoInterface proxyObject=(ConcreteClassNoInterface)enhancer.create();   
  12. //...见下文  
Enhancer enhancer=new Enhancer();
enhancer.setSuperclass(ConcreteClassNoInterface.class);
CallbackFilter filter=new ConcreteClassCallbackFilter();
enhancer.setCallbackFilter(filter);

Callback interceptor=new ConcreteClassInterceptor();//(1)
Callback noOp=NoOp.INSTANCE;//(2)
Callback fixedValue=new ConcreteClassFixedValue();//(3)
Callback[] callbacks=new Callback[]{interceptor,noOp,fixedValue};
enhancer.setCallbacks(callbacks);
ConcreteClassNoInterface proxyObject=(ConcreteClassNoInterface)enhancer.create();
//...见下文

生成代理类前,设置了CallbackFilter,上文中ConcreteClassCallbackFilter实现类的返回值对应Callback[]数组中的位置索引。此处包含了CGLib中的3种回调方式:

(1)MethodInterceptor:方法拦截器,上一篇文章中已经详细介绍过,此处不再赘述。

(2)NoOp.INSTANCE:这个NoOp表示no operator,即什么操作也不做,代理类直接调用被代理的方法不进行拦截。

(3)FixedValue:表示锁定方法返回值,无论被代理类的方法返回什么值,回调方法都返回固定值。

其中,ConcreteClassFixedValue类实现如下:

Java代码 复制代码  收藏代码
  1. public class ConcreteClassFixedValue implements FixedValue{   
  2.     public Object loadObject() throws Exception {   
  3.         System.out.println("ConcreteClassFixedValue loadObject ...");   
  4.         Object object=999;   
  5.         return object;   
  6.     }   
  7. }  
public class ConcreteClassFixedValue implements FixedValue{
	public Object loadObject() throws Exception {
		System.out.println("ConcreteClassFixedValue loadObject ...");
		Object object=999;
		return object;
	}
}

该类实现FixedValue接口,同时锁定回调值为999(整型,CallbackFilter中定义的使用FixedValue型回调的方法为getConcreteMethodFixedValue,该方法返回值为整型)。

下面进行验证

Java代码 复制代码  收藏代码
  1. //接上文...   
  2. System.out.println("*** NoOp Callback ***");   
  3. proxyObject.getConcreteMethodA("abcde");   
  4.   
  5. System.out.println("*** MethodInterceptor Callback ***");   
  6. proxyObject.getConcreteMethodB(1);   
  7.   
  8. System.out.println("*** FixedValue Callback ***");   
  9. int fixed1=proxyObject.getConcreteMethodFixedValue(128);   
  10. System.out.println("fixedValue1:"+fixed1);   
  11. int fixed2=proxyObject.getConcreteMethodFixedValue(256);   
  12. System.out.println("fixedValue2:"+fixed2);  
//接上文...
System.out.println("*** NoOp Callback ***");
proxyObject.getConcreteMethodA("abcde");

System.out.println("*** MethodInterceptor Callback ***");
proxyObject.getConcreteMethodB(1);

System.out.println("*** FixedValue Callback ***");
int fixed1=proxyObject.getConcreteMethodFixedValue(128);
System.out.println("fixedValue1:"+fixed1);
int fixed2=proxyObject.getConcreteMethodFixedValue(256);
System.out.println("fixedValue2:"+fixed2);

控制台输出

控制台代码 复制代码  收藏代码
  1. *** NoOp Callback ***   
  2. ConcreteMethod A ... abcde   
  3.   
  4. *** MethodInterceptor Callback ***   
  5. Before :public int generic.cglib.proxy.ConcreteClassNoInterface.getConcreteMethodB(int)   
  6. ConcreteMethod B ... 1  
  7. After :public int generic.cglib.proxy.ConcreteClassNoInterface.getConcreteMethodB(int)   
  8. ConcreteClassInterceptor end...   
  9.   
  10. *** FixedValue Callback ***   
  11. ConcreteClassFixedValue loadObject ...   
  12. fixedValue1:999  
  13. ConcreteClassFixedValue loadObject ...   
  14. fixedValue2:999  
*** NoOp Callback ***
ConcreteMethod A ... abcde

*** MethodInterceptor Callback ***
Before :public int generic.cglib.proxy.ConcreteClassNoInterface.getConcreteMethodB(int)
ConcreteMethod B ... 1
After :public int generic.cglib.proxy.ConcreteClassNoInterface.getConcreteMethodB(int)
ConcreteClassInterceptor end...

*** FixedValue Callback ***
ConcreteClassFixedValue loadObject ...
fixedValue1:999
ConcreteClassFixedValue loadObject ...
fixedValue2:999

getConcreteMethodA对应CallbackFilter中定义的索引1,在Callback[]数组中使用的过滤为NoOp,因此直接执行了被代理方法。

getConcreteMethodB对应CallbackFilter中定义的索引0,在Callback[]数组中使用的过滤为MethodInterceptor,因此执行了方法拦截器进行拦截。

getConcreteMethodFixedValue对应CallbackFilter中定义的索引2,在Callback[]数组中使用的过滤为FixedValue,因此2次赋值128和256的调用其结果均被锁定为返回999。

你可能感兴趣的:(实战CGLib系列之proxy篇(二):回调过滤CallbackFilter)