__CONFIG_colors_palette__{"active_palette":0,"config":{"colors":{"40f3f":{"name":"Main Accent","parent":-1}},"gradients":[]},"palettes":[{"name":"Default","value":{"colors":{"40f3f":{"val":"var(--tcb-color-0)"}},"gradients":[]},"original":{"colors":{"40f3f":{"val":"rgb(127, 215, 132)","hsl":{"h":123,"s":0.52,"l":0.67,"a":1}}},"gradients":[]}}]}__CONFIG_colors_palette__

__CONFIG_colors_palette__{"active_palette":0,"config":{"colors":{"40f3f":{"name":"Main Accent","parent":-1}},"gradients":[]},"palettes":[{"name":"Default","value":{"colors":{"40f3f":{"val":"var(--tcb-color-0)"}},"gradients":[]},"original":{"colors":{"40f3f":{"val":"rgb(127, 215, 132)","hsl":{"h":123,"s":0.52,"l":0.67,"a":1}}},"gradients":[]}}]}__CONFIG_colors_palette__
Table of Contents

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

 

Introduction

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 application.properties file.

server.port = 8090

Set server port property in application.yml file

server:
     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. 

@Component
public class CustomizationPort implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

 @Override
 public void customize(ConfigurableServletWebServerFactory server) {
  server.setPort(9001);
 }
}

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

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

 

[pullquote align=”normal”] The port defined inside the CustomContainer always overrides the value defined inside application.properties. [/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 application.properties 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 application-development.properties.

Set server.port property inapplication-development.properties file.

server.port = 9001

Set server.port property inapplication-production.properties 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 (application.properties file).
  4. Configuration at the @SpringBootApplication class.

 

Summary

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.

0 0 vote
Article Rating

Related Posts

Java Development Journal


Hello!! Welcome to the Java Development Journal. We love to share our knowledge with our readers and love to build a thriving community.

Subscribe
Notify of

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

6 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
6
0
Would love your thoughts, please comment.x
()
x