使用Java 8构造时Bean创建的ArrayOutOfBoundsException [英] ArrayOutOfBoundsException on Bean creation while using Java 8 constructs

查看:1369
本文介绍了使用Java 8构造时Bean创建的ArrayOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用Java 8功能时,我在服务启动(Bean创建)上得到 ArrayIndexOutOfBoundsException

I am getting an ArrayIndexOutOfBoundsException on service start up (Bean creation) when i use Java 8 features.

Java 8已经建立并一直在运行。代码编译正确。在服务启动时,服务无法侦听端口,因为bean未创建。当我更改代码(删除java 8构造)时,服务启动并且一切正常。

Java 8 has been set up and has been working. The code compiles correctly. On service start, the service fails to listen to port as the beans don't get created. When i change the code (remove java 8 constructs) the service starts and everything works fine.

这是我正在使用的代码(服务启动的工作代码) ):

This is the code i am using (the working code for which the service starts):

for (Item itemObject : response) {
    if (itemObject.hasId()) {
        idList.add(String.valueOf(itemObject.Id());
    }
}

使用Java 8构造的相同代码:

Same code using Java 8 constructs:

response.parallelStream()
        .filter(itemObject -> itemObject.hasId())
        .map(itemObject -> itemObject.getId())
        .forEach(id -> idList.add(id));

包含这段代码的类的bean是使用组件扫描创建的。

The bean for the class containing this piece of code is created using component scan.

以下是使用第二个代码块代替第一个代码块时的异常消息:

The following is the exception message when the second code block is used in place of the first one:

Exiting with throwable: java.lang.IllegalArgumentException: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/workspace/.../GetContainerIdForFcSkuAdapter.class]; nested exception is java.lang.ArrayIndexOutOfBoundsException: 51880
 java.lang.IllegalArgumentException: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/workspace....Some.class]; nested exception is java.lang.ArrayIndexOutOfBoundsException: 51880

对我来说没有意义的是,为什么是创建bean时,函数内部的代码(不是bean类的构造函数)被覆盖。我问这个,因为当我使用普通for循环而不是并行流时,异常不存在。调用该函数并实际使用此代码时,不应出现 ArrayOutOfBoundsException

What does not make sense to me is, why is the code inside a function (which is not the constructor of the bean class) being covered while creating the bean. I ask this, because the exception is not there when i use the normal for loop instead of the parallel stream. Shouldn't an ArrayOutOfBoundsException arise when the function is called and this code is actually used.

我如何修复这个?

推荐答案

您使用的是哪个版本的Spring?您需要升级到Spring 4才能使用Java 8 lambda表达式。

Which version of Spring do you use? You need upgrade to Spring 4 to use Java 8 lambda expressions.

这篇关于使用Java 8构造时Bean创建的ArrayOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆