Default Port in Spring Boot: 5 Configuration Methods

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why won't your Spring Boot "listen" on port 8080?

\n

You launch your first Spring Boot application, see the coveted banner in the console, open your browser — and get "Connection refused". Sound familiar? Most often, the problem is that port 8080 is already occupied by another process (e.g., Skype, Docker, or another instance of your own application). Or, conversely, you need to run three microservices on one machine, and they all want to grab the same port.

\n

In this article, we'll cover all the main ways to configure the port in Spring Boot — from the simplest to "production-ready" methods. You'll learn how to override the port through configuration files, environment variables, command-line arguments, and even programmatically. At the end, I'll provide a checklist for choosing the right method depending on the situation.

\n\n

Method 1: application.properties — The Classic

\n

The most common and intuitive method. In the src/main/resources/application.properties file, simply add one line:

\n
server.port=9090
\n

That's it. After restarting, the application will listen on port 9090. To revert to 8080, just delete or comment out the line.

\n

Pros: simplicity, clarity, works "out of the box".

\n

Cons: hardcoded — for each environment (dev, test, prod) you'll have to change the file manually or use profiles.

\n\n

Method 2: application.yml (YAML) — For Structure Lovers

\n

If you prefer the YAML format, the configuration will look like this:

\n
server:\n  port: 9090
\n

Note the indentation: two spaces before port. YAML is whitespace-sensitive, but it allows grouping settings. For example, you can set both the port and the context path at once:

\n
server:\n  port: 9090\n  servlet:\n    context-path: /myapp
\n

When to use: if your project already uses YAML for all configurations (e.g., for Spring Cloud Config).

\n\n

Method 3: Command-Line Arguments — Flexibility for DevOps

\n

The most flexible method, often used in containers and CI/CD. When launching the JAR file, simply pass the parameter:

\n
java -jar myapp.jar --server.port=9090
\n

Or, if using the Maven/Gradle plugin:

\n
mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=9090
\n

Command-line arguments have the highest priority among all configuration methods in Spring Boot. This means they will override even what is written in application.properties.

\n

Ideal for: Docker containers, Kubernetes, automated deployments.

\n\n

Method 4: Environment Variables — The Twelve-Factor App Standard

\n

Spring Boot automatically reads environment variables and converts them into properties. The rule is simple: SERVER_PORT (uppercase, underscore instead of a dot) becomes server.port.

\n

On Linux/macOS:

\n
export SERVER_PORT=9090\njava -jar myapp.jar
\n

On Windows (cmd):

\n
set SERVER_PORT=9090\njava -jar myapp.jar
\n

In Docker Compose, it looks even more elegant:

\n
services:\n  myapp:\n    image: myapp:latest\n    ports:\n      - "9090:9090"\n    environment:\n      - SERVER_PORT=9090
\n

Why this is great: environment variables don't end up in the repository, they are easy to change for different environments, and they follow the 12-factor app methodology.

\n\n

Method 5: Programmatic Configuration — For Micromanagement

\n

Sometimes you need to calculate the port dynamically (e.g., find a free port or get it from an external service). In that case, Java code comes to the rescue.

\n

The simplest option is to override the setServerPort method in the class

Blogs

Book Recommendations