"Reprint" Springboot Modify the default port number

Source: Internet
Author: User

Sometimes we may need to start more than one springboot, and the Springboot default port number is 8080, so at this point we need to modify the default ports of Springboot. There are two ways to modify the default port for Springboot. Here are the two ways to talk about each one.

Modify Application.properties

The first way we only need to add such a sentence in application.properties: server.port=8004. Why is this way possible to modify the default port for Springboot? Because there is such a class in Springboot: Serverproperties. We can take a look at this class in general:

[Java]View PlainCopy
    1. @ConfigurationProperties (prefix =   "server",  ignoreunknownfields = true)    
    2. public class  serverproperties  
    3.          implements embeddedservletcontainercustomizer, environmentaware,  ordered {  
    4.   
    5.     /** 
    6.      * server http  port. 
    7.      */   
    8.     private integer port;   

In this class there is a @configurationproperties annotation that reads the value of the springboot default configuration file application.properties into the bean. This defines a server prefix and a port field, so the value of Server.port is read from Application.properties when Springboot is started. Let's take a look down:

[Java]View PlainCopy
    1. @Override
    2. public void Customize (Configurableembeddedservletcontainer container) {
    3. if (getport () = null) {
    4. Container.setport (Getport ());
    5. }

Here is a customize method, which will give Springboot the port number to read.

Implementation Embeddedservletcontainercustomizer we saw that the port number was set in the Customize method, And this method is in Embeddedservletcontainercustomizer this interface, so we can implement this interface, to change the default port number of Springboot. The specific code is as follows: [Java]View PlainCopy
  1. @RestController
  2. @EnableAutoConfiguration
  3. @ComponentScan
  4. Public class Firstexample implements Embeddedservletcontainercustomizer {
  5. @RequestMapping ("/first.do")
  6. String Home () {
  7. return "Hello world! World Hello! O (∩_∩) o haha ~!!! I'm not too good! ";
  8. }
  9. public static void Main (string[] args) {
  10. Springapplication.run (firstexample.   class, args);
  11. }
  12. @Override
  13. public void Customize (Configurableembeddedservletcontainer configurableembeddedservletcontainer) {
  14. Configurableembeddedservletcontainer.setport (8003);
  15. }
  16. }
Then when you start Springboot, you find that the port number is changed to 8003.
Using command-line arguments if you just want to modify the port number at startup, you can modify the port number with the command-line arguments. The configuration is as follows: Java-jar after packaging Springboot.jar--server.port=8000 using the virtual machine parameters you can also put the configuration of the modified port number in the JVM parameters. The configuration is as follows:-dserver.port=8009. The port number that was started is changed to 8009. Reprinted from: 53433592

"Reprint" Springboot Modify the default port number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.