In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the principle and function of Spring Boot Profiles". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the principle and function of Spring Boot Profiles"?
Introduction to Spring Boot Profiles
The concept of Profile actually existed in Spring Framework a long time ago. In version 3.1 of Spring Framework, the annotation @ Profile and the abstraction of Environment environment configuration were introduced, but the Profiles function was further extended in the Spring Boot framework to make it one of the Spring Boot features, which is separately found in the official document 25. As described in the Profiles section, Spring Boot Profiles is also called Spring Profiles in the document.
So what is Spring Profiles and why do you need it? Let's start with a familiar scenario: when we develop a project, we often need to modify the configuration according to different environments, for example, local development will load the native configuration and development environment database, and when deployed on the test server, we need to load the test environment configuration and database. Similarly, when the project releases the production environment, it needs to be set to the production environment configuration and database. In this way, different environment deployments require additional processing to adjust the configuration of the environment, which is very tedious to maintain and error-prone.
In order to solve the problem of configuration switching required by different environments, Spring Profiles provides a way for us to specify that only the corresponding program configuration is loaded in a specific environment, and each environment configuration corresponds to a Profile. Only when the current Profile is active, the configuration and Bean corresponding to the Profile will be loaded into the Spring program.
Spring Profiles is a solution for applications that require different configurations to load in different environments.
Of course, Spring allows multiple Profile to be activated, such as subdividing the application configuration into database configuration, message middleware configuration, cache configuration, etc., all define different Profile names for different environments, and specify multiple Profile when you need to activate the corresponding configuration of the environment.
Spring Profiles actual combat
There are two ways to use Profiles:XML configuration and the annotation @ Profile in the Spring program.
XML configuration definition Profile
Although XML configuration is less and less used now, let's briefly introduce that we usually have a root element when we define the Bean in the XML file. An attribute profile is added to the beans element to specify the environment, for example, the profile of the development environment is defined as dev, and the profile of the production environment is: prod.
It is important to note that you must use Spring XML Beans Schema version 4.0 or above to support the profile attribute. After the definition of the XML file, we only need to activate the specified Profile name to load the corresponding Bean object. There are two main ways to activate it in the Spring program:
In Java API mode, get the environment Bean of the current Spring container, set the activeProfiles property, and then start the container
Specified by startup parameters, fixed format:-Dspring.profiles.active=dev
Note @ Profiles definition Profile
It is also easy to define Profile using annotations. Introduce a new annotation @ Profiles. @ Profiles is usually used with @ Component or @ Configuration, as shown in the following example:
The way to activate Profile is the same, as long as the specified Profile is activated, its corresponding Bean will be loaded. In Spring programs, Profile defaults to default. Currently, we can modify it through spring.profiles.default configuration or org.springframework.core.env.AbstractEnvironment#setDefaultProfiles API.
Spring Boot Profile actual combat
OK, now let's take a look at how to use Profile in a Spring Boot program. Usually the configuration file of a Spring Boot program is in yml or properties format. Because the structure of the yml format file is concise and read, we can take a look at how to define Profile and the corresponding configuration in application.yml.
Unlike yml format files, different Profile cannot be set in a properties file. The official naming format is applications-$ {profile} .properties to achieve the same effect. In order to see the effect of the activation of the specified Profile, we can get the current environment configuration variables by activating different Profile launchers and requesting the / enviroment interface in the following example.
Here we describe how to activate Profile in the configuration file: add the following configuration at the top of application.yml to indicate that the currently activated Profile is prod. Of course, you can also activate it with the startup parameters described earlier:
Then start the program, and access http://localhost:9000/enviroment in curl mode to get the following output:
Similarly, if the value of the active attribute above is specified as dev, the output is: current app enviroment is prod.
Activate Profile in Spring Boot API mode
In the Spring Boot program, in addition to the above methods to activate Profile, you can also use Spring Boot API to activate:
SpringApplication.setAdditionalProfiles (…)
SpringApplicationBuilder.profiles (...)
But it should be noted that if you use Spring Boot API, you need to set it before the program starts, that is, SpringApplication.run (.) Method is executed, otherwise there is no effect. The Profile added in Spring Boot API is an additional activated Profile, that is, overwriting the Profile specified by the external incoming spring.profiles.activie.
Summary
In Spring Boot programs, we usually define configuration files for different Profiles, such as application- {profile} .properties. Set spring.profiles.active=dev in the default configuration file application.properties for normal development. When you need to package and upload the server, you can load the configuration of the corresponding environment by specifying the Profile launcher of the corresponding environment through the startup parameter jar-Dspring.profiles.active=prod xxx.jar. Here we learn how to deal with the switching of different environment configurations in the program through the Spring Boot Profiles feature, hoping to help the partners at work, and welcome them to share their practical experience on how to deal with the project environment configuration. If there are any mistakes or inadequacies, please criticize and correct them and learn and communicate together.
Sample code
The sample code in this article can be obtained from the following warehouse address:
Springboot-actions-profiles: https://github.com/wrcj12138aaa/springboot-actions-profiles
Environmental support:
JDK 8
SpringBoot 2.1.6
Maven 3.6.0
At this point, I believe you have a deeper understanding of "what is the principle and function of Spring Boot Profiles". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.