Get the App
SLTechnology News&Howtos  ›  Development  › 

How to replace configuration files with SpringBoot

Shulou Source: shulou.com Published: 2022-06-02 09:12:16 11月15日 Update

This article mainly introduces SpringBoot how to achieve the replacement of configuration files, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Replacement of SpringBoot configuration files with spring.profiles.active

At work, the problem that must be encountered when testing or launching is to modify the configuration. Generally speaking, several configurations are available: dev (development environment), test (test environment), and pro (production | online environment). At this point, we need to comment out all the configurations in the configuration file, and then unlock the required configurations. At this point, this problem can be solved through the configuration of spring.profiles.active.

Application.properties

Create different properties files, such as application-dev.properties,application-test.properties,application-pro.properties, according to different environments.

Create an application.properties as follows:

Spring.profiles.active=dev # test testing, pro production

Then change the different configuration by modifying the application.properties.

Application.yml

As above, different yml profiles are created according to different environments, distinguished by suffixes (e.g. application-dev.yml).

Create an application.yml as follows:

Spring: profiles: active: test # dev development, pro production

As above, you only need to change the application.yml file to modify the configuration file.

SpringBoot read profile summary 1, use annotation @ ConfigurationProperties mapping

You can automatically map the configuration in the configuration file to the entity application.properties configuration by annotating @ ConfigurationProperties (the prefix of key in the prefix= configuration file).

2. Use annotated Value ("${key}") to map

You can map the values in the configuration file to a field of Bean managed by Spring through the @ Value annotation.

3. Use the annotation @ PropertySource ("classpath:xxx.properties")

Load comments for custom properties files

It is specially used to load the properties file at the specified location, which needs to be used in conjunction with @ ConfigurationProperties (prefix = ""). Spring does not provide an annotation for loading the yml file at the specified location.

4. Use Environment

This environment configuration class can read not only configuration files, but also system variables, such as jdk version, current operating system type, user directory, and so on.

5. Use @ ImportResource annotations

Use to load external configuration files into the program, for example, we define a beans.xml file with a bean configured in it, and the bean is not loaded into the Spring container by default. We need the @ ImportResource (locations = {"classpath:beans.xml"}) annotation to load this configuration file. SpringBoot recommends that you use configuration classes to add components to the container.

Thank you for reading this article carefully. I hope the article "how to replace configuration files in SpringBoot" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Configuration files comments environment different articles tests production location content can be passed containers systems issues development operating systems generally speaking value interests prefixes Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Apple macOS MariaDB Microsoft