论坛首页 Java企业应用论坛

cglib实现无接口代理

浏览 8439 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (2)
作者 正文
   发表时间:2011-02-13  
OO

  使用情况:有时候我们需要为一个类建立代理对象,当执行原类的某种方法时,进行某些操作,但是这要求我们原来的类实现某种接口。如果原来的类没有实现任何的接口怎么实现代理哪?现在我们可以利用Cglib为任何的类产生代理对象,不管原来的类有没有实现接口,甚至我们可以使代理对象动态的实现任意接口。
  例子
     1、产生带拦截器的代理对象。
     2、产生带拦截器和继承接口的代理对象。
     3、产生带拦截器和拦截器过滤器的代理对象。

  包里的内容

     案例,依赖包。

 

   发表时间:2011-02-16  
System.out.println(proxyObjWithInterfaceAndCallBack instanceof TestInterface);可以打印出true

但是System.out.println(((TestInterface)proxyObjWithInterfaceAndCallBack).getIntValue());却会报错

Exception in thread "main" java.lang.NoSuchMethodError: com.muzi.util.reflect.CglibTest.getIntValue()I
at com.muzi.util.reflect.CglibTest$$EnhancerByCGLIB$$ddc15b7a.CGLIB$getIntValue$7(<generated>)
at com.muzi.util.reflect.CglibTest$$EnhancerByCGLIB$$ddc15b7a$$FastClassByCGLIB$$d4e392b.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:215)
at com.muzi.util.reflect.MethodInterceptorImpl1.intercept(MethodInterceptorImpl1.java:21)
at com.muzi.util.reflect.CglibTest$$EnhancerByCGLIB$$ddc15b7a.getIntValue(<generated>)
at com.muzi.util.reflect.CglibTest.main(CglibTest.java:34)
0 请登录后投票
   发表时间:2011-02-16  
zuiyanwangyue 写道
System.out.println(proxyObjWithInterfaceAndCallBack instanceof TestInterface);可以打印出true

但是System.out.println(((TestInterface)proxyObjWithInterfaceAndCallBack).getIntValue());却会报错

Exception in thread "main" java.lang.NoSuchMethodError: com.muzi.util.reflect.CglibTest.getIntValue()I
at com.muzi.util.reflect.CglibTest$$EnhancerByCGLIB$$ddc15b7a.CGLIB$getIntValue$7(<generated>)
at com.muzi.util.reflect.CglibTest$$EnhancerByCGLIB$$ddc15b7a$$FastClassByCGLIB$$d4e392b.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:215)
at com.muzi.util.reflect.MethodInterceptorImpl1.intercept(MethodInterceptorImpl1.java:21)
at com.muzi.util.reflect.CglibTest$$EnhancerByCGLIB$$ddc15b7a.getIntValue(<generated>)
at com.muzi.util.reflect.CglibTest.main(CglibTest.java:34)

因为CglibTest类并没有getIntValue方法所以会报错,所以还要自CglibTest类中加入getIntValue方法,比如
public int getIntValue(){
return 1;
}。
0 请登录后投票
   发表时间:2011-02-16  
Cglib and dynamically proxy are the implementation of Spring IOC.I think proxy patten is a good design in some situation.
0 请登录后投票
   发表时间:2011-02-16  
,不错
		CglibTest proxyObjWithInterfaceAndCallBack=(CglibTest)CglibProxy.getProxyInstance(CglibTest.class,new Class[]{TestInterface.class},new MethodInterceptorImpl1());
		//带拦截器和接口的代理对象。


CglibTest 应该实现 TestInterface 接口吧,不然的话,proxyObjWithInterfaceAndCallBack 调用接口的方法怎么调?
0 请登录后投票
   发表时间:2011-02-16  
pouyang 写道
,不错
		CglibTest proxyObjWithInterfaceAndCallBack=(CglibTest)CglibProxy.getProxyInstance(CglibTest.class,new Class[]{TestInterface.class},new MethodInterceptorImpl1());
		//带拦截器和接口的代理对象。


CglibTest 应该实现 TestInterface 接口吧,不然的话,proxyObjWithInterfaceAndCallBack 调用接口的方法怎么调?

  不需要实现TestInterface,只需要含有接口中方法就行,当然不含有的话也不会报错,只有调用时才报错。
0 请登录后投票
   发表时间:2011-02-16  
azure2a 写道
zuiyanwangyue 写道
System.out.println(proxyObjWithInterfaceAndCallBack instanceof TestInterface);可以打印出true

但是System.out.println(((TestInterface)proxyObjWithInterfaceAndCallBack).getIntValue());却会报错

Exception in thread "main" java.lang.NoSuchMethodError: com.muzi.util.reflect.CglibTest.getIntValue()I
at com.muzi.util.reflect.CglibTest$$EnhancerByCGLIB$$ddc15b7a.CGLIB$getIntValue$7(<generated>)
at com.muzi.util.reflect.CglibTest$$EnhancerByCGLIB$$ddc15b7a$$FastClassByCGLIB$$d4e392b.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:215)
at com.muzi.util.reflect.MethodInterceptorImpl1.intercept(MethodInterceptorImpl1.java:21)
at com.muzi.util.reflect.CglibTest$$EnhancerByCGLIB$$ddc15b7a.getIntValue(<generated>)
at com.muzi.util.reflect.CglibTest.main(CglibTest.java:34)

因为CglibTest类并没有getIntValue方法所以会报错,所以还要自CglibTest类中加入getIntValue方法,比如
public int getIntValue(){
return 1;
}。


我觉得CGLIB的真正威力就是动态代理吧,即使原来的对象不具备某接口中的方法也是可以的,因为CGLIB可以在运行时修改类的定义,个人觉得还是你这个例子做的不是很到位。
0 请登录后投票
   发表时间:2011-02-16  
建议看看Spring拦截器的实现,就是用cglib和java中的proxy类
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics