> ## Content Index
> Fetch the complete content index at: https://www.ggorantala.dev/llms.txt
> Use this file to discover other available public pages before exploring further.

# How To Configure a Port For a Spring Boot Application
- URL: https://www.ggorantala.dev/how-to-configure-port-for-a-spring-boot-application/
- Published: 2024-08-16T12:17:26.000Z
- Updated: 2025-05-06T02:11:01.000Z
- Description: 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.
- Author: Gopi Gorantala
- Tags: Spring Issues, application properties, spring-boot-common-errors, Spring, server port

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:

```yml
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`:

```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:

```java
@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
java -jar myapp.jar --server.port=9090

```

Or in Maven:

```java
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