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

本系列文章均整理自我在先前一家公司的CGLib使用总结和笔记。分享出来,希望对看到的人有所帮助,同时欢迎大家提出宝贵意见大笑。如需转载,请勿修改,且注明作者shensy及出处。

--------------------------------------

实战CGLib系列文章

本篇介绍回调过滤器CallbackFilter。

一、作用

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

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

二、示例

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

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接口的类:
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[]中的位置索引(见下文)。

生成代理类

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类实现如下:

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,该方法返回值为整型)。

下面进行验证

//接上文...
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);
控制台输出
*** 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回调过滤的一个示例,本系列下一篇将继续介绍CGLib的强大功能,敬请期待。

猜你喜欢

转载自shensy.iteye.com/blog/1881130