Change the default port in Spring Boot

In this short post, we are going to cover the common ways to change the default port in Spring Boot.



Spring Boot comes with the ability to give sensible defaults based on the application configuration. For example, in the case of web application, it comes with an embedded servlet container (tomcat) with defaults values configured for the servlet container. There are a few cases where we like to override these values with our custom values. In this post, we are going to learn the different options to change the default port in Spring Boot application.


1. Change Port using Properties Files

Spring Boot provides a flexible way to configure our application using a property file. To change the default port, we need to set the desired port number using server.port properties either through application.prioperties or application.yml file.

Set server.port property in file.

server.port = 8090

Set server port property in application.yml file

     port: 8090


2. Programmatic Customization

We have the option to programmatically configure your embedded servlet container. To do this, create a Spring bean which implements the WebServerFactoryCustomizer interface. 

public class CustomizationPort implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

 public void customize(ConfigurableServletWebServerFactory server) {

There is other option to set the property in the main @SpringBootApplication class:

public class ChangeApplicationPort {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(ChangeApplicationPort.class);
          .singletonMap("server.port", "9001"));;


[pullquote align=”normal”] The port defined inside the CustomContainer always overrides the value defined inside [/pullquote]


3. Command Line Argument

We also have the option to set the port while starting our application. This is done by passing the argument through the command line.

java -Dserver.port=9001 -jar demo.jar
java -jar demo.jar –server.port=9001


4. Environment Specific Port

Spring Profiles provides a powerful and easy way to control code and configuration based on the environment. Using Spring Profiles its possible to segregate parts of our application and make it only available in certain environments. We can use the same feature to change the default port based on the environment or profile.

To use profile specific configuration files, we need to the naming convention of application-{profile}.propertieswhere profile defines the name of the intended profile. It will load profile from the same location as file. Let’s take an example, where we want to run our server on 9001 port on the dev environment while on the production we like to run our application on 9022 port.

To do this using configuration files, we will define 2 configuration files, namely application-production.propertiesand

Set server.port property file.

server.port = 9001

Set server.port property file.

server.port = 9022


5. Configuration Evaluation Order

Spring Boot follows a certain order to load the configuration properties. While overriding the changes, please remember the sequence used by Spring Boot to load these configurations:

  1. Embedded Server configuration.
  2. Command line arguments.
  3. Property file ( file).
  4. Configuration at the @SpringBootApplication class.



In this quick post, we cover the common ways to change the default port in Spring Boot. We learned how to do this using property file or by passing the port number through the command line argument.

Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments