深入分析Spring Boot2,解决 java.lang.ArrayStoreException异常

在将项目从SpringBoot1升级到SpringBoot2并尝试与Activiti6.0.0集成时,遇到java.lang.ArrayStoreException异常。本文详细分析了问题原因,提供了三种解决方案,包括降级SpringBoot版本、排除特定配置类和修改源码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将某个项目从Spring Boot1升级Spring Boot2之后出现如下报错,查了很多不同的解决方法都没有解决:

Spring boot2项目启动时遇到了异常:

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

Caused by: java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
    at sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:724) ~[na:1.8.0_65]
    at sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:531) ~[na:1.8.0_65]
    at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:355) ~[na:1.8.0_65]
    at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:286) ~[na:1.8.0_65]
    at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:120) ~[na:1.8.0_65]
    at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:72) ~[na:1.8.0_65]
    at java.lang.Class.createAnnotationData(Class.java:3521) ~[na:1.8.0_65]
    at java.lang.Class.annotationData(Class.java:3510) ~[na:1.8.0_65]
    at java.lang.Class.createAnnotationData(Class.java:3526) ~[na:1.8.0_65]
    at java.lang.Class.annotationData(Class.java:3510) ~[na:1.8.0_65]
    at java.lang.Class.getAnnotation(Class.java:3415) ~[na:1.8.0_65]
    at java.lang.reflect.AnnotatedElement.isAnnotationPresent(AnnotatedElement.java:258) ~[na:1.8.0_65]
    at java.lang.Class.isAnnotationPresent(Class.java:3425) ~[na:1.8.0_65]
    at org.springframework.core.annotation.AnnotatedElementUtils.hasAnnotation(AnnotatedElementUtils.java:570) ~[spring-core-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.isHandler(RequestMappingHandlerMapping.java:177) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:218) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:189) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:136) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1758) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1695) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    ... 16 common frames omitted

经过简单排查后,怀疑是因为jar版本冲突引起的异常,使用异常断点:

然后在

 

 

应该是从class org.activiti.spring.boot.SecurityAutoConfiguration出错,然后报错java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy

尝试复现异常:

SecurityAutoConfiguration securityAutoConfiguration=new SecurityAutoConfiguration();

正常

SecurityAutoConfiguration.class.getDeclaredAnnotation(Aspect.class);

异常复现。

然后找到TypeNotPresentExceptionProxy类,使用Ctrl+N/Ctrl+N+N

然后在构造方法中打断点,发现:

发现是cause:DefaultAuthenticationEventPublisher找不到引发的报错。

实际报错是ClassNotFound。

仔细看下代码,可以发现AnnotationParser.parseClassValue把异常包装成为Object。

    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<?>)里尝试直接设置到数组里。

而这里数组越界了,ArrayStoreException只有越界的Object的类型信息,也就是上面的。

解决:

1:将springboot2.0降级为原来的1.X版本

2:在springboot启动类上添加

@SpringBootApplication(exclude = SecurityAutoConfiguration.class)

3:修改源码的集成问题,重新编译

总结:

具体问题还要具体分析,不同的代码引发该问题的原因也不相同。

我的问题是:

springboot2.0不能与activiti6.0.0直接集成使用,因为activiti6.0.0出来的时候springboot2.0还没有出来,activiti6.0.0 支持springboot1.2.6以上,2.0.0以下的版本。

这里实际报错是ClassNotFound。

 

转载于:https://www.cnblogs.com/jiangwz/p/9711998.html

异常描述: java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy 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.reflect.Executable.declaredAnnotations(Executable.java:599) at java.lang.reflect.Executable.declaredAnnotations(Executable.java:597) at java.lang.reflect.Executable.getDeclaredAnnotations(Executable.java:588) at java.lang.reflect.Method.getDeclaredAnnotations(Method.java:630) at org.springframework.core.annotation.AnnotationsScanner.getDeclaredAnnotations(AnnotationsScanner.java:453) at org.springframework.core.annotation.AnnotationsScanner.isKnownEmpty(AnnotationsScanner.java:491) at org.springframework.core.annotation.TypeMappedAnnotations.from(TypeMappedAnnotations.java:251) at org.springframework.core.annotation.MergedAnnotations.from(MergedAnnotations.java:351) at org.springframework.core.annotation.MergedAnnotations.from(MergedAnnotations.java:330) at org.springframework.core.annotation.AnnotatedElementUtils.findAnnotations(AnnotatedElementUtils.java:800) at org.springframework.core.annotation.AnnotatedElementUtils.hasAnnotation(AnnotatedElementUtils.java:541)怎么解决
最新发布
03-28
### 解决方案 `java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy` 是一种常见的错误,通常发生在类加载阶段。这种错误可能由多种原因引起,特别是在复杂的微服务架构中。 #### 原因分析 1. **依赖冲突**:当项目的多个模块引入了不同版本的库时,可能导致某些类无法正常加载[^2]。 2. **字节码增强工具问题**:Spring Cloud 使用 CGLIB 或其他代理技术来实现动态代理功能。如果这些工具未能正确处理目标类,则可能出现此类异常[^3]。 3. **注解解析失败**:在 Spring Boot 启动过程中,框架会尝试解析所有的注解。如果有某个类未被正确加载或者缺失必要的元数据文件(如 `META-INF/spring-configuration-metadata.json`),则可能会抛出此异常[^4]。 4. **测试环境中的特殊行为**:在单元测试 (`@SpringBootTest`) 中运行的应用程序有时会有不同的上下文配置,这可能导致一些生产环境中不会遇到的问题显现出来[^5]。 #### 处理方法 以下是几种可以用来解决问题的方法: 1. **清理并重建项目** 确保所有构建产物都是最新的,并且没有残留旧版编译结果干扰新版本部署。 ```bash mvn clean install -U ``` 2. **排除不必要的自动装配组件** 如果知道哪些默认启用的功能并不需要的话,可以通过设置@SpringBootApplication 的 exclude 属性将其禁用掉。例如: ```java @SpringBootApplication(exclude = {SecurityAutoConfiguration.class, HystrixAutoConfiguration.class}) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 3. **调整依赖管理策略** 统一整个工程内的第三方库版本号,避免混杂不兼容的情况发生。可以在父POM里定义dependencyManagement部分统一指定各子模块使用的具体版本信息。 4. **验证 META-INF 文件完整性** 检查是否有任何重要的资源文件丢失或损坏。特别是那些用于描述自定义注解属性结构的信息表单。 5. **升级至最新稳定发行版** 考虑到早期预览版可能存在较多未知缺陷,建议尽可能采用官方推荐的正式长期支持(LTS)系列作为基础平台搭建依据。 6. **调试模式下深入探查** 开启更详尽的日志记录等级以便于定位确切触发点所在位置以及关联参数详情。 ```properties logging.level.org.springframework=DEBUG ``` 7. **单独隔离影响因素** 尝试逐一移除可疑插件/扩展包直至恢复正常运作状态为止;然后再逐步恢复以确认真正罪魁祸首身份。 --- ### 示例代码片段展示如何合理运用exclude机制规避潜在风险 ```java import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; import org.springframework.cloud.netflix.hystrix.HystrixAutoConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication( scanBasePackages={"com.example"}, exclude={SecurityAutoConfiguration.class,HystrixAutoConfiguration.class} ) public class MyApplication { public static void main(String[] args){ SpringApplication.run(MyApplication.class,args); } } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值