基础类
public class HelloWorld{
public void sayHello(String name) {
System.err.println(name+":Hello World~");
}
}
代理类
public class Proxy_CGLib implements MethodInterceptor{
private Object target;
public Object getInstance(Object target){
this.target=target;
Enhancer enhancer =new Enhancer();
enhancer.setSuperclass(this.target.getClass());
enhancer.setCallback(this);
return enhancer.create();
}
@Override
//回调方法
public Object intercept(Object obj, Method method, Object[] args,
MethodProxy methodProxy) throws Throwable {
Object returnObj = methodProxy.invokeSuper(obj, args);
return returnObj;
}
}
测试
public class CGLibTest {
public static void main(String[] args) {
Proxy_CGLib cglib=new Proxy_CGLib();
HelloWorld helloService =(HelloWorld) cglib.getInstance(new HelloWorld());
helloService.sayHello("yw");
}
}
报错信息
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.yan.cglib.Proxy_CGLib.getInstance(Proxy_CGLib.java:15) at com.yan.cglib.CGLibTest.main(CGLibTest.java:6)
Caused by: java.lang.IllegalStateException: Unable to load cache item
使用jar包
cglib-3.2.4.jar
asm-3.3.1.jar
各位大神,请问问题出在哪?
遇到了同样的问题。通过修改cglib的jar包版本为2.2.2解决。