Tomcat端口8080启动失败,修改为8087仍遇问题求助
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=8080takes precedence over properties files. - Profile-specific configs: If you're using a profile (e.g.,
spring.profiles.active=dev), checkapplication-dev.properties—it might still haveserver.port=8080. - YAML config: If you have an
application.ymlfile, it might containserver.port: 8080which 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
targetdirectory 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 8087in Command Prompt to check for active connections. - Linux/macOS: Run
lsof -i :8087in 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

