下面的代码:如何使用cglib双重增强类?
Patient patient = factory.createPatient();
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(patient.getClass());
enhancer.setCallback(new DefaultMethodInterceptor(patient));
patient = (Patient) enhancer.create();
assertThat(patient.getFirstName()).isNotNull();
Enhancer enhancer2 = new Enhancer();
enhancer2.setSuperclass(patient.getClass());
enhancer2.setCallback(new DefaultMethodInterceptor(patient));
patient = (Patient) enhancer2.create();
assertThat(patient.getFirstName()).isNotNull();
它未能在最后断言与
net.sf.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
...
Caused by: java.lang.reflect.InvocationTargetException
...
Caused by: java.lang.ClassFormatError: Duplicate method name&signature in class file my/package/entity/Patient$$EnhancerByCGLIB$$ca1e6685$$EnhancerByCGLIB$$f52743be
,因为我要增强Hibernate的实体我问这个,但有时它返回由自己和我的第二个已增强的人增强失败。我怎样才能避免这种情况?
谢谢你,这个伎俩。我很难发现哪一个类是原来的类,我最终从它的名字中切割出了$$$ EnhancerByCGLIB $$中的所有内容。 – artemb 2009-08-17 11:43:41