Table of Contents
1. Multi-environment switching in SpringBoot
2. Configuration file loading order in SpringBoot
Home Java javaTutorial How does SpringBoot load multiple configuration files to switch between dev and product environments?

How does SpringBoot load multiple configuration files to switch between dev and product environments?

May 12, 2023 pm 11:58 PM
dev springboot product

1. Multi-environment switching in SpringBoot

In SpringBoot, in addition to application.properties, the file names of other configuration files we create need to meet application-{profile}.properties format, where {profile} corresponds to your environment identifier (not necessarily a .properties file, it can also be .yml) and its corresponding {profile} value is customized by the developer (such as dev, product), when starting the project, you only need to add the corresponding parameters, and springboot will read the configuration file. The specific profile configuration is set in the application.properties file through the spring.profiles.active property. Next, we use an example to illustrate

(1) First, five configuration files of dev, product, qa, stage and default application are created here

How does SpringBoot load multiple configuration files to switch between dev and product environments?

(2) When loading the configuration file, the application.properties configuration file will be loaded first (some public configurations are generally stored here), and the configuration file of the environment to be loaded is configured in this file. There are two configuration methods.

For example, to load the dev environment, you can configure it like this in application.properties

1

spring.profiles.active=dev

Copy after login

or use @spring.profiles.active@, as shown below

1

spring.profiles.active=@spring.profiles.active@

Copy after login

If you use this In this way, you need to add the following content to pom.xml, where the activeByDefault tag specifies the configuration file that is loaded by default when the project starts.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

<profiles>

  <profile>

    <id>dev</id>

    <activation>

      <activeByDefault>true</activeByDefault>

    </activation>

    <properties>

      <spring.profiles.active>dev</spring.profiles.active>

    </properties>

  </profile>

  <profile>

    <id>qa</id>

    <properties>

      <spring.profiles.active>qa</spring.profiles.active>

    </properties>

  </profile>

  <profile>

    <id>stage</id>

    <properties>

      <spring.profiles.active>stage</spring.profiles.active>

    </properties>

  </profile>

  <profile>

    <id>product</id>

    <properties>

      <spring.profiles.active>product</spring.profiles.active>

    </properties>

  </profile>

</profiles>

Copy after login

When the mvn clean package -P dev command is executed to package and publish the project, @spring.profiles.active@ in the configuration file in the jar/war package will be replaced with dev.

Note@spring.profiles.active@ must be consistent with the label <spring.profiles.active></spring.profiles.active>> in the pom, otherwise it will Report an error.

How does SpringBoot load multiple configuration files to switch between dev and product environments?

2. Configuration file loading order in SpringBoot

Priority sorting of configuration files(Which configuration file is the Accurate):

1. The config directory under the project root directory. [Highest priority]
2. Project root directory.
3. The config directory under classpath.
4. Classpath directory (the default location of application.properties when creating a new project). [Lowest priority]

The loading order of configuration files is opposite to the order of priority. The one with lower priority is loaded first, because if there are duplicate configurations, the configuration file loaded first will be overwritten.

In the same level directory, if application.yml and application.properties configuration files exist at the same time, the application.properties configuration file will prevail, that is to say Load the .yml file first and then the .properties file.

How does SpringBoot load multiple configuration files to switch between dev and product environments?

Priority order: 1->2->3->4->5.

File loading order: 5->4->3->2->1.

The above is the detailed content of How does SpringBoot load multiple configuration files to switch between dev and product environments?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution to win10 snmp installation failure error code 0x8024402C Solution to win10 snmp installation failure error code 0x8024402C Feb 13, 2024 am 08:30 AM

The snmp protocol is a simple network management protocol. This protocol can support the network management system and is used to monitor whether the devices connected to the network have any situation that causes management concern. However, many users have recently reported that the win10 installation of snmp failed with the error code 0x8024402C. what to do? Users can start Powershell as an administrator to set up. Let this site carefully introduce to users the solution to the error code 0x8024402C when installing snmp in win10. Solution to win10 snmp installation failure error code 0x8024402C 1. Start Powershell as administrator 1. The first step is to run the win10 system

How Springboot integrates Jasypt to implement configuration file encryption How Springboot integrates Jasypt to implement configuration file encryption Jun 01, 2023 am 08:55 AM

Introduction to Jasypt Jasypt is a java library that allows a developer to add basic encryption functionality to his/her project with minimal effort and does not require a deep understanding of how encryption works. High security for one-way and two-way encryption. , standards-based encryption technology. Encrypt passwords, text, numbers, binaries... Suitable for integration into Spring-based applications, open API, for use with any JCE provider... Add the following dependency: com.github.ulisesbocchiojasypt-spring-boot-starter2. 1.1Jasypt benefits protect our system security. Even if the code is leaked, the data source can be guaranteed.

Detailed tutorial on installing Carla on Ubuntu 20.04 system. Detailed tutorial on installing Carla on Ubuntu 20.04 system. Feb 19, 2024 pm 08:20 PM

Carla is an open source autonomous driving simulation platform designed for developing and testing autonomous driving algorithms. The following is a detailed tutorial for installing Carla on Ubuntu20.04 system: Install dependencies: Open a terminal window and run the following command to install Carla’s dependencies: sudoaptupdatesudoaptinstall-ybuild-essentialclang-10llvm-10python3-pippython3-devlibpng-devlibjpeg-devlibtiff5-devlibopenexr -devlibhdf5-devlibsquish-de

How to use Redis to implement distributed locks in SpringBoot How to use Redis to implement distributed locks in SpringBoot Jun 03, 2023 am 08:16 AM

1. Redis implements distributed lock principle and why distributed locks are needed. Before talking about distributed locks, it is necessary to explain why distributed locks are needed. The opposite of distributed locks is stand-alone locks. When we write multi-threaded programs, we avoid data problems caused by operating a shared variable at the same time. We usually use a lock to mutually exclude the shared variables to ensure the correctness of the shared variables. Its scope of use is in the same process. If there are multiple processes that need to operate a shared resource at the same time, how can they be mutually exclusive? Today's business applications are usually microservice architecture, which also means that one application will deploy multiple processes. If multiple processes need to modify the same row of records in MySQL, in order to avoid dirty data caused by out-of-order operations, distribution needs to be introduced at this time. The style is locked. Want to achieve points

How to solve the problem that springboot cannot access the file after reading it into a jar package How to solve the problem that springboot cannot access the file after reading it into a jar package Jun 03, 2023 pm 04:38 PM

Springboot reads the file, but cannot access the latest development after packaging it into a jar package. There is a situation where springboot cannot read the file after packaging it into a jar package. The reason is that after packaging, the virtual path of the file is invalid and can only be accessed through the stream. Read. The file is under resources publicvoidtest(){Listnames=newArrayList();InputStreamReaderread=null;try{ClassPathResourceresource=newClassPathResource("name.txt");Input

How to implement Springboot+Mybatis-plus without using SQL statements to add multiple tables How to implement Springboot+Mybatis-plus without using SQL statements to add multiple tables Jun 02, 2023 am 11:07 AM

When Springboot+Mybatis-plus does not use SQL statements to perform multi-table adding operations, the problems I encountered are decomposed by simulating thinking in the test environment: Create a BrandDTO object with parameters to simulate passing parameters to the background. We all know that it is extremely difficult to perform multi-table operations in Mybatis-plus. If you do not use tools such as Mybatis-plus-join, you can only configure the corresponding Mapper.xml file and configure The smelly and long ResultMap, and then write the corresponding sql statement. Although this method seems cumbersome, it is highly flexible and allows us to

Comparison and difference analysis between SpringBoot and SpringMVC Comparison and difference analysis between SpringBoot and SpringMVC Dec 29, 2023 am 11:02 AM

SpringBoot and SpringMVC are both commonly used frameworks in Java development, but there are some obvious differences between them. This article will explore the features and uses of these two frameworks and compare their differences. First, let's learn about SpringBoot. SpringBoot was developed by the Pivotal team to simplify the creation and deployment of applications based on the Spring framework. It provides a fast, lightweight way to build stand-alone, executable

How SpringBoot customizes Redis to implement cache serialization How SpringBoot customizes Redis to implement cache serialization Jun 03, 2023 am 11:32 AM

1. Customize RedisTemplate1.1, RedisAPI default serialization mechanism. The API-based Redis cache implementation uses the RedisTemplate template for data caching operations. Here, open the RedisTemplate class and view the source code information of the class. publicclassRedisTemplateextendsRedisAccessorimplementsRedisOperations, BeanClassLoaderAware{//Declare key, Various serialization methods of value, the initial value is empty @NullableprivateRedisSe

See all articles