By default, Spring Boot runs on port 8080, you can check our article by building your first application with Spring Boot but you might need to change this for various reasons such as avoiding conflicts with other applications, meeting organizational policies, or deploying multiple instances on the same machine. In this article will explore different methods how to configure the port for a Spring Boot application and discuss some alternatives.
Configuring the Port via application.properties or application.yml
The most straightforward way to change the port and I think the most used method is by setting a property in the application.properties
or application.yml
file located in the src/main/resources
directory of your Spring Boot project.
Using application.properties:
server.port=9090
Using application.yml:
server:
port: 9090
Do you use a different approach that we haven’t mentioned here? Share your techniques and experiences!
Setting the Port Programmatically
In some cases, you might want to set the port programmatically, especially if you need to determine the port dynamically based on some logic. This can be achieved by configuring the EmbeddedServletContainerCustomizer
bean.
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class WebServerConfig {
@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
return factory -> factory.setPort(9090);
}
}
Command Line Arguments
Another convenient way to set the port is by passing it as a command-line argument when starting the application. This is particularly useful in different environments (development, testing, production) without changing the configuration files.
java -jar taskapp.jar --server.port=9090
Environment Variables
You can also use environment variables to configure the port. This method is useful for Docker containers or cloud environments where you want to externalize the configuration.
Setting Environment Variables in Unix-based systems:
export SERVER_PORT=9090
Setting Environment Variables in Windows:
set SERVER_PORT=9090
In the application.properties
file, you can refer to the environment variable using the ${}
syntax:
server.port=${SERVER_PORT}
Using a Custom Port File
If you prefer to externalize the port configuration to a separate file, you can use the application.properties
or application.yml
in conjunction with a custom configuration file.
- Create a file named
port.config
with the following content:server.port=9090
- Modify your
application.properties
to include the custom configuration file:properties spring.config.import=optional:classpath:port.config
Check out Java 8 Streams.
Using Spring Boot Profiles
Spring Boot’s profile-specific configuration files allow you to define different configurations for different environments. You can create multiple profile-specific properties files such as application-dev.properties
and application-prod.properties
with different port settings.
application-dev.properties
:
server.port=8081
application-prod.properties
:
server.port=8082
Activate the profile using a command-line argument:
java -jar taskapp.jar --spring.profiles.active=dev
How do you manage port configurations in a multi-environment setup, let us know in the comments?
Choosing the appropriate method depends on your specific use case and deployment environment. Each method has its advantages, allowing you to tailor the configuration to your needs.
Which method do you prefer for configuring the port in your Spring Boot applications, and why?