Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to activate profiles in SpringBoot

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to activate profiles in SpringBoot". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

How to activate Profiles

The active profiles creates a corresponding configuration file within the project in the format of application- {profile} .yml.

Command line mode

Command-line mode is an external configuration, and the specified profiles list can be activated by-- spring.profiles.active=test when executing the java-jar command.

The usage is as follows:

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

System variable mode

Mac/Linux system configuration environment variables

Edit the environment variable configuration file / etc/profile, and add an environment variable named SPRING_PROFILES_ACTIVE, as follows:

# spring environment activates export SPRING_PROFILES_ACTIVE=dev

Windows system configuration environment variables

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

The way of system variable is applicable to the SpringBoot applications in the unified environment deployed under the system, such as the unified deployment of applications in the prod environment.

Java system attribute mode

The Java system attribute mode is also an external configuration mode, and the specified profiles list can be activated by-Dspring.profiles.active=test when executing the java-jar command.

The usage is as follows:

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

Note: the Java system property set in-D mode should be defined before-jar.

Configuration file mode

Configuration file is the most commonly used method, but it is not flexible and limited, so it is not recommended to activate the configuration file.

We just need to add the configuration in the application.yml configuration file, which is used as follows:

Spring: profiles: # activate profiles active: dev

Priority

Command line mode > Java system attribute mode > system variable mode > configuration file mode

The tested command line approach has the highest priority, while the internal profile approach is the lowest.

Activate multiple profile

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

Knock on the blackboard and draw the key points

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

It is recommended that you use the system environment variable to activate the configuration of the specified profile. This method is relatively simple and can be used globally (Note: the global system represents that all SpringBoot applications running under the system will use this configuration). Of course, you can also use the priority replacement rule to specify separately.

This is the end of "how to activate profiles in SpringBoot". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report