专栏名称: ImportNew
伯乐在线旗下账号,专注Java技术分享,包括Java基础技术、进阶技能、架构设计和Java技术领域动态等。
目录
相关文章推荐
芋道源码  ·  监控系统选型,一篇全搞定! ·  18 小时前  
芋道源码  ·  如何应对消息堆积? ·  昨天  
芋道源码  ·  为了DDD 熬夜撸了一套 IDEA 插件) ·  2 天前  
芋道源码  ·  Hutool中的这些工具类,太实用了! ·  2 天前  
51好读  ›  专栏  ›  ImportNew

深入 Spring Boot: 怎样排查 java.lang.ArrayStoreException

ImportNew  · 公众号  · Java  · 2018-03-12 12:00

正文

(点击 上方公众号 ,可快速关注)


来源:hengyunabc,

blog.csdn.net/hengyunabc/article/details/79475505


java.lang.ArrayStoreException 分析


这个demo来说明怎样排查一个spring boot 1应用升级到spring boot 2时可能出现的java.lang.ArrayStoreException。


demo地址:


https://github.com/hengyunabc/spring-boot-inside/tree/master/demo-ArrayStoreException


demo里有两个模块,springboot1-starter和springboot2-demo。


在springboot1-starter模块里,是一个简单的HealthIndicator实现


public class MyHealthIndicator extends AbstractHealthIndicator {

@Override

protected void doHealthCheck(Builder builder) throws Exception {

builder.status(Status.UP);

builder.withDetail("hello", "world");

}

}


@Configuration

@AutoConfigureBefore(EndpointAutoConfiguration.class)

@AutoConfigureAfter(HealthIndicatorAutoConfiguration.class)

@ConditionalOnClass(value = { HealthIndicator.class })

public class MyHealthIndicatorAutoConfiguration {

@Bean

@ConditionalOnMissingBean(MyHealthIndicator.class)

@ConditionalOnEnabledHealthIndicator("my")

public MyHealthIndicator myHealthIndicator() {

return new MyHealthIndicator();

}

}


springboot2-demo则是一个简单的spring boot2应用,引用了springboot1-starter模块。


把工程导入IDE,执行springboot2-demo里的ArrayStoreExceptionDemoApplication,抛出的异常是


Caused by: java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy

at sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:724) ~[na:1.8.0_112]

at sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:531) ~[na:1.8.0_112]

at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:355) ~[na:1.8.0_112]

at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:286) ~[na:1.8.0_112]

at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:120) ~[na:1.8.0_112]

at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:72) ~[na:1.8.0_112]

at java.lang.Class.createAnnotationData(Class.java:3521) ~[na:1.8.0_112]

at java.lang.Class.annotationData(Class.java:3510) ~[na:1.8.0_112]

at java.lang.Class.createAnnotationData(Class.java:3526) ~[na:1.8.0_112]

at java.lang.Class.annotationData(Class.java:3510) ~[na:1.8.0_112]

at java.lang.Class.getAnnotation(Class.java:3415) ~[na:1.8.0_112]

at java.lang.reflect.AnnotatedElement.isAnnotationPresent(AnnotatedElement.java:258) ~[na:1.8.0_112]

at java.lang.Class.isAnnotationPresent(Class.java:3425) ~[na:1.8.0_112]

at org.springframework.core.annotation.AnnotatedElementUtils.hasAnnotation(AnnotatedElementUtils.java:575) ~[spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.isHandler(RequestMappingHandlerMapping.java:177) ~[spring-webmvc-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:217) ~[spring-webmvc-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:188) ~[spring-webmvc-5.0.4.RELEASE.jar:5.0.4.RELEASE]

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 , Set ) 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) {







请到「今天看啥」查看全文