动态Web项目用Tomcat部署时出现两个甚至多个spring-web

本人小白一枚,在创建第一个SpringMVC项目的时候出现了如下问题:
HelloWorld项目下出现两个spring-web
这就导致部署该项目的时候控制台报错,如下:

严重: 子容器启动失败
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/HelloWorld]]
	at java.util.concurrent.FutureTask.report(Unknown Source)
	at java.util.concurrent.FutureTask.get(Unknown Source)
	at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:916)
	at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:841)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374)
	at java.util.concurrent.FutureTask.run(Unknown Source)
	at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
	at java.util.concurrent.AbstractExecutorService.submit(Unknown Source)
	at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909)
	at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
	at org.apache.catalina.core.StandardService.startInternal(StandardService.java:421)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
	at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:932)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
	at org.apache.catalina.startup.Catalina.start(Catalina.java:633)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:344)
	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:475)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/HelloWorld]]
	at org.apache.catalina.util.LifecycleBase.handleSubClassException(LifecycleBase.java:440)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:198)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374)
	at java.util.concurrent.FutureTask.run(Unknown Source)
	at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
	at java.util.concurrent.AbstractExecutorService.submit(Unknown Source)
	at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909)
	... 21 more
Caused by: java.lang.IllegalArgumentException: More than one fragment with the name [spring_web] was found. This is not legal with relative ordering. See section 8.2.2 2c of the Servlet specification for details. Consider using absolute ordering.
	at org.apache.tomcat.util.descriptor.web.WebXml.orderWebFragments(WebXml.java:2257)
	at org.apache.tomcat.util.descriptor.web.WebXml.orderWebFragments(WebXml.java:2215)
	at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1134)
	at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:775)
	at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:301)
	at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5048)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
	... 27 more

重点在这:
Caused by: java.lang.IllegalArgumentException: More than one fragment with the name [spring_web] was found. This is not legal with relative ordering. See section 8.2.2 2c of the Servlet specification for details. Consider using absolute ordering.
刚开始不理解,去网上找了一下
其中一种解决方法
按照链接中的做法处理之后,重启Tomcat依旧报错,我注意到,链接中出现的问题
链接中出现的问题
是加载了两次相同的jar包,而我的是不同的jar包,但是名字都是spring-web
还有一种解决方法:

项目右键properties–deployment assembly里remove了一个/WebContent

但是这样操作之后,虽然Tomcat能启动,但是访问页面404,因为这样WebContent文件夹下的jsp,xml文件,lib文件夹都不能部署到Tomcat中,找不到访问路径自然就404了。

网上关于这个错误的记录很少,只能靠自己了。我就想,既然是出现了多个jar包,那是不是我引入到lib中的jar包出现了问题,一看果然是:
在lib文件夹下出现了两个spring-web
lib文件夹下
原因找到了,我下载的spring-framework版本是

spring-framework-5.1.8.RELEASE-dist

我在引入jar包的时候把spring-framework-5.1.8.RELEASE文件夹下的libs文件夹中所有的jar包都引入到了项目中,就导致了如上错误。
解决方法:很简单,把项目中/Web-Content/WEB-INF/lib文件夹下的spring-web-5.1.8.RELEASE-sources.jar删掉就可以了。再重新启动Tomcat,页面就能正常访问了。
困扰我一个下午的问题终于解决了┭┮﹏┭┮

建议像我一样的新手小白在引入jar包的时候尽量用到什么就引入什么,多引入jar包也可能会出现问题。

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐