关于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
现在有这个需求,我想继续对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
...全文
相关推荐
Spring框架中JDK动态代理和cglib动态代理
Spring框架的AOP中重要的一个知识点,动态代理,springAOP框架会根绝实际情况选择使用jdk的动态代理还是cglib的动态代理
AOP之JDK动态代理和CGLib动态代理
AOP之JDK动态代理和CGLib动态代理 ,具体效果和过程看博文 http://blog.csdn.net/evankaka/article/details/45195383
浅谈JDK动态代理与CGLIB代理去区别
NULL 博文链接:https://jummy.iteye.com/blog/255628
Cglib动态代理需要用到的jar包
Cglib动态代理需要用到的jar包 http://www.cnblogs.com/mfrank/p/8150925.html
jdk与cglib动态代理与底层实现
代理模式详解-jdk与cglib动态代理与底层实现,spring中常用的设计模式,本案例是从源码到代理模式的实现。


Web 开发
