Several ways for spring boot to modify the default port number

Keywords: Spring SpringBoot encoding Tomcat

  • Modify application.properties

In the first way, we just need to add a sentence like this in application.properties: server.port=8004. Why can I modify the default port of spring boot in this way? Because there is such a class in SpringBoot: ServerProperties. Let's take a look at this class in general:

@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties
		implements EmbeddedServletContainerCustomizer, EnvironmentAware, Ordered {
 
	/**
	 * Server HTTP port.
	 */
	private Integer port;

In this class, there is an @ ConfigurationProperties annotation, which will read the value of the default configuration file application.properties of SpringBoot and inject it into the bean. A server prefix and a port field are defined here, so the value of server.port will be read from application.properties when spring boot is started. Let's look down:
 

        @Override
	public void customize(ConfigurableEmbeddedServletContainer container) {
		if (getPort() != null) {
			container.setPort(getPort());
		}

Here is a customize method, which will set the port number read to spring boot.

  • Implement EmbeddedServletContainerCustomizer

We see above that the port number is set in the customize method, which is in the EmbeddedServletContainerCustomizer interface, so we can implement this interface to change the default port number of SpringBoot. The specific code is as follows:

@RestController
@EnableAutoConfiguration
@ComponentScan
public class FirstExample implements EmbeddedServletContainerCustomizer {
 
    @RequestMapping("/first.do")
    String home() {
        return "Hello World!Hello world! O(∩_∩)O Ha-ha~!!!I'm not very good!";
    }
 
    public static void main(String[] args) {
 
        SpringApplication.run(FirstExample.class, args);
    }
 
    @Override
    public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
 
        configurableEmbeddedServletContainer.setPort(8003);
    }
}

Then when you start spring boot, the port number is changed to 8003

  • Specify ports by encoding

Add the servletContainer method to the startup class

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Bean
    public TomcatServletWebServerFactory servletContainer(){
        return new TomcatServletWebServerFactory(8081) ;
    }

}
  • Using command line parameters

If you just want to change the port number at startup, you can use the command line parameters to change the port number. The configuration is as follows: SpringBoot.jar -- server.port=8000 after Java jar packaging

  • Using virtual machine parameters

You can also change the port number configuration into the JVM parameters. The configuration is as follows: - Dserver.port=8009. In this way, the starting port number is changed to 8009.

You can select the corresponding configuration mode according to your business requirements.

From: Spring boot changes the default port number

spring boot specifies the boot port

Posted by paperthinT on Mon, 04 May 2020 13:25:32 -0700