Home >Java >javaTutorial >Idea how to start multiple SpringBoot projects

Idea how to start multiple SpringBoot projects

WBOY
WBOYforward
2023-05-28 18:46:253253browse

    1. Preparation

    Use Idea to build a hello world Spring Boot project.

    Development environment description:

    (1) Spring Boot 2.7.0

    (2) Idea: IntelliJ IDEA 2022.2.2

    (3) OS : Mac OS

    The environment is different, some operations are slightly different, but the overall idea is the same.

    2. Start multiple SpringBoot

    2.1 Solution 1: Modify the port of the configuration file

    In the Spring Boot project, the port number can be configured in the configuration file, so the final The simple solution that can be thought of is to modify the port configuration server.port in the configuration file application.(properties/yml). The specific operations are:

    (1) First configure server.port = 8080, and run the startup class to start the application.

    (2) Modify server.port = 8081 and run the startup class to start the application.

    This will start two Spring Boot applications with different ports. Although this method is simple, its operation experience is not friendly, and modifying the configuration file in an actual project will definitely not work.

    2.2 Solution 2: Edit Configurations of Idea tool

    Use the Edit Configurations provided by Idea to configure application parameters.

    Idea how to start multiple SpringBoot projects

    Click Modify Options to add application parameters:

    Idea how to start multiple SpringBoot projects

    The interface may be slightly different depending on the version of the development tool. , but you can all find Program arguments:

    Idea how to start multiple SpringBoot projects

    Description:

    (1) VM options: VM options is actually the runtime environment we need in the program Variable, it needs to start with -D or -X or -XX, each parameter is separated by a space eg: -Dspring.profiles.active=dev

    (2) Program arguments: Program arguments are passed into main for us String array args[] of the method, which usually starts with --, such as --spring.profiles.active=dev; which is equivalent to -Dspring.profiles.active=dev. If both exist, the Program arguments configuration takes precedence.

    Then add a --server.port=8081 configuration:

    Idea how to start multiple SpringBoot projects

    2.3 Solution 3: Use RunDashboard

    IDEA provides powerful Dashboard functionality (Run Dashboard) can manage the above multi-application startup instances very well, visualize the projects we configure, facilitate the reconfiguration, Run, and Debug of the spring boot project, and simplify our operation steps.

    Add the following configuration in the .idea/workspace.xml file:

    <component name="RunDashboard">
      <option name="configurationTypes">
        <set>
          <option value="SpringBootApplicationConfigurationType" />
        </set>
      </option>
    </component>

    After starting again, you can see the Run Dashboard at the bottom

    Idea how to start multiple SpringBoot projects

    You can copy a configuration here:

    Idea how to start multiple SpringBoot projects

    Idea how to start multiple SpringBoot projects

    In this way, you can start multiple ones:

    Idea how to start multiple SpringBoot projects

    3. Two Spring Boot startups on the same port

    SpringBoot comes with Tomcat, just run SpringApplication.run in the main method directly, and it is not required for access With project name.

    If there are two SpringBoot projects in the idea and the access paths of the controller layer are the same, and since there is no path name, two main methods cannot be run at the same time. If two main methods are run at the same time, the port number must be occupied. How to start two projects at the same time.

    You only need to deploy the war package to the Tomcat server, and there is no need to use SpringBoot's built-in Tomcat server. To access, just enter localhost:8080/project name/path in the URL. In the development tools, you can also use external Tomcat to start.

    The above is the detailed content of Idea how to start multiple SpringBoot projects. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete