ไม่สามารถโหลด ApplicationContext ที่เกิดจาก ArrayIndexOutOfBoundsException ใน ClassReader

เมื่อฉันรันคลาสทดสอบ 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$1.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)

ชั้นเรียนทดสอบของฉันคือ

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

public class CandidateDAOImplTest{
@Autowired
    public CandidateDAO candidateService;


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

โปรแกรมเลือกจ่ายงาน-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>

มันตั้งอยู่ที่ web/WEB-INF/dispatcher-servlet.xml
ฉันใช้ netbeams ied 7.3 และ glassfish server 3.0


person deepak kumar    schedule 10.07.2013    source แหล่งที่มา
comment
เป็นเรื่องที่น่าหดหู่ใจที่จำนวนคำตอบสำหรับคำถามนี้บ่งชี้ว่าผู้ตอบไม่สามารถอ่านการติดตามสแต็กได้   -  person Raedwald    schedule 16.12.2014
comment
ฉันพบปัญหาที่คล้ายกันและแก้ไขโดยการลบ ‹context:component-scan base-package=com.global /› เนื่องจากเราไม่ต้องการมันจริงๆ ซึ่งเหมาะกับเรา ยังไม่รู้ว่าทำไมมันถึงทำให้เกิดปัญหา   -  person troutinator    schedule 23.04.2015


คำตอบ (5)


เป็นไปได้มากว่าคุณจะมีการแสดงออกของแลมบ์ดาอยู่ในถั่วสปริงตัวใดตัวหนึ่งของคุณ เห็นได้ชัดว่า spring 3 beans ไม่สามารถเริ่มต้นได้หากมีแลมบ์ดาอยู่ที่ไหนสักแห่งในโค้ด ในกรณีที่การโยกย้ายไปยังสปริง 4 ไม่ใช่ตัวเลือก ให้ลองเขียนนิพจน์แลมบ์ดาของคุณใหม่โดยใช้คลาสที่ไม่ระบุชื่อ เช่น

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

หรือย้ายรหัสแลมบ์ดาออกจากสปริงบีน ฉันมีปัญหาเดียวกันและมันช่วยฉันได้ ฉันยังคงสามารถใช้ spring 3 กับ jre/jdk 8 ได้

หลีกเลี่ยงความล้มเหลวนี้:

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

คุณไม่ได้ระบุว่าคุณใช้ JDK ใด หากคุณใช้ 1.8 คุณอาจพบว่าคุณจะต้องอัปเกรด Spring เป็น 4 (หรือ 3.2.9+ ตามที่ yurez ชี้ให้เห็น)

ดูคำตอบสำหรับคำถามนี้: Java 1.8 ASM ClassReader ไม่สามารถแยกวิเคราะห์ไฟล์คลาสได้ - อาจเนื่องมาจากเวอร์ชันไฟล์คลาส Java ใหม่ที่ยังไม่รองรับ

person vegemite4me    schedule 19.03.2015
comment
ขอบคุณ นั่นมีประโยชน์มาก! - person Kirill Smirnov; 24.07.2016
comment
ตามคำตอบที่สองสำหรับคำถามที่เชื่อมโยง การอัปเดตเป็น Spring 3.2.9+ ก็ใช้งานได้เช่นกัน - person yurez; 11.05.2018

ปัญหาคือตำแหน่งของไฟล์ xml ของคุณ WEB-INF ไม่ได้เป็นส่วนหนึ่งของ classpath ลองคัดลอกไปไว้ในโฟลเดอร์ "src"

person DiTap    schedule 11.08.2014

ลองวางไฟล์ไว้ใต้โฟลเดอร์ src จากนั้นจะถูกคัดลอกไปยังโฟลเดอร์ WEB-INF/คลาส จากนั้น คุณสามารถอ้างถึงเป็น classpath:dispatcher-servlet.xml หากไฟล์ไม่ได้อยู่ภายใต้ไดเร็กทอรี classes โดยตรง คุณจะไม่สามารถใช้ตำแหน่งเป็น classpath:dispatcher-servlet.xml ในการทดสอบของคุณได้

person Atul    schedule 15.10.2014

ดูเหมือนว่าจะมีไฟล์คลาสที่ไม่ถูกต้องบน classpath ของคุณ

บล็อก(http://blog.163.com/mxl_880310/blog/static/1847222162012320102631220/) (ในภาษาจีนตัวเต็ม) อธิบายปัญหาที่คล้ายกัน ผู้เขียนแก้ไขโดยตรวจสอบว่ามีไฟล์คลาสขนาดศูนย์หรือไม่

person Yugang Zhou    schedule 10.07.2013
comment
นี่ไม่ใช่คำตอบสำหรับลิงก์เท่านั้น เนื่องจากคำตอบจะให้บทสรุปของ pidt ของบล็อกที่เชื่อมโยง - person Raedwald; 16.12.2014