Spring3.x 版本和 JDK1.8 不兼容导致 java.lang.IllegalStateException: Failed to load ApplicationContext
由于安装了 JDK1.8 的版本,最近在进行整合 Struts2+Spring+Hibernate 框架的时候,不小心导入了之前下载的 Spring 3.2.0 版本的 jar 包。
结果在运行测试用例的时候,总是显示红条,报出 java.lang.IllegalStateException: Failed to load ApplicationContext 的错误。
异常描述:
1 java.lang.IllegalStateException: Failed to load ApplicationContext 2 at org.springframework.test.context.TestContext.getApplicationContext 3 (TestContext.java:157) 4 at 5 org.springframework.test.context.web.ServletTestExecutionListener. 6 setUpRequestContextIfNecessary(ServletTestExecutionListener.java:103) 7 at 8 org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance 9 (ServletTestExecutionListener.java:73) 10 at org.springframework.test.context.TestContextManager.prepareTestInstance 11 (TestContextManager.java:313) 12 at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest 13 (SpringJUnit4ClassRunner.java:211) 14 at org.springframework.test.context.junit4.SpringJUnit4ClassRunner 15 $1.runReflectiveCall(SpringJUnit4ClassRunner.java:288) 16 at org.junit.internal.runners.model.ReflectiveCallable.run 17 (ReflectiveCallable.java:12) 18 at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock 19 (SpringJUnit4ClassRunner.java:284) 20 at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild 21 (SpringJUnit4ClassRunner.java:231) 22 at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild 23 (SpringJUnit4ClassRunner.java:88) 24 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 25 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 26 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 27 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 28 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 29 at 30 org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate 31 (RunBeforeTestClassCallbacks.java:61) 32 at 33 org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate 34 (RunAfterTestClassCallbacks.java:71) 35 at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 36 at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run 37 (SpringJUnit4ClassRunner.java:174) 38 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run 39 (JUnit4TestReference.java:86) 40 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 41 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests 42 (RemoteTestRunner.java:459) 43 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests 44 (RemoteTestRunner.java:678) 45 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run 46 (RemoteTestRunner.java:382) 47 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main 48 (RemoteTestRunner.java:192) 49 Caused by: java.lang.IllegalArgumentException 50 at org.springframework.asm.ClassReader.<init>(Unknown Source) 51 at org.springframework.asm.ClassReader.<init>(Unknown Source) 52 at org.springframework.asm.ClassReader.<init>(Unknown Source) 53 at org.springframework.core.type.classreading.SimpleMetadataReader.<init> 54 (SimpleMetadataReader.java:52) 55 at 56 org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader 57 (SimpleMetadataReaderFactory.java:80) 58 at 59 org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader 60 (CachingMetadataReaderFactory.java:101) 61 at 62 org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader 63 (SimpleMetadataReaderFactory.java:76) 64 at 65 org.springframework.context.annotation.ConfigurationClassUtils. 66 checkConfigurationClassCandidate(ConfigurationClassUtils.java:70) 67 at 68 org.springframework.context.annotation.ConfigurationClassPostProcessor. 69 processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:253) 70 at 71 org.springframework.context.annotation.ConfigurationClassPostProcessor. 72 postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:223) 73 at 74 org.springframework.context.support.AbstractApplicationContext. 75 invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:630) 76 at org.springframework.context.support.AbstractApplicationContext.refresh 77 (AbstractApplicationContext.java:461) 78 at 79 org.springframework.test.context.support.AbstractGenericContextLoader.loadContext 80 (AbstractGenericContextLoader.java:106) 81 at 82 org.springframework.test.context.support.AbstractGenericContextLoader.loadContext 83 (AbstractGenericContextLoader.java:57) 84 at 85 org.springframework.test.context.support.AbstractDelegatingSmartContextLoader. 86 delegateLoading(AbstractDelegatingSmartContextLoader.java:100) 87 at 88 org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext 89 (AbstractDelegatingSmartContextLoader.java:248) 90 at org.springframework.test.context.TestContext.loadApplicationContext 91 (TestContext.java:124) 92 at org.springframework.test.context.TestContext.getApplicationContext 93 (TestContext.java:148) 94 ... 24 more
经查明问题的原因是:安装的 JDK 版本和 Myeclipse 项目 lib 中导入的 Spring 的 jar 包版本存在不兼容的情况。
解决办法:
一:把 JDK 版本换成1.7 or 1.7以下
二:使用 Spring 4.0 RELEASE 及以上版本
关于 JDK 各个版本的下载:
可以点击链接:http://java.sun.com/products/archive/
关于 Spring 各个版本 jar 包的下载:
可以点击链接:http://repo.spring.io
鼠标点击左侧导航栏 –> 符号展开菜单选择 Artifacts 选项 –> 选择子菜单下的 libs-release-local 选项 –> 依次进入其子目录 org/springframework/spring 就可以看到 spring 各版本信息了 –> 选择需要的 spring 版本 ( 例如 4.3.8.RELEASE ,点击打开选择 spring-framework-4.3.8.RELEASE-dist.zip ),下载解压即可获得 jar 包、api 等文件。