How to Change Port for Spring Boot Applications

How to Change Port for Spring Boot Applications


Spring Boot applications ship with an embedded server, and the default port for them is 8080. Whether some other service already inhibits this port, or whether you'd like to create a new microservice on a new one - in this guide, we'll take a look at how to configure the port of a Spring Boot application. and application.yml

There are two types of properties files typically used in Spring Boot projects - and application.yml.

The file follows a simple key-value format, where each line represents a new key. The application.yml file follows the YAML format.

Both of these are very human-readable and straightforward and typically, when you start out with a skeleton project, the server.port is the only setting you'll have.


server.port = 8090

For application.yml:

  port: 8090

Note: You can set the port to a random available port, by setting it to 0. Which is then obtainable through @Value("${local.server.port}").

Set Spring Boot Port Programmatically

You can additionally set the port programmatically, since most aspects of customization are ultimately expressible through classes and annotations in Spring Boot.

The WebServerFactoryCustomizer interface allows us to customize the server factory. The server factory will "consult" any bean of this type before configuring the server itself - so you can set the port, address, error pages, etc. this way as well.

The customize() method is the only one exposed in the interface, and whatever you don't set in the method is left to the default values. If you just want to change the port, you simply invoke the setPort() method of the generic factory instance. The default factory is TomcatServletWebServerFactory:

public class ChangePort implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
    public void customize(TomcatServletWebServerFactory factory) {

For a generic servlet factory, you can use:

public class ChangePort implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
    public void customize(ConfigurableWebServerFactory factory) {

Spring Boot automatically picks up beans that implement WebServerFactoryCustomizer and just by defining it, you can alter the server startup process.

Why would you set it programmatically instead of through a configuration file?

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

Changing a configuration file programmatically is a bigger hassle than setting the port programmatically. When deploying applications to remote hosts, you might have to read the proposed port of the application through environment variables or another configuration file and set the application's port based on the input data.

In that case, instead of reading and altering a file, it's much simpler and cleaner to simply read the environment variables, and set the port based on it.

Set Port with CLI

Finally, you can easily change the port of an application when starting it up, via the CLI. You may not be using the CLI to start up the application, in which case, the previous two options are probably better suited for you.

If you're using Maven, when starting up your application, include a JVM argument:

$ mvn spring-boot:run'-Dserver.port=8090'

Or, if you're using Spring's own CLI, you can pass in command-line arguments separate from Spring's command arguments by adding an additional, blank --:

$ spring run application_name.groovy -- --server.port=8090


In this short tutorial, we've taken a quick look at three ways to change the default port of a Spring Boot application. You can change the default port by modifying the appropriate properties file, programmatically by implementing a WebServerFactoryCustomizer or via the CLI.

Last Updated: January 19th, 2022
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

David LandupAuthor

Entrepreneur, Software and Machine Learning Engineer, with a deep fascination towards the application of Computation and Deep Learning in Life Sciences (Bioinformatics, Drug Discovery, Genomics), Neuroscience (Computational Neuroscience), robotics and BCIs.

Great passion for accessible education and promotion of reason, science, humanism, and progress.

Make Clarity from Data - Quickly Learn Data Visualization with Python

Learn the landscape of Data Visualization tools in Python - work with Seaborn, Plotly, and Bokeh, and excel in Matplotlib!

From simple plot types to ridge plots, surface plots and spectrograms - understand your data and learn to draw conclusions from it.

Want a remote job?

    © 2013-2022 Stack Abuse. All rights reserved.