Skip to content

How To Configure a Port For a Spring Boot Application

Learn how to change the default port in your Spring Boot application using application.properties, YAML, programmatic settings, or command-line arguments. Avoid port conflicts and customize your setup easily with these straightforward configuration methods.

Gopi Gorantala
Gopi Gorantala
2 min read
How To Configure a Port For a Spring Boot Application

Table of Contents

Spring Boot is known for its convention-over-configuration approach, and by default, it runs on port 8080. But in real-world scenarios, you often need to change this port—maybe to avoid conflicts or align with deployment requirements.

In this guide, we’ll show you three easy ways to change the port of a Spring Boot application, so your service runs exactly where you want it.

Why Change the Default Port?

Changing the port is necessary when:

  • You have multiple services on the same server.
  • Port 8080 is already in use.
  • Your DevOps or security policies require custom ports.

Now let’s go over the different methods to configure the port.

Method 1: Change Port in application.properties

The simplest way to change the port is by adding the following line to your src/main/resources/application.properties file:

server.port=9090

Replace 9090 with any port number you want your application to use.

Advantages:

  • Easy to implement.
  • Works well for small projects or local development.

Method 2: Use application.yml (YAML format)

If you prefer YAML over .properties, add this to your application.yml:

server:
  port: 9090

This is especially useful when managing multiple configuration options in a structured format.

Method 3: Set Port Programmatically

If you need more control (e.g., dynamic port selection), you can set the port programmatically in your main application class:

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(MyApplication.class);
        app.setDefaultProperties(Collections.singletonMap("server.port", "9090"));
        app.run(args);
    }
}

This approach is useful when:

  • You want to read the port from environment variables or arguments.
  • You’re embedding your app in a larger system and need flexible configuration.

Bonus: Override with Command-Line Argument

You can also override the port at runtime using:

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

Or in Maven:

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

This is perfect for CI/CD pipelines or quick testing.

Best Practices

  • Avoid hardcoding ports unless absolutely necessary.
  • Use environment-specific profiles (application-dev.properties, application-prod.properties) to manage ports for different stages.
  • Check for port availability to prevent runtime failures.

Changing the port in a Spring Boot application is flexible and straightforward. You have complete control over choosing properties files, YAML, or programmatic configuration. Use the method that fits your project’s structure and environment.

Keywords for SEO: Spring Boot change port, configure Spring Boot port, server.port Spring Boot, change default port Spring Boot, Spring Boot application.properties port

Spring Issuesapplication propertiesspring-boot-common-errorsSpringserver port

Gopi Gorantala Twitter

Gopi is an Engineering Manager with over 14 years of extensive expertise in Java-based applications. He resides in Europe and specializes in designing and scaling high-performance applications.

Comments


Related Posts

Members Public

Spring Boot Hello World Tutorial with Lombok and H2 Database – Quick Start for Beginners

Learn how to create a Hello World Spring Boot application using Lombok for cleaner code and the H2 in-memory database for rapid development. This step-by-step guide includes annotations, project setup, REST API, H2 console access, and more to kickstart your Spring Boot journey.

Spring Boot Hello World Tutorial with Lombok and H2 Database – Quick Start for Beginners
Members Public

How to fix port 8080 already in use error on Windows and macOS

Getting the “Web server failed to start. Port 8080 was already in use” error? Learn how to identify and kill the process blocking port 8080 on Windows and macOS using simple terminal commands. Fix server startup issues fast and keep your development running smoothly.

How to fix port 8080 already in use error on Windows and macOS
Members Public

Spring vs. Spring Boot: Key Differences Developers Must Know

Spring or Spring Boot? One gives you full control, the other gets you up and running fast. If you're building Java apps, knowing the difference isn't optional—it’s critical. Here’s the breakdown every dev should read.

Spring vs. Spring Boot: Key Differences Developers Must Know