Home  >  Article  >  Java  >  How to specify spring.profiles.active when springboot starts

How to specify spring.profiles.active when springboot starts

王林
王林forward
2023-05-12 22:49:043678browse

    springboot startup specifies spring.profiles.active

    Maven startup specifies Profile through -P,

    such as mvn spring-boot:run -Ptest

    But this is Maven's Profile.

    If you want to specify spring.profiles.active of spring-boot,

    • spring-boot 1.x use mvn spring-boot:run -Drun.profiles=test ,

    • spring-boot 2.x uses mvn spring-boot:run -Dspring-boot.run.profiles=test.

    If you use the command line to run the jar file directly, use java -jar -Dspring.profiles.active=test demo-0.0.1-SNAPSHOT.jar

    If Use development tools to run the Application.java file to start,

    then add parameters - -spring.profiles.active=test

    At the same time, pay attention to the problem that the startup does not take effect:

    In the linux centos 7 system, release the jar package

    nohup java -Xmx256m -jar xxxx --spring.profiles.active=prod &

    Every time it is started, the test environment is started

    After various troubleshooting, it was finally discovered that the startup class does not exist Pass in the parameters args

    How to specify spring.profiles.active when springboot starts

    SpringBoot activates profiles. Do you know several ways?

    Multiple environments are one of the most common configuration isolation methods, and can be run according to different The environment provides different configuration information to deal with different business scenarios. SpringBoot supports multiple configuration isolation methods, and single or multiple configuration files can be activated.

    How to activate Profiles

    Activated profiles must create a corresponding configuration file in the project, in the format of application-{profile}.yml.

    1. Command line method

    The command line method is an external configuration method. When executing the java -jar command, you can use the –spring. Use profiles.active=test to activate the specified profiles list.

    The usage method is as follows:

    java -jar order-service-v1.0.jar --spring.profiles.active=dev &> order-service.log &

    System variable method

    1. Mac/Linux system configuration environment variable

    Edit Environment variable configuration file /etc/profile, add an environment variable named SPRING_PROFILES_ACTIVE, as shown below:

    spring environment activation

    export SPRING_PROFILES_ACTIVE=dev

    2. Windows system configuration environment variables

    For how to configure environment variables, please refer to Java environment variable configuration. Create a new system environment variable named SPRING_PROFILES_ACTIVE and set the value of the variable to dev.

    The system variable method is suitable for SpringBoot applications deployed in a unified environment under the system. For example, all applications deployed in a unified environment are prod environment applications.

    Java system property mode

    Java system property mode is also an external configuration method. When executing the java -jar command, it can be activated and specified through -Dspring.profiles.active=test. List of profiles.

    The usage method is as follows:

    java -Dspring.profiles.active=dev -jar order-service-v1.0.jar &> order-service.log &

    Note: The -D method to set Java system properties must be defined before -jar.

    Configuration file method

    The configuration file method is the most commonly used method, but it is not very flexible and has great limitations. It is not recommended to use this method to activate the configuration file.

    We only need to add configuration in the application.yml configuration file. The usage method is as follows:

    spring:
      profiles:
        # 激活profiles
        active: dev

    Priority

    Command line method> Java System property mode> System variable mode> Configuration file mode

    After testing, the command line mode has the highest priority, while the internal configuration file mode is the lowest.

    Activate multiple profiles

    If you need to activate multiple profiles, you can use commas to separate them, such as: - -spring.profiles.active=dev,test

    Key points on the blackboard

    Every application project will use a large number of configuration files or external configuration centers, and the activation of configuration information is an essential step and is particularly important.

    It is recommended that you use system environment variables to activate the configuration of the specified profile. This method is relatively simple and can be used globally in the system (note: the global system means that all SpringBoot applications running under the system will use it. configuration), of course, it can also be specified individually using priority replacement rules.

    The above is the detailed content of How to specify spring.profiles.active when springboot starts. 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