java 无法加载由 ClassReader 中的 ArrayIndexOutOfBoundsException 引起的 ApplicationContext

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17563149/
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-01 02:19:57  来源:igfitidea点击:

Failed to load ApplicationContext caused by ArrayIndexOutOfBoundsException in ClassReader

javaspringspring-mvcjunitspring-test

提问by deepak kumar

When I am running junit test class the below exception arises? How can i resolve this?

当我运行 junit 测试类时出现以下异常?我该如何解决这个问题?

Failed to load ApplicationContext
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 8
    at org.springframework.asm.ClassReader.readUnsignedShort(Unknown Source)
    at org.springframework.asm.ClassReader.<init>(Unknown Source)
    at org.springframework.asm.ClassReader.<init>(Unknown Source)
    at org.springframework.asm.ClassReader.<init>(Unknown Source)
    at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:48)
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80)
    at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:101)
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:76)
    at org.springframework.context.annotation.ConfigurationClassUtils.checkConfigurationClassCandidate(ConfigurationClassUtils.java:70)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:233)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:203)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:617)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:446)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:103)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
    at org.springframework.test.context.support.DelegatingSmartContextLoader.loadContext(DelegatingSmartContextLoader.java:228)
    at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:124)

my test class is

我的考试班是

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:dispatcher-servlet.xml")

public class CandidateDAOImplTest{
@Autowired
    public CandidateDAO candidateService;


    @Test
    public void testGetCandidate() {...}
}

dispatcher-servlet.xml

调度程序-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
>

    <context:component-scan base-package="com.global" />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>mymessages</value>
            </list>
        </property>
    </bean>

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
            <value>database.properties</value>
    </property>
    </bean>


    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>


    <!-- Configures the @Controller programming model -->
    <mvc:annotation-driven />

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
    </bean>


    <bean id="candidateService" class="com.global.dao.impl.CandidateDAOImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate" />
    </bean>    

    <!-- Define a Transaction Manager -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
    </bean>

    <!-- Turn on support for transaction annotations -->
    <tx:annotation-driven transaction-manager="txManager"/>

</beans>

its located at web/WEB-INF/dispatcher-servlet.xml.
I am using netbeams ied 7.3 and glassfish server 3.0

它位于 web/WEB-INF/dispatcher-servlet.xml。
我使用的是 netbeams ied 7.3 和 glassfish server 3.0

回答by andrii

Most likely you have lambda expression inside one of your spring beans. Apparently, spring 3 beans can't initialize if there is a lambda somewhere in their code. In case migration to spring 4 is not an option, try to rewrite your lambda expressions using anonymous classes, like

很可能您的一个 spring bean 中有 lambda 表达式。显然,如果代码中的某个地方存在 lambda,则 spring 3 bean 无法初始化。如果迁移到 spring 4 不是一种选择,请尝试使用匿名类重写 lambda 表达式,例如

Function<A,B> lambda = new Function() {
    public B apply(A s) { ... }
}

or move lambda code out of the spring bean. I had the same issue and it helped me, I could still use spring 3 with jre/jdk 8.

或者将 lambda 代码移出 spring bean。我遇到了同样的问题,它对我有帮助,我仍然可以将 spring 3 与 jre/jdk 8 一起使用。

Avoids this failure:

避免这种失败:

 Caused by: java.lang.ArrayIndexOutOfBoundsException: 10348
    at org.springframework.asm.ClassReader.<init>(Unknown Source)

回答by vegemite4me

You have not stated which JDK you are using. If you are using 1.8 then you might find that you will need to upgrade Spring to 4 (or 3.2.9+ as yurez has pointed out).

您没有说明您使用的是哪个 JDK。如果您使用的是 1.8,那么您可能会发现需要将 Spring 升级到 4(或 yurez 指出的 3.2.9+)。

See the answer to this question: Java 1.8 ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet

请参阅此问题的答案: Java 1.8 ASM ClassReader failed to parse class file - 可能是由于尚不支持新的 Java 类文件版本

回答by DiTap

The problem is the location of your xml file. WEB-INF is not part of the classpath. Try copying it under the "src" folder.

问题是您的 xml 文件的位置。WEB-INF 不是类路径的一部分。尝试将其复制到“src”文件夹下。

回答by Atul

Try putting the file under src folder. It will be then copied to the WEB-INF/classesfolder. Then you can refer to it as classpath:dispatcher-servlet.xml. If the file is not directly under the classesdirectory, then you cannot use the location as classpath:dispatcher-servlet.xml in your test

尝试将文件放在 src 文件夹下。然后将其复制到 WEB-INF/ classes文件夹。然后您可以将其称为 classpath:dispatcher-servlet.xml。如果该文件不直接位于classes目录下,则不能在测试中将该位置用作 classpath:dispatcher-servlet.xml

回答by Yugang Zhou

There seems to be some invalid class file on your classpath.

您的类路径上似乎有一些无效的类文件。

A blog(http://blog.163.com/mxl_880310/blog/static/1847222162012320102631220/) (in Traditional Chinese) describes a similar problem, the author solves it by checking if there is a zero size class file.

一个博客(http://blog.163.com/mxl_880310/blog/static/1847222162012320102631220/)(繁体中文)描述了类似的问题,作者通过检查是否有零大小的类文件来解决它。