您的浏览器过于古老 & 陈旧。为了更好的访问体验, 请 升级你的浏览器
一位不愿透露姓名的用户 发布于2019年11月25日 15:30

More than one fragment with the name [spring_web] was found.

3806 次浏览 读完需要≈ 13 分钟 Spring MVCSpring

我在使用 Tomcat 9.0.27 启动一个基于 Spring Web MVC 的项目时遇到了问题,Tomcat 的控制台输出如下错误信息:

25-Nov-2019 15:22:20.381 严重 [RMI TCP Connection(3)-127.0.0.1] org.apache.tomcat.util.modeler.BaseModelMBean.invoke Exception invoking method [createStandardContext]
	javax.management.RuntimeOperationsException: Exception invoking method [manageApp]
		at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:298)
		at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
		at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
		at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:456)
		at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:405)
		at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
		at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
		at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
		at java.lang.reflect.Method.invoke(Method.java:498)
		at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:289)
		at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
		at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
		at com.sun.jmx.remote.security.MBeanServerAccessController.invoke(MBeanServerAccessController.java:468)
		at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
		at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
		at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
		at java.security.AccessController.doPrivileged(Native Method)
		at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1408)
		at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
		at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
		at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
		at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
		at java.lang.reflect.Method.invoke(Method.java:498)
		at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:346)
		at sun.rmi.transport.Transport$1.run(Transport.java:200)
		at sun.rmi.transport.Transport$1.run(Transport.java:197)
		at java.security.AccessController.doPrivileged(Native Method)
		at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
		at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
		at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
		at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
		at java.security.AccessController.doPrivileged(Native Method)
		at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
		at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
		at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
		at java.lang.Thread.run(Thread.java:748)
	Caused by: java.lang.IllegalStateException: Error starting child
		at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:720)
		at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:690)
		at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:705)
		at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1728)
		at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
		at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
		at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
		at java.lang.reflect.Method.invoke(Method.java:498)
		at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:289)
		... 35 more
	Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
		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.addChildInternal(ContainerBase.java:717)
		... 43 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:1133)
		at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:774)
		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:5051)
		at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
		... 44 more

请问这个错误是什么意思?又该如何解决呢?求各位大神帮帮忙 ~

1 个回答

Ready · 4年前

分析

要解决异常问题,就要找到导致异常的根本原因。一般情况,「Caused by」根异常的 message 信息会告诉我们关键信息。就像你这个问题里面的关键信息一样:

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.

这段话什么意思呢?大意如下:

找到了不止一个名为 [spring_web] 的 fragment,这在相对顺序上是不合法的。详情请参考 Servlet 规范的 8.2.2 2c 章节。可以考虑使用 absolute ordering。

那么,我们查一下 Servlet 4.0 规范 (PDF下载链接在此)的相应章节,里面是这样写的:

8.2.2 Ordering of web.xml and web-fragment.xml

Since the specification allows the application configuration resources to be composed of multiple configuration files (web.xml and web-fragment.xml), discovered and loaded from several different places in the application, the question of ordering must be addressed. This section specifies how configuration resource authors may declare the ordering requirements of their artifacts.

A web-fragment.xml may have a top level <name> element of type javaee:java-identifierType. There can only be one <name> element in a web-fragment.xml. If a <name> element is present, it must be considered for the ordering of artifacts (unless the duplicate name exception applies, as described below).

[此处有省略]

2. Relative ordering: an <ordering> element within the web-fragment.xml. There can only be one <ordering> element in a web-fragment.xml.

[此处有省略]

c. Duplicate name exception: if, when traversing the web-fragments, multiple members with the same <name> element are encountered, the application must log an informative error message including information to help fix the problem, and must fail to deploy. For example, one way to fix this problem is for the user to use absolute ordering, in which case relative ordering is ignored.

大致意思是说,由于 Servlet 规范允许由多个配置文件(web.xmlweb-fragment.xml)组合来作为Web应用的配置。web-fragment.xml中可以有且最多只能有一个顶级的 <name> 节点,如果存在 <name> 节点,则必须考虑排序问题。

如果 <name> 元素的名称重复,则应用必须记录一个有用的、有助于修复该问题的错误信息,并中断部署(部署失败)。比如,修复该问题的方法之一就是使用绝对排序,此时,相对排序将被忽略。

我们在项目中全局搜索一下 web-fragment.xml,会发现在 spring-web-<version>.jarMETA-INF 中存在一个 web-fragment.xml,其内容如下:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-fragment xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd"
	version="3.0" metadata-complete="true">

	<name>spring_web</name>
	<distributable/>

</web-fragment>

看得出来,异常信息中的 [spring_web] 就是来自于这个文件。

解决

总而言之,出现该问题,就意味着出现了多个重复的 <name> 元素。

那怎么会重复了呢?你就得检查一下——你是否由于 项目或者IDE 的配置原因而误引入了多个版本的 Spring MVC jar文件

如果发现了问题,就解决;如果确认无误,那么你可以在项目的 web.xml 中添加一个空的<absolute-ordering />元素。

已采纳 ? 0 0 1 编辑

回答得太详细了,非常感谢!我这边检查了下配置,重新清理了下 Maven 依赖,就没问题了~

撰写答案