EN
Spring Boot 2 - change default application port from source code
9
points
To change change default application port from source code with Spring Boot 2 it is necessary to create own bean that overrides default port configuration. This article shows how to do it.
Notes:
- read this article to see different methods how to change default port with Spring Boot,
- read this article to see the exact place where to keep bean configurations.
Spring Boot 2.x.x - web server factory bean
In this approach webServerFactory()
method that returns ConfigurableServletWebServerFactory
object is used.
ServerConfig.java
file:
package com.dirask.examples;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ServerConfig {
@Bean
public ConfigurableServletWebServerFactory webServerFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.addConnectorCustomizers(connector -> {
connector.setPort(80);
});
return factory;
}
}
ServerConfig.java
file location: