Deep understanding of spring boot property configuration files

preface

I believe many people choose spring boot mainly because it can not only take into account the powerful functions of spring, but also realize the convenience of rapid development. In the process of using spring boot, the most intuitive feeling is that we don't have many XML configuration contents when integrating spring applications. Instead, we use POM The modularized starter POMS is introduced into XML, in which each module has its own default configuration. Therefore, if it is not a special application scenario, it only needs to be in application The application of each module can be started by configuring some properties in properties.

Application. Has been mentioned in previous articles Properties is mainly used to configure database connection, log related configuration, etc. In addition to these configuration contents, this article will introduce some details in application Other features and usage in the properties configuration.

Custom properties and loading

When using spring boot, we usually need to define some properties for our own use. We can directly define them in the following ways:

Then load the corresponding configuration properties through the @ value ("${property name}") annotation, as follows:

By convention, unit tests are used to verify whether the properties in blogproperties have been loaded according to the configuration file.

References between parameters

In application The parameters in properties can also be directly referenced, just like the following settings:

com. didispace. blog. The desc parameter refers to the name and title attributes defined above. Finally, the value of this attribute is that the program DD is trying to write the spring boot tutorial.

Use random numbers

In some cases, we need to hope that some parameters are not a fixed value, such as key, service port, etc. In the property configuration file of spring boot, ${random} can be used to generate int value, long value or string string to support random values of properties.

Setting property values from the command line

I believe that users who have used spring boot for some time must know this command: Java - jar XXX jar --server. Port = 8888, by using cserver Port property to set XXX The port of jar application is 8888.

When running on the command line, two consecutive minus signs -- that is, for application The identification of the property value in properties. So, Java - jar XXX jar --server. The port = 8888 command is equivalent to our command in application Add the property server. In properties Port = 8888, which is visible in the sample project. Readers can verify by deleting the value or setting the value using the command line.

It is convenient to modify the attribute value through the command line, but it is not very safe to change the parameters of the application running through the command line? Yes, so spring boot also provides the setting of shielding the command line access property. You only need this setting to shield: springapplication setAddCommandLineProperties(false) 。

Multi environment configuration

When we develop spring boot applications, the same program is usually applied and installed in several different environments, such as development, testing, production, etc. The database address, server port and other configurations of each environment will be different. If you have to modify the configuration file frequently when packaging for different environments, it will be a very cumbersome and error prone thing.

For the configuration of multiple environments, the basic ideas of various project construction tools or frameworks are the same. By configuring multiple configuration files of different environments, and then specifying the content to be packaged through the packaging command, the packaging is differentiated. Spring boot is no exception, or simpler.

In spring boot, the multi environment configuration file name needs to meet the requirements of application - {profile} Properties, where {profile} corresponds to your environment ID, such as:

As for which specific configuration file will be loaded, you need to click application Properties file through spring profiles. The active property, whose value corresponds to the {profile} value.

For example: spring profiles. Active = test will load application test Properties configuration file content

Next, take different service ports configured in different environments as an example to conduct sample experiments.

Execute Java - jar XXX Jar, you can observe that the service port is set to 1111, which is the default development environment (DEV)

Execute Java - jar XXX jar --spring. profiles. Active = test, you can observe that the service port is set to 2222, that is, the configuration of the test environment (test)

Execute Java - jar XXX jar --spring. profiles. Active = prod, you can observe that the service port is set to 3333, that is, the configuration of the production environment (prod)

According to the above experiments, the configuration idea of multi environment can be summarized as follows:

Example code: chapter2-1-1

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>