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

How to specify spring.profiles.active when springboot starts

王林
Release: 2023-05-12 22:49:04
forward
4002 people have browsed it

    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 &
    Copy after login

    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 &
    Copy after login

    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
    Copy after login

    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 &
    Copy after login

    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
    Copy after login

    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!

    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template