問題: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版本包就可以解決問題。