Spring Boot
Port Configuration
Application Development
Java Frameworks
Coding Guide

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

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)

properties
server.port=8081

YAML File (application.yml)

yaml
server:
  port: 8081

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:

java
1public static void main(String[] args) {
2    new SpringApplicationBuilder(Application.class)
3        .properties("server.port=8082")
4        .run(args);
5}

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:

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

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:

bash
export SERVER_PORT=8084
java -jar myapp.jar

In application.properties, you can refer to this environment variable:

properties
server.port=${SERVER_PORT:8080} # Default to 8080 if not set

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

properties
server.port=8085

When running the application, specify the active profile:

bash
java -jar myapp.jar --spring.profiles.active=dev

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:

  1. Command line arguments
  2. Environment variables
  3. Programmatically set configuration
  4. Profile-specific configuration files
  5. Main application.properties or application.yml

Summary Table

Here is a quick reference:

Configuration MethodExampleUse Case
Properties or YAML fileserver.port=8081Standard method, ideal for static environments
Programmatic.properties("server.port=8082")Dynamic conditions within the application
Command line arguments--server.port=8083Temporary changes or script-driven deployments
Environment variablesSERVER_PORT=8084Containerized or PaaS deployments
Profiles-Dspring.profiles.active=devDifferent 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
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.