问题
java.lang.IllegalArgumentException: 找到多个名为spring_web的片段。这是不合法的相对排序。
详细问题
笔者使用Servlet+JSP技术框架进行项目开发,对于项目进行国际化(即i18n,实现中英文页面转换),需要引入
下述包
<dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webartifactId> <version>5.3.20version>dependency>
启动项目报错,控制台具体报错如下
02-Jul-2023 21:49:58.237 警告 [RMI TCP Connection(2)-127.0.0.1] org.apache.tomcat.util.descriptor.web.WebXml.setVersion 未知版本字符串 [5.0]。将使用默认版本。02-Jul-2023 21:49:58.256 严重 [RMI TCP Connection(2)-127.0.0.1] org.apache.tomcat.util.modeler.BaseModelMBean.invoke 调用方法[manageApp]时发生异常java.lang.IllegalStateException: 启动子级时出错at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:729)at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:698)at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:696)at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1783)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:293)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:460)at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:408)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:293)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:357)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:573)at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:834)at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)at java.security.AccessController.doPrivileged(Native Method)at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)at java.lang.Thread.run(Thread.java:748)Caused by: org.apache.catalina.LifecycleException: 无法启动组件[StandardEngine[Catalina].StandardHost[localhost].StandardContext[/bms]]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:726)... 43 moreCaused by: java.lang.IllegalArgumentException: 找到多个名为spring_web的片段。这是不合法的相对排序。有关详细信息,请参阅Servlet规范的第8.2.2 2c节。考虑使用绝对排序。at org.apache.tomcat.util.descriptor.web.WebXml.orderWebFragments(WebXml.java:2275)at org.apache.tomcat.util.descriptor.web.WebXml.orderWebFragments(WebXml.java:2231)at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1294)at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:986)at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:303)at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5136)at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)... 44 more02-Jul-2023 21:49:58.257 严重 [RMI TCP Connection(2)-127.0.0.1] org.apache.tomcat.util.modeler.BaseModelMBean.invoke 调用方法[createStandardContext]时发生异常javax.management.RuntimeOperationsException: 调用方法[manageApp]时发生异常at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:303)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:460)at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:408)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:293)at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)[2023-07-02 09:49:58,286] Artifact bookmanagesystem:war exploded: Error during artifact deployment. See server log for details.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:357)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:573)at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:834)at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)at java.security.AccessController.doPrivileged(Native Method)at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)at java.lang.Thread.run(Thread.java:748)Caused by: java.lang.IllegalStateException: 启动子级时出错at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:729)at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:698)at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:696)at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1783)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:293)... 35 moreCaused by: org.apache.catalina.LifecycleException: 无法启动组件[StandardEngine[Catalina].StandardHost[localhost].StandardContext[/bms]]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:726)... 43 moreCaused by: java.lang.IllegalArgumentException: 找到多个名为spring_web的片段。这是不合法的相对排序。有关详细信息,请参阅Servlet规范的第8.2.2 2c节。考虑使用绝对排序。at org.apache.tomcat.util.descriptor.web.WebXml.orderWebFragments(WebXml.java:2275)at org.apache.tomcat.util.descriptor.web.WebXml.orderWebFragments(WebXml.java:2231)at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1294)at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:986)at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:303)at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5136)at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)... 44 more
解决方案
删除刚刚所引入的包,对于笔者而言。即下述包
<dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webartifactId> <version>5.3.20version>dependency>
重新刷新maven配置
2:若重启项目依旧存在上述报错,删除target再次重启
问题产生原因
原因是项目中存在多个相同名称的servlet片段(servlet fragment)。具体来说,错误提示中提到了"找到多个名为spring_web的片段",这违反了Servlet规范的要求。根据Servlet规范的第8.2.2 2c节,相对排序(relative ordering)中不能存在多个相同名称的片段。
但在笔者的项目中,引入了Spring框架的spring-web依赖,该依赖可能包含了一个名为spring_web的servlet片段。当项目启动时,Servlet容器(如Tomcat)会尝试对这些片段进行排序,但由于存在多个相同名称的片段,无法确定它们的相对顺序,因此抛出了异常。
如果删除依赖后问题仍然存在,是由于项目在使用之前编译生成的类文件和部署文件,导致项目依旧包含冲突的servlet片段。
问题解决原因
通过删除重复的依赖项并解决依赖冲突,使得 Maven 能够正确解析和排序 “spring-web” 依赖项。这样,当 Tomcat 容器启动项目时,它能够正确加载所需的依赖项,从而避免了"java.lang.IllegalArgumentException"异常的出现。
通过删除项目的target目录后重新构建和启动项目。可以清除之前编译生成的类文件和部署文件,以确保项目重新构建时不会包含冲突的servlet片段。
总结
大多数帖子仅仅写了笔者解决方案的第一步。事实上,从理论上讲,仅仅使用第一步是可以解决问题的。
但是笔者的情况似乎有些诡异,其实这是由于项目缓存造成的。删除target后便会重新构建项目(清除之前的构建结果,然后从头开始重新编译整个项目。这包括编译源代码、处理资源文件和构建输出目录等步骤。重新构建项目适用于需要确保所有代码都是最新版本并且没有任何旧构建残留的情况。重新构建项目可能需要更长的时间,特别是对于大型项目或依赖项较多的项目)。
参考文献
问题产生原因以及问题解决原因参考chatgpt
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈