Learn how to change Spring Boot port with easy steps. Discover multiple methods including application properties, command line, programmatic configuration, and random port assignment.
1. Introduction
Spring Boot applications, by default, run on port 8080. However, there are situations where you might need to change this default port. In this guide, we’ll explore different ways to customize the port on which your Spring Boot application starts, including setting a random port. Whether you’re a beginner or looking to refine your Spring Boot skills, this tutorial has you covered.
2. Change Spring Boot Port Number
2.1 Using application.properties or application.yml
The most common way to change the default port is by modifying the application.properties
or application.yml
file.
For application.properties
:
server.port=9090
application.propertiesFor application.yml
:
server:
port: 9090
application.ymlReplace 9090
with the port number you want your application to run on. This is a straightforward way to handle a Spring Boot port change.
2.2 Using Command Line Arguments
You can also change the port by passing a command line argument when running your Spring Boot application.
server.port=9090
CMDOr if you’re using Maven:
mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=9090
CMDThis method is particularly useful when you want to override the port number without modifying the configuration files. It’s an easy way to implement a Spring Boot change port command line solution.
2.3 Programmatic Configuration
Programmatic configuration allows setting the port dynamically in a Spring Boot application. You can either set the port directly in the main @SpringBootApplication
class using SpringApplication.setDefaultProperties
, or customize the server configuration by implementing the WebServerFactoryCustomizer
interface and overriding the customize
method.
Initially, let’s delve into configuring the property within the main @SpringBootApplication
class:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.Collections;
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(HelloWorldApplication.class);
app.setDefaultProperties(Collections.singletonMap("server.port", "9093"));
app.run(args);
}
}
HelloWorldApplication.classAlternatively, we can configure the server port by creating a bean that implements the WebServerFactoryCustomizer interface:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class, args);
}
@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
return factory -> factory.setPort(9093);
}
}
HelloWorldApplication.classThis approach is ideal for those who need to change the port of embedded Tomcat in Spring Boot dynamically.
3. Order of Evaluation
When changing the port in Spring Boot, the order of evaluation is important to understand. The precedence of port settings in Spring Boot is as follows, from highest to lowest:
- Embedded server configuration (via
WebServerFactoryCustomizer
). - Command line arguments.
application.properties
orapplication.yml
files.- Programmatically set properties within
HelloWorldApplication
main class.
In this order, embedded server configuration set through WebServerFactoryCustomizer
has the highest precedence, followed by command line arguments, then properties in configuration files, and finally, programmatically set properties within HelloWorldApplication
have the lowest precedence.
4. Using a Random Port
You can configure your application to start on a random available port using Spring Boot’s server.port
property.
For a random port assignment, set the port to 0
in application.properties
:
server.port=0
application.propertiesOr in application.yml
:
server:
port: 0
application.ymlWith this configuration, Spring Boot will assign a random available port each time the application starts, ensuring it doesn’t conflict with other services running on the same machine.
Alternatively, if you prefer a specific range of ports, you can specify it like this:
server.port=${random.int(8501,8505)}
application.propertiesOr in application.yml
:
server:
port: ${random.int(8501,8505)}
application.ymlIn this case, Spring Boot will choose a random port from the specified range (8501 to 8505) on startup. Both approaches are useful for testing and preventing port conflicts.
5. Things to Consider
Here are some important considerations to keep in mind when configuring and managing ports in a Spring Boot application:
- Avoid Port Conflicts: Ensure that the port you choose for your Spring Boot application is not already in use by another application or service running on the same machine. Port conflicts can cause your application to fail to start.
- Firewall Rules: Update your firewall settings if necessary to allow traffic on the port your Spring Boot application is running on. This is important for external accessibility and network security.
- Dynamic Port Allocation: When using a random port, be aware that the assigned port may change each time the application starts. This can impact external systems or configurations that rely on a specific port number.
- Port Range Selection: If you choose to specify a port range, ensure that the range is wide enough to accommodate potential port allocations without causing conflicts. Consider factors such as the number of instances running simultaneously and any external integrations that may require specific ports.
- Environment-Specific Configurations: Manage port configurations appropriately across different environments (development, testing, production). Use environment-specific configuration files or environment variables to set the port dynamically based on the deployment environment.
- Load Balancing and Scaling: If your application is deployed in a clustered or load-balanced environment, consider how port configurations and routing will be handled across multiple instances to ensure seamless communication and scalability.
6. FAQs
Why would I need to change the default port in Spring Boot?
Changing the default port is often necessary to avoid conflicts with other applications running on the same machine or to comply with network policies.
How can I find the random port assigned to my Spring Boot application?
When using a random port, you can find the assigned port in the application logs. Spring Boot logs the port number on startup.
Can I change the port without modifying the configuration files?
Yes, you can change the port using command line arguments or programmatically in your Spring Boot application’s main class.
Is it possible to change the port dynamically in Spring Boot?
Yes, Spring Boot provides options to dynamically change the port, such as using random port assignment or programmatically setting the port based on conditions.
What is the default port for Spring Boot applications?
The default port for Spring Boot applications is 8080, but this can be changed based on configuration or programmatic settings.
Can I use a different port for each environment?
Yes, Spring Boot allows setting different ports for each environment using environment-specific configuration files or environment variables.
7. Conclusion
In conclusion, this guide comprehensively covers various methods to change the default port in Spring Boot applications. Whether through configuration files, command line arguments, or programmatic settings, developers have a range of options to manage port assignments effectively.
8. Learn More
Interested in learning more?
Spring Boot Smooth Startup: Performing Basic Checks Before Application Context Creation
Add a Comment