不像听我废话。可以直接拉到最后看解决方法。

背景,使用工具。我使用的是Intellij idea 和 tomcat 开发Spring web项目。但是我发现我在启动的时候报错。启动不起来,有端口被占用了。但是事实上端口没被占用。于是看日志发现项目里面有两次启动信息。如下。

Connected to server
[2019-04-04 05:57:11,528] Artifact firework-open:war: Artifact is being deployed, please wait...
04-Apr-2019 17:57:11.569 警告 [RMI TCP Connection(4)-127.0.0.1] org.apache.catalina.core.StandardContext.setPath A context path must either be an empty string or start with a '/' and do not end with a '/'. The path [/firework-open/] does not meet these criteria and has been changed to [/firework-open]
04-Apr-2019 17:57:11.607 信息 [RMI TCP Connection(4)-127.0.0.1] org.apache.catalina.startup.ExpandWar.expand An expanded directory [/Users/xmly/local/apache-tomcat-8.5.32/webapps/firework-open] was found with a last modified time that did not match the associated WAR. It will be deleted.
04-Apr-2019 17:57:12.884 警告 [RMI TCP Connection(4)-127.0.0.1] org.apache.tomcat.util.descriptor.web.WebXml.setVersion Unknown version string [4.0]. Default version will be used.
04-Apr-2019 17:57:16.537 信息 [RMI TCP Connection(4)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
[firework-open]17:57:16 633  INFO (org.springframework.web.context.ContextLoader:305) - Root WebApplicationContext: initialization started
[firework-open]17:57:16 694 DEBUG (org.springframework.core.io.support.PathMatchingResourcePatternResolver:192) - Found Equinox FileLocator for 


[firework-open]17:57:21 82 DEBUG (org.springframework.web.context.ContextLoader:340) - Published root WebApplicationContext as ServletContext attribute with name [org.springframework.web.context.WebApplicationContext.ROOT]
[firework-open]17:57:21 82  INFO (org.springframework.web.context.ContextLoader:345) - Root WebApplicationContext: initialization completed in 4446 ms
[firework-open]17:57:21 194 DEBUG (org.springframework.web.servlet.DispatcherServlet:118) - Initializing servlet 'Spring Web'

04-Apr-2019 17:57:21.238 警告 [localhost-startStop-1] org.apache.tomcat.util.descriptor.web.WebXml.setVersion Unknown version string [4.0]. Default version will be used.
[firework-open]17:57:21 294  INFO (org.springframework.web.servlet.DispatcherServlet:489) - FrameworkServlet 'Spring Web': initialization started
[firework-open]17:57:21 294 DEBUG (org.springframework.web.servlet.DispatcherServlet:617) - Servlet with name 'Spring Web' will try to create custom Web

通过日志,我们可以发现,在Spring Web 这个DispatcherServlet 启动成功后,又开始了一个初始化web容器。

[firework-open]17:57:21 194 DEBUG (org.springframework.web.servlet.DispatcherServlet:118) - Initializing servlet 'Spring Web'

04-Apr-2019 17:57:21.238 警告 [localhost-startStop-1] org.apache.tomcat.util.descriptor.web.WebXml.setVersion Unknown version string [4.0]. Default version will be used.
[firework-open]17:57:21 294  INFO (org.springframework.web.servlet.DispatcherServlet:489) - FrameworkServlet 'Spring Web': initialization started
[firework-open]17:57:21 294 DEBUG 

所以。第二次启动的时候会发现第一次启动的端口被占用了。于是就启动不起来了。最开始是怀疑Spring的配置不对。可能是有bean即被SpringMVC这个容器又被Spring这个容器扫描了。但是仔细想想是不对的。日志明确是DispatchServlet被初始化了两次。在这种情况下只能继续查看日志。寻找线索。

在Console里面输出了下面的一条警告信息

04-Apr-2019 17:57:11.569 警告 [RMI TCP Connection(4)-127.0.0.1] org.apache.catalina.core.StandardContext.setPath A context path must either be an empty string or start with a '/' and do not end with a '/'. The path [/firework-open/] does not meet these criteria and has been changed to [/firework-open]

这个的意思是,设置Application Context 不要以斜杠结尾。于是打开idea发现果然自己是以 '/'结尾了 ,去掉这个斜杠后。项目就可以正常启动。直接运行起来了。

Intellij idea SpringMVC 项目启动两次

有的人出现的问题可能不是这个原因。可以参考链接。具体的事情需要具体分析。切记要耐心,一步步分析。问题都是可以解决的。

http://blog.sina.com.cn/s/blog_6118a0550102uwff.html

 

 

相关文章: