使用 Java 8 构造时创建 Bean 时的 ArrayOutOfBoundsException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30729125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:34:08  来源:igfitidea点击:

ArrayOutOfBoundsException on Bean creation while using Java 8 constructs

javaspringjava-8javabeansindexoutofboundsexception

提问by akshitBhatia

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

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

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.

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

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());
    }
}

Same code using Java 8 constructs:

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

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

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

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

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

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 ArrayOutOfBoundsExceptionarise when the function is called and this code is actually used.

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

How do i fix this?

我该如何解决?

回答by vasekt

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

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

回答by Ash

I found a bug recently when using Spring 3.0.5, which appears to be fixed in 4.0.5. Here are the details.

我最近在使用 Spring 3.0.5 时发现了一个错误,该错误似乎已在 4.0.5 中修复。这是详细信息。

If you have a class that has a parameterized constructor and also has a method that uses a lambda expression (introduced in Java 8), then a ArrayIndexOutOfException occurs when creating a bean for that class.

如果您有一个具有参数化构造函数的类,并且还有一个使用 lambda 表达式的方法(在 Java 8 中引入),那么在为该类创建 bean 时会发生 ArrayIndexOutOfException。