You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring项目8080端口无法释放,重启提示占用的问题求助

Spring Boot项目端口8080无法释放及异常占用问题解决方案

核心问题梳理

  • 停止Spring Boot应用后进程残留,8080端口无法正常释放
  • 修改端口配置后,所有新指定端口均提示被占用,但CurrPort工具检测不到对应进程
  • IDE停止按钮无法终止进程,仅该8080端口的项目受影响,其他电脑无此问题

分步解决方案

1. 深度排查端口占用(含TIME_WAIT状态连接)

CurrPort可能无法显示处于TIME_WAIT状态的连接,这类连接会暂时占用端口:

  • 打开命令提示符,执行:
    netstat -ano | findstr :8080
    
    列出所有占用8080的连接(包括TIME_WAIT状态),记录对应的PID
  • 强制终止该PID对应的进程:
    taskkill /F /PID <你的PID>
    

2. 禁用Spring DevTools自动重启

DevTools的热重载机制可能导致进程残留:

  • 在application.properties中添加:
    spring.devtools.restart.enabled=false
    
  • 同时关闭IDE中项目的自动重启功能,避免多次启动导致进程堆积

3. 清理IDE及Java进程残留

  • 打开任务管理器,找到所有java.exe或javaw.exe进程,强制结束所有相关进程
  • 完全重启IDE,避免IDE停止信号未正确传递给应用进程

4. 检查系统预留端口

Windows系统可能预留部分端口,导致应用无法使用:

  • 执行命令查看预留端口范围:
    netsh interface ipv4 show excludedportrange protocol=tcp
    
  • 如果8080在预留范围内,执行以下步骤修改:
    1. 停止Winnat服务:net stop winnat
    2. 调整系统端口预留规则(例如将8080排除在预留外)
    3. 重启Winnat服务:net start winnat

5. 检查项目资源释放逻辑

代码中未正确释放资源可能导致进程无法终止:

  • 检查自定义线程池、Socket连接、数据库连接池等,确保应用关闭时调用shutdown()或对应关闭方法
  • 在主应用类中添加销毁钩子,手动释放资源:
    @SpringBootApplication
    public class CalculApplication {
        public static void main(String[] args) {
            SpringApplication.run(CalculApplication.class, args);
        }
    
        @PreDestroy
        public void onDestroy() {
            // 手动关闭线程池、连接池等资源
            // 示例:executorService.shutdown();
        }
    }
    

问题日志参考

2024-04-13T15:03:49.253+02:00  INFO 4584 --- [  restartedMain] com.example.calcul.CalculApplication     : Starting CalculApplication using Java 21.0.2 with PID 4584 (C:\Users\chres\Documents\jetBrainProjects\intellij-idea-projects\calcul\target\classes started by chres in C:\Users\chres\Documents\jetBrainProjects\intellij-idea-projects\calcul)
2024-04-13T15:03:49.256+02:00  INFO 4584 --- [  restartedMain] com.example.calcul.CalculApplication     : No active profile set, falling back to 1 default profile: "default"
2024-04-13T15:03:49.314+02:00  INFO 4584 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2024-04-13T15:03:49.315+02:00  INFO 4584 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2024-04-13T15:03:50.575+02:00  INFO 4584 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2024-04-13T15:03:50.587+02:00  INFO 4584 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-04-13T15:03:50.588+02:00  INFO 4584 --- [  restartedMain] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.19]
2024-04-13T15:03:50.637+02:00  INFO 4584 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-04-13T15:03:50.639+02:00  INFO 4584 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1322 ms
2024-04-13T15:03:51.267+02:00  WARN 4584 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : Unable to start LiveReload server
2024-04-13T15:03:51.288+02:00  WARN 4584 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'
2024-04-13T15:03:51.302+02:00  INFO 4584 --- [  restartedMain] .s.b.a.l.ConditionEvaluationReportLogger : 

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-04-13T15:03:51.323+02:00 ERROR 4584 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Web server failed to start. Port 8080 was already in use.

Action:

Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.912 s
[INFO] Finished at: 2024-04-13T15:03:51+02:00
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0

内容的提问来源于stack exchange,提问作者Christophe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 00:39:51