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.
Configuring the port for a Spring Boot application is often necessary to avoid conflicts with other services or to adhere to deployment environment constraints. Spring Boot offers various methods to set the application port, catering to different development and deployment scenarios. Below, we delve into these methods, providing technical explanations and examples.
Default Port Configuration
By default, a Spring Boot web application runs on port 8080. However, in practical scenarios, such as multiple applications running simultaneously or specific production environment requirements, there's often a need to change this port.
Configuration File Approach
The most common method to configure the port is through application properties files (application.properties or application.yml), which Spring Boot automatically loads at startup.
Properties File (application.properties)
YAML File (application.yml)
Using Programmatic Configuration
You can also set the port programmatically in your Spring Boot application. This is particularly useful for setting the port dynamically based on certain conditions at runtime.
Example:
Here is how you can set the port programmatically using the SpringApplicationBuilder:
Command Line Arguments
Another flexible option is to specify the server port directly via command line arguments when running the application.
Running from Command Line:
This method is especially useful for temporary changes or when deploying using scripts that might require dynamic allocation of ports.
Environment Variables
Setting the port through environment variables is an ideal approach for containerized applications (e.g., Docker) or platform-as-a-service environments where port configuration might be externally managed.
Example:
Before running the application, you can set an environment variable:
In application.properties, you can refer to this environment variable:
Configuring Using Profiles
Spring Profiles allow you to maintain different configurations for different environments. You can specify a different port for each profile.
application-dev.properties
When running the application, specify the active profile:
Priority of Various Configuration Methods
It's important to note that if multiple configuration methods are used, there is a precedence that Spring Boot follows:
- Command line arguments
- Environment variables
- Programmatically set configuration
- Profile-specific configuration files
- Main
application.propertiesorapplication.yml
Summary Table
Here is a quick reference:
| Configuration Method | Example | Use Case |
| Properties or YAML file | server.port=8081 | Standard method, ideal for static environments |
| Programmatic | .properties("server.port=8082") | Dynamic conditions within the application |
| Command line arguments | --server.port=8083 | Temporary changes or script-driven deployments |
| Environment variables | SERVER_PORT=8084 | Containerized or PaaS deployments |
| Profiles | -Dspring.profiles.active=dev | Different configurations for various environments |
Best Practices and Considerations
- Security: When deploying in production, ensure that the chosen port does not conflict with other critical services and is compliant with security policies.
- Ephemeral Ports: Be cautious about using ports within the ephemeral range; these are typically higher numbered ports temporarily used by client sessions.
- Load Balancing: In load-balanced environments, ensure that the port configuration is consistent across all instances or handled appropriately by the load balancer configuration.
Adjusting the port in Spring Boot is straightforward but crucial for the correct deployment and operation of your application. Whether through properties, programmatically, or via the command line, Spring Boot's flexible configuration methods cater to a wide array of deployment scenarios.
Related reading
- How to configure port for a Spring Boot application
- 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

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.