关于cglib的动态代理
有个A对象,我用动态代理对A生成A的代理对象A1
现在有这个需求,我想继续对A1,进行代理生成A2怎么做?
如下方式直接报错:、
public class DynamicProxy implements MethodInterceptor {
private static DynamicProxy dynamicProxy = new DynamicProxy();
public static DynamicProxy getInstance() {
return dynamicProxy;
}
public <T> T getProxy(Class<T> cls) {
return (T)Enhancer.create(cls,this);
}
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
before();
Object result = methodProxy.invokeSuper(o, objects);
after();
return result;
}
private void before() {
System.out.println("before");
}
private void after() {
System.out.println("after");
}
public static void main(String[] args) {
Object parent1 = DynamicProxy.getInstance().getProxy(Parent.class);
Object parent2 = DynamicProxy.getInstance().getProxy(parent1.getClass());
}
}
Exception in thread "main" net.sf.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:237)
at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:285)
at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:663)
at com.kaze.test.DynamicProxy.getProxy(DynamicProxy.java:19)
at com.kaze.test.DynamicProxy.main(DynamicProxy.java:39)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at net.sf.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:384)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:219)
... 5 more
Caused by: java.lang.ClassFormatError: Duplicate method name&signature in class file com/kaze/test/Parent$$EnhancerByCGLIB$$2795b80$$EnhancerByCGLIB$$da721171
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
... 11 more