一、异常描述
对于Spring MVC项目配置spring-webmvc依赖包,启动spring初始化全文报“[ERROR] ContextLoader: Context initialization failed...org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from URL [file:..target/classes/spring-context.xml]; nested exception ...java.lang.NoSuchMethodError: org.springframework.beans.factory.xml.XmlReaderContext.getEnvironment()Lorg/springframework/core/env/Environment”异常,日志如下
[00:46:36] [ERROR] ContextLoader: Context initialization failed@b@org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from URL [file:/C:/WS/NJ/project/xwood-project/xwood-test/target/classes/spring-context.xml]; nested exception is java.lang.NoSuchMethodError: org.springframework.beans.factory.xml.XmlReaderContext.getEnvironment()Lorg/springframework/core/env/Environment;@b@ at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:412)@b@ at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)@b@ at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)@b@ at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)@b@ at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)@b@ at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)@b@ at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)@b@ at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)@b@ at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)@b@ at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)@b@ at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:452)@b@ at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)@b@ at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)@b@ at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)@b@ at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4135)@b@ at org.apache.catalina.core.StandardContext.start(StandardContext.java:4630)@b@ at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)@b@ at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)@b@ at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)@b@ at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445)@b@ at org.apache.catalina.startup.Embedded.start(Embedded.java:825)@b@ at org.codehaus.mojo.tomcat.AbstractRunMojo.startContainer(AbstractRunMojo.java:558)@b@ at org.codehaus.mojo.tomcat.AbstractRunMojo.execute(AbstractRunMojo.java:255)@b@ at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:105)@b@ at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:577)@b@ at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:324)@b@ at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:247)@b@ at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:104)@b@ at org.apache.maven.cli.MavenCli.execute(MavenCli.java:427)@b@ at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:157)@b@ at org.apache.maven.cli.MavenCli.main(MavenCli.java:121)@b@ at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)@b@ at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)@b@ at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)@b@ at java.lang.reflect.Method.invoke(Method.java:597)@b@ at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)@b@ at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)@b@ at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)@b@ at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)@b@Caused by: java.lang.NoSuchMethodError: org.springframework.beans.factory.xml.XmlReaderContext.getEnvironment()Lorg/springframework/core/env/Environment;@b@ at org.springframework.context.annotation.ComponentScanBeanDefinitionParser.parse(ComponentScanBeanDefinitionParser.java:81)@b@ at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:73)@b@ at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1438)@b@ at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1428)@b@ at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:185)@b@ at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:139)@b@ at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:108)@b@ at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)@b@ at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)@b@ ... 38 more
二、解决办法
因为没有配置spring-context-support依赖包导致,POM文件中应包含如下内容
<dependency>@b@ <groupId>org.springframework</groupId>@b@ <artifactId>spring-context-support</artifactId>@b@ <version>3.2.4.RELEASE</version>@b@</dependency>