rg.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration
cause是 java.lang.ClassNotFoundException: org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration
终于真相大白了,是找不到org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration
这个类。
那么它是怎么变成ArrayStoreException
的呢?
仔细看下代码,可以发现AnnotationParser.parseClassValue
把异常包装成为Object
//sun.reflect.annotation.AnnotationParser.parseClassValue(ByteBuffer, ConstantPool, Class<?>)
private static Object parseClassValue(ByteBuffer buf,
ConstantPool constPool,
Class<?> container) {
int classIndex = buf.getShort() & 0xFFFF;
try {
try {
String sig = constPool.getUTF8At(classIndex);
return parseSig(sig, container);
} catch (IllegalArgumentException ex) {
// support obsolete early jsr175 format class files
return constPool.getClassAt(classIndex);
}
} catch (NoClassDefFoundError e) {
return new TypeNotPresentExceptionProxy("[unknown]", e);
}
catch (TypeNotPresentException e) {
return new TypeNotPresentExceptionProxy(e.typeName(), e.getCause());
}
}
然后在sun.reflect.annotation.AnnotationParser.parseClassArray(int, ByteBuffer, ConstantPool, Class<?>)
里尝试直接设置到数组里
// sun.reflect.annotation.AnnotationParser.parseClassArray(int, ByteBuffer, ConstantPool, Class<?>)
result[i] = parseClassValue(buf, constPool, container);
而这里数组越界了,ArrayStoreException
只有越界的Object
的类型信息,也就是上面的
java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
解决问题
发现是java.lang.ClassNotFoundException: org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration
,则加上@ConditionalOnClass
的检查就可以了:
@Configuration
@AutoConfigureBefore(EndpointAutoConfiguration.class)
@AutoConfigureAfter(HealthIndicatorAutoConfiguration.class)
@ConditionalOnClass(value = {HealthIndicator.class, EndpointAutoConfiguration.class})
public class MyHealthIndicatorAutoConfiguration {
准确来说是spring boot2把一些类的package改了:
spring boot 1里类名是:
- org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration
spring boot 2里类名是:
- org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration
总结