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

Tomcat端口8080启动失败,修改为8087仍遇问题求助

Fixing Spring Boot's Persistent 8080 Port Binding Issue

Hey there, let's troubleshoot why your Spring Boot app keeps trying to bind to port 8080 even after you set server.port=8087 in application.properties. This is a common gotcha, so let's walk through the most likely fixes step by step:

1. Verify Your application.properties is Being Loaded

First, double-check that your config file is in the right place: it should live directly under src/main/resources. If it's in a subfolder or misplaced, Spring Boot won't pick up the port change.

To confirm, look for a line in your startup logs like:

Server initialized with port: 8087

If you see 8080 instead, the config isn't being read. You can also add a quick debug check in your HomeController to validate the loaded port:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RestController
public class HomeController {
    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
    
    @Value("${server.port}")
    private int serverPort;

    @GetMapping("/")
    public String home() {
        logger.info("Current server port: {}", serverPort);
        return "Hello World!";
    }
}

Run the app and check the logs—if it prints 8080, your config isn't being applied.

2. Check for Config Overrides

Spring Boot uses a priority order for config sources, and some might be overriding your application.properties setting:

  • Command line arguments: If you start the app with java -jar your-app.jar --server.port=8080, this will overwrite the config file value.
  • Environment variables: A system variable like SERVER_PORT=8080 takes precedence over properties files.
  • Profile-specific configs: If you're using a profile (e.g., spring.profiles.active=dev), check application-dev.properties—it might still have server.port=8080.
  • YAML config: If you have an application.yml file, it might contain server.port: 8080 which could override the properties file (depending on load order).

3. Look for Hardcoded Tomcat Port in Code

Sometimes developers add custom Tomcat configurations that hardcode the port, which bypasses the config file. Check your config classes for beans like this:

@Bean
public TomcatServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.setPort(8080); // This hardcoded value overrides your config!
    return tomcat;
}

If you find this, update it to use the config value instead:

@Bean
public TomcatServletWebServerFactory servletContainer(@Value("${server.port}") int port) {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.setPort(port);
    return tomcat;
}

Or better yet, delete this custom bean entirely—Spring Boot will handle port configuration automatically if you don't override it.

4. Clear IDE Cache and Rebuild

IDE caches can sometimes cause old config values to stick around. Try:

  • For IntelliJ IDEA: Go to File > Invalidate Caches..., check "Clear file system cache and local history", then restart the IDE.
  • For Eclipse: Go to Project > Clean..., select your project, and rebuild.
  • Manually delete the target directory in your project, then recompile and run the app.

5. Confirm Port 8087 is Available

While the issue is that the app is trying 8080, it's still worth making sure 8087 isn't occupied:

  • Windows: Run netstat -ano | findstr 8087 in Command Prompt to check for active connections.
  • Linux/macOS: Run lsof -i :8087 in Terminal to see if the port is in use.

If none of these steps work, sharing the full error stack trace would help narrow down the exact issue—there might be a connector misconfiguration or a dependency conflict we haven't covered yet.

内容的提问来源于stack exchange,提问作者Sercan Göger

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:04:44