Spring Boot
configuration
ports
application setup
tutorial

How to configure port for a Spring Boot application

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Spring Boot applications listen on port 8080 by default, but changing the port is straightforward. The best method depends on where you want the configuration to live: inside the application config, outside the app at runtime, or in custom startup code.

Set the Port in application.properties

The most common approach is to define the server port in src/main/resources/application.properties.

properties
server.port=9090

After that, the embedded server starts on port 9090 instead of 8080.

This is the simplest option when the port is part of the application's normal configuration and does not change often between runs.

Set the Port in application.yml

If your project uses YAML configuration, the same setting looks like this:

yaml
server:
  port: 9090

Functionally, this is equivalent to the properties-file example. Use whichever format matches the rest of the codebase.

Override the Port at Runtime

Spring Boot also lets you override configuration at startup. This is useful in containerized, test, or deployment environments where the runtime decides the port.

Command-line override:

bash
java -jar myapp.jar --server.port=9090

Environment variable override:

bash
export SERVER_PORT=9090
java -jar myapp.jar

These runtime overrides are often preferable in CI, Docker, and cloud deployments because they avoid rebuilding the application just to change one environment-specific value.

Configure the Port Programmatically

If you really need to set the port in code, define a web-server factory bean. This is less common than configuration-file or environment-based approaches, but it can be useful for special startup logic.

java
1import org.springframework.boot.SpringApplication;
2import org.springframework.boot.autoconfigure.SpringBootApplication;
3import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
4import org.springframework.context.annotation.Bean;
5
6@SpringBootApplication
7public class DemoApplication {
8
9    public static void main(String[] args) {
10        SpringApplication.run(DemoApplication.class, args);
11    }
12
13    @Bean
14    public TomcatServletWebServerFactory webServerFactory() {
15        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
16        factory.setPort(9090);
17        return factory;
18    }
19}

Use this sparingly. In most cases, externalized configuration is cleaner and easier to manage.

Useful Special Case: Random Port

For tests or temporary local runs, you can ask Spring Boot to pick an available port automatically:

properties
server.port=0

This is common in integration tests where you do not want hard-coded port conflicts.

If you need the chosen port at runtime in a test, Spring can inject it:

java
1import org.springframework.boot.test.context.SpringBootTest;
2import org.springframework.boot.test.web.server.LocalServerPort;
3
4@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
5class DemoApplicationTests {
6
7    @LocalServerPort
8    int port;
9}

Common Pitfalls

The biggest mistake is setting the port in multiple places and forgetting which source has higher precedence. If application.properties says 8081 but the command line says 9090, Spring Boot uses the higher-precedence runtime value.

Another issue is trying to instantiate an interface or abstract web-server factory type directly when configuring the port programmatically. Use the concrete implementation for your embedded server, such as TomcatServletWebServerFactory, unless your setup uses a different server.

Developers also sometimes expose one port internally and publish a different port externally through Docker or a reverse proxy. That is normal, but you need to understand which layer you are changing. Changing the application port is different from changing a container port mapping or load balancer listener.

Finally, do not hard-code ports in code unless you truly need to. Environment variables or command-line overrides are much easier to operate across development, staging, and production.

Summary

  • The default Spring Boot HTTP port is 8080.
  • Set server.port in application.properties or application.yml for normal app configuration.
  • Use command-line arguments or SERVER_PORT for runtime-specific overrides.
  • Programmatic configuration is possible but is usually not the simplest choice.
  • Use server.port=0 when you want Spring Boot to choose a random available port.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.