问题:Caused by: net.sf.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
描述:很奇怪我用的是FastClass,当项目刚启动的时候,第一次调用会抛出这个异常,但是以后的都正常调用了,很奇怪,我用jdk反射就不会出现这个问题。
代码:
cglib:
@SuppressWarnings("unchecked") protected <T> T execute(String method, Object service,Object paramter,T retrunType) throws Exception { FastMethod serviceFastMethod = getDeclaredMethod(method,service,new Class[]{paramter.getClass()}); return (T) serviceFastMethod.invoke(service, new Object[]{paramter}); } public FastMethod getDeclaredMethod(String method,Object object, Class<?> ... parameterTypes){ FastMethod serviceFastMethod = null; for(Class<?> clazz = object.getClass() ; clazz != Object.class ; clazz = clazz.getSuperclass()){ try { FastClass serviceFastClass = FastClass.create(clazz); return serviceFastClass.getMethod(method, parameterTypes); } catch(Error suchMethodError){ LOG.error("invoke method {} ,cause {} ,object {}",new Object[]{method,suchMethodError.getMessage(),clazz.getName()}); continue; } } return serviceFastMethod; }
jdk :
@SuppressWarnings("unchecked") protected <T> T executeJDK(String method, Object service, Object paramter,T retrunType) throws Exception { Method clzMethod = getDeclaredMethodJDK(method,service,new Class[]{paramter.getClass()}); return (T) clzMethod.invoke(service,paramter); } private Method getDeclaredMethodJDK(String method, Object service,Class<?> ... parameterTypes) { Method jdkMethod = null; for(Class<?> clazz = service.getClass() ; clazz != Object.class ; clazz = clazz.getSuperclass()){ try { return clazz.getDeclaredMethod(method, parameterTypes); } catch (Exception e) { LOG.error("invoke method {} ,cause {} ,object {}",new Object[]{method,e.getMessage(),clazz.getName()}); continue; } } return jdkMethod; }问题分析:
根据跟踪代码发现,FassClass.create()会把代理的类缓存到内存中,来提高效率。spring-core已经合并了cglib功能。此时我的项目中引用的是cglib 3.2.0 ,导致spring-core中和cglib3.2.0版本冲突。去掉cglib3.2.0版本包就可以解决问题。