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

时间:2013-07-10 05:38:57

标签: java spring spring-mvc junit spring-test

当我运行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

5 个答案:

答案 0 :(得分:9)

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

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

或将lambda代码移出spring bean。我有同样的问题,它帮助了我,我仍然可以使用带有jre / jdk 8的弹簧3。

避免这种失败:

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

答案 1 :(得分:7)

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

查看此问题的答案: Java 1.8 ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

答案 4 :(得分:-1)

您的类路径中似乎存在一些无效的类文件。

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