简体   繁体   English

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

[英]ArrayOutOfBoundsException on Bean creation while using Java 8 constructs

I am getting an ArrayIndexOutOfBoundsException on service start up (Bean creation) when i use Java 8 features. 当我使用Java 8功能时,我在服务启动(Bean创建)上得到一个ArrayIndexOutOfBoundsException

Java 8 has been set up and has been working. Java 8已经建立并一直在运行。 The code compiles correctly. 代码编译正确。 On service start, the service fails to listen to port as the beans don't get created. 在服务启动时,服务无法侦听端口,因为bean未创建。 When i change the code (remove java 8 constructs) the service starts and everything works fine. 当我更改代码(删除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. 对我来说没有意义的是,为什么在创建bean时,函数内部的代码(不是bean类的构造函数)被覆盖。 I ask this, because the exception is not there when i use the normal for loop instead of the parallel stream. 我问这个,因为当我使用普通for循环而不是并行流时,异常不存在。 Shouldn't an ArrayOutOfBoundsException arise when the function is called and this code is actually used. 调用函数并实际使用此代码时, ArrayOutOfBoundsException出现ArrayOutOfBoundsException

How do i fix this? 我该如何解决?

Which version of Spring do you use? 你使用的是哪个版本的Spring? You need upgrade to Spring 4 to use Java 8 lambda expressions. 您需要升级到Spring 4才能使用Java 8 lambda表达式。

I found a bug recently when using Spring 3.0.5, which appears to be fixed in 4.0.5. 我最近在使用Spring 3.0.5时发现了一个bug,它似乎在4.0.5中得到修复。 Here are the details. 这是详细信息。

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。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM