SE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:129) ~[spring-webmvc-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1769) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1706) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
... 16 common frames omitted
使用 Java Exception Breakpoint
下面来排查这个问题。
在IDE里,新建一个断点,类型是Java Exception Breakpoint
(如果不清楚怎么添加,可以搜索对应IDE的使用文档),异常类是上面抛出来的java.lang.ArrayStoreException
。
当断点起效时,查看AnnotationUtils.findAnnotation(Class<?>, Class<A>, Set<Annotation>) line: 686
函数的参数。
可以发现
- clazz是
class com.example.springboot1starter.MyHealthIndicatorAutoConfiguration$$EnhancerBySpringCGLIB$$945c1f
- annotationType是
interface org.springframework.boot.actuate.endpoint.annotation.Endpoint
说明是尝试从MyHealthIndicatorAutoConfiguration
里查找@Endpoint
信息时出错的。
MyHealthIndicatorAutoConfiguration
上的确没有@Endpoint
,但是为什么抛出java.lang.ArrayStoreException
?
尝试以简单例子复现异常
首先尝试直接 new MyHealthIndicatorAutoConfiguration :
public static void main(String[] args) {
MyHealthIndicatorAutoConfiguration cc = new MyHealthIndicatorAutoConfiguration();
}
本以为会抛出异常来,但是发现执行正常。
再仔细看异常栈,可以发现是在at java.lang.Class.getDeclaredAnnotation(Class.java:3458)
抛出的异常,则再尝试下面的代码:
public static void main(String[] args) {
MyHealthIndicatorAutoConfiguration.class.getDeclaredAnnotation(Endpoint.class);
}
发现可以复现异常了:
Exception in thread "main" java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
at sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:724)
at sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:531)
at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:355)
at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:286)
at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:120)
at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:72)
at java.lang.Class.createAnnotationData(Class.java:3521)
at java.lang.Class.annotationData(Class.java:3510)
at java.lang.Class.getDeclaredAnnotation(Class.java:3458)
为什么会是java.lang.ArrayStoreException
再仔细看异常信息:java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
ArrayStoreException
是一个数组越界的异常,它只有一个String信息,并没有cause
。
那么我们尝试在 sun.reflect.annotation.TypeNotPresentExceptionProxy
的构造函数里打断点。
public class TypeNotPresentExceptionProxy extends ExceptionProxy {
private static final long serialVersionUID = 5565925172427947573L;
String typeName;
Throwable cause;
public TypeNotPresentExceptionProxy(String typeName, Throwable cause) {
this.typeName = typeName;
this.cause = cause;
}
在断点里,我们可以发现: