解析 applicationContext.xml 会呈现一个 `java.lang.NoSuchMethodError`。这可能是由依赖性问题引起的吗?

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

Parsing applicationContext.xml renders a `java.lang.NoSuchMethodError`. Could this be caused by dependency issues?

javaspringapplicationcontext

提问by atripes

I am invoking a test method (Junit) with an annotated application context (@ContextConfiguration(locations={"classpath:applicationContext.xml"})). The test fails with the following stacktrace:

我正在使用带注释的应用程序上下文 ( @ContextConfiguration(locations={"classpath:applicationContext.xml"}))调用测试方法 (Junit )。测试失败并显示以下堆栈跟踪:

11:20:39.793 [main] ERROR o.s.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@28419cd] to prepare test instance [com.atrioom.TestDynamo@7fbdc7db]
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:91) ~[spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:74) ~[spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:116) ~[spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:82) ~[spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212) ~[spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:199) [spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runReflectiveCall(SpringJUnit4ClassRunner.java:251) [spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.11.jar:na]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:253) [spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:216) [spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:82) [spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.junit.runners.ParentRunner.run(ParentRunner.java:238) [junit-4.11.jar:na]
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63) [junit-4.11.jar:na]
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) [junit-4.11.jar:na]
    at org.junit.runners.ParentRunner.access
<?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"
xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.1.xsd">

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

<context:annotation-config />
0(ParentRunner.java:53) [junit-4.11.jar:na] at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:229) [junit-4.11.jar:na] at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:60) [spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE] at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:67) [spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE] at org.junit.runners.ParentRunner.run(ParentRunner.java:309) [junit-4.11.jar:na] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:162) [spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE] at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) [.cp/:na] at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) [.cp/:na] at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) [.cp/:na] at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) [.cp/:na] at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) [.cp/:na] at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) [.cp/:na] Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [applicationContext.xml]; nested exception is java.lang.NoSuchMethodError: org.springframework.beans.factory.xml.XmlReaderContext.getEnvironment()Lorg/springframework/core/env/Environment; at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:413) ~[spring-beans-4.0.7.RELEASE.jar:4.0.7.RELEASE] at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:335) ~[spring-beans-4.0.7.RELEASE.jar:4.0.7.RELEASE] at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303) ~[spring-beans-4.0.7.RELEASE.jar:4.0.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180) ~[spring-beans-4.0.7.RELEASE.jar:4.0.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216) ~[spring-beans-4.0.7.RELEASE.jar:4.0.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187) ~[spring-beans-4.0.7.RELEASE.jar:4.0.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:251) ~[spring-beans-4.0.7.RELEASE.jar:4.0.7.RELEASE] at org.springframework.test.context.support.AbstractGenericContextLoader.loadBeanDefinitions(AbstractGenericContextLoader.java:253) ~[spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE] at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:122) ~[spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE] at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60) ~[spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE] at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:108) ~[spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE] at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:260) ~[spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE] at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:63) ~[spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE] at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:83) ~[spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE] ... 25 common frames omitted Caused by: java.lang.NoSuchMethodError: org.springframework.beans.factory.xml.XmlReaderContext.getEnvironment()Lorg/springframework/core/env/Environment; at org.springframework.context.annotation.ComponentScanBeanDefinitionParser.parse(ComponentScanBeanDefinitionParser.java:81) ~[spring-context-4.1.1.RELEASE.jar:4.1.1.RELEASE] at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:74) ~[spring-beans-4.0.7.RELEASE.jar:4.0.7.RELEASE] at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1424) ~[spring-beans-4.0.7.RELEASE.jar:4.0.7.RELEASE] at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1414) ~[spring-beans-4.0.7.RELEASE.jar:4.0.7.RELEASE] at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:187) ~[spring-beans-4.0.7.RELEASE.jar:4.0.7.RELEASE] at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:141) ~[spring-beans-4.0.7.RELEASE.jar:4.0.7.RELEASE] at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:110) ~[spring-beans-4.0.7.RELEASE.jar:4.0.7.RELEASE] at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:508) ~[spring-beans-4.0.7.RELEASE.jar:4.0.7.RELEASE] at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:391) ~[spring-beans-4.0.7.RELEASE.jar:4.0.7.RELEASE] ... 38 common frames omitted

I think the problem is a missing method (org.springframework.beans.factory.xml.XmlReaderContext.getEnvironment()). I am not sure how to read the line correctly, the closed parenthesis with the addition Lorg/springframework/core/env/Environmentconfuses me. I tried different bean versions in my applicationContext.xml as well as different spring versions.

我认为问题在于缺少方法 ( org.springframework.beans.factory.xml.XmlReaderContext.getEnvironment())。我不确定如何正确阅读该行,带有添加的闭括号Lorg/springframework/core/env/Environment让我感到困惑。我在 applicationContext.xml 中尝试了不同的 bean 版本以及不同的 spring 版本。



This is my applicationContext:

这是我的应用程序上下文:

##代码##

回答by chutcher

I just encountered this with Spring 4.1.1.RELEASE, Spring Security 3.2.5.RELEASE and OAuth2 2.0.3.RELEASE. It was OAuth2 that needed the getEnvironment method.

我刚刚在 Spring 4.1.1.RELEASE、Spring Security 3.2.5.RELEASE 和 OAuth2 2.0.3.RELEASE 中遇到了这个问题。OAuth2 需要 getEnvironment 方法。

One of the Spring Security dependencies downloaded Spring Beans 4.0.3.RELEASE which does not have getEnvironment. I fixed by explicitly defining spring-beans 4.1.1.RELEASE as a dependency.

Spring Security 依赖项之一下载了没有 getEnvironment 的 Spring Beans 4.0.3.RELEASE。我通过明确定义 spring-beans 4.1.1.RELEASE 作为依赖项来修复。

回答by atripes

As Jens and Xstian hinted, there was a problem with the version of some of the jars in my classpath. Problem solved by doing a mvn clean install -U. I tried that before posting this question, but sometimes it just does not seem to do the trick, while sometimes it does.

正如 Jens 和 Xstian 所暗示的,我的类路径中的一些 jar 版本存在问题。问题通过做一个mvn clean install -U. 我在发布这个问题之前尝试过,但有时它似乎并不能解决问题,而有时却可以。