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.
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.
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:
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:
Environment variable override:
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.
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:
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:
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.portinapplication.propertiesorapplication.ymlfor normal app configuration. - Use command-line arguments or
SERVER_PORTfor runtime-specific overrides. - Programmatic configuration is possible but is usually not the simplest choice.
- Use
server.port=0when you want Spring Boot to choose a random available port.
Related reading
- How to configure RabbitMQ connection with spring-rabbit?
- How to configure RabbitMQ connection with spring-rabbit?
- how to configure redis ttl with spring boot 2.0
- How to configure rolling file appender within Spring Boot's application.yml
- How to configure slf4j-simple
- How to configure spring-boot to use file based H2 database
- How to configure spring-data-mongodb to use a replica set via properties
- How to configure spring boot application to use aspectj transactions?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.