[Spring Boot] Spring Boot system configuration - system configuration file

system configuration file

The system configuration file of Spring Boot, including the use of application.properties and application.yml configuration files and the difference between YML and Properties configuration files, finally introduces how to change the startup pattern of Spring Boot.

1.application.properties

Spring Boot supports configuration files in two different formats: one is Properties and the other is YML. Spring Boot uses application.properties as the system configuration file by default. After the project is successfully created, the application.properties file will be generated in the resources directory by default. This file contains the global configuration for the Spring Boot project. We can configure all configuration items supported by Spring Boot in the application.properties file, such as port number, database connection, log, startup pattern, etc. Next, some knowledge related to configuration in the Spring Boot project development process will be introduced.

(1) Basic grammar

After the Spring Boot project is successfully created, the application.properties file will be automatically created in the default resources directory. It is also very simple to use, the configuration format is as follows:

# 服务器端口配置
server.port=8081

In the example above, the startup port of the application is configured. If you do not configure this item, port 8080 is used by default; if you need to use other ports, modify the system startup port by server.port=8081.

In addition, the configuration items in the Properties file can be out of order, but in order to ensure that the configuration file is clear and easy to read, it is recommended to put related configuration items together, for example:

insert image description here
The above example puts the configuration related to the thymeleaf template together, which looks clear and clear, making it easy to quickly find all configurations of thymeleaf.

(2) Configuration file loading order

The configuration files of the Spring Boot project are stored in the resources directory by default. In fact, when the Spring Boot system starts, it will read configuration files in four different paths:

1) The config directory under the project root directory.

2) The project root directory.

3) The config directory under the classpath.

4) classpath directory.

Spring Boot will load all the main configuration files from these four locations, and the priority of the application.properties files in these four locations will be lowered in the order listed above. If the same attribute appears in these 4 files, the file with higher priority shall prevail.

(3) Modify the default configuration file name

Some people may ask, must the configuration file of the project be named application.properties? Of course not, we can customize the configuration file name by modifying the project startup class and calling the properties() method of the SpringApplicationBuilder class. The sample code is as follows:

insert image description here

In the above example, when the Spring Boot project is started and loaded, the configuration file with the changed name is read by default, and the application.yml file name loaded by default can be modified.

2.application.yml

application.yml is a configuration file suffixed with yml and using YAML (YAML Ain't a Markup Language). Compared with markup languages ​​such as XML, the YMAL structure is clearer and easier to read, and is more suitable for use as a property configuration file.

(1) Basic grammar

The basic syntax of YML is the key-value pair form of key: (space) value, and a space must be added after the colon. The hierarchical relationship of attributes is controlled by the indentation of spaces. As long as a column of data is left-aligned, they are all at the same level. The specific format is as follows:

insert image description here

In the above example, attributes such as the log level and file path of the system are customized and configured. You can see that there are two sub-configuration items under logging, level and file.

Although the format of the YML file is simple and intuitive, it has high requirements on the format. When using the YML configuration file, you need to pay attention to the following points:

1) There must be a space between the attribute value and the colon. If name: Weiz is correct, an error will be reported if name: Weiz is used.
2) Pay attention to the indentation and alignment between attributes.
3) Tabs are not allowed for indentation, only spaces are allowed.
4) Attributes and values ​​are case-sensitive.

(2) Data type

YML files are data-centric and support multiple data formats such as arrays, JSON objects, and Maps, so they are more suitable for use as configuration files.

(1) Ordinary values ​​(numbers, strings, booleans)

Ordinary data is directly written in the form of k:v key-value pairs. Ordinary value types or strings do not need to add single quotes or double quotes by default.

Of course, you can also use double quotes ("") to escape the special characters in the string. After the special characters are escaped, they represent their own meaning, for example:

name: "zhangsan \n lisi"

The above example would output:

zhangsan
lisi

Using single quotes ('') does not escape special characters, all characters are treated as normal characters, as string data, for example:

name: 'zhangsan \n lisi'

The above example will output: zhangsan \n lisi. "\n" characters are treated as normal strings without being escaped as newlines.

(2) Object, Map (properties and values)

Objects are also presented in the form of k:v key-value pairs, but the relationship between each attribute and value of the object is written by line break and indentation. The sample code is as follows:

person:
	lastName: zhangsan
	age: 20

If you use the inline writing method, you can write the properties and values ​​of the object in JSON format. The specific writing method is as follows:

person: {
    
    lastName: zhangsan, age: 20}

(3) Arrays (List, Set)

Arrays represent the elements in the array in the form of - value. The specific writing method is as follows:

persons:
 -zhangsan
 -lisi
 -wangwu

You can also use the inline writing method, and the array uses the form of square brackets. The specific writing method is as follows:

persons: [zhangsan, lisi, wangwu]

We can see that in addition to supporting basic data types, YML files also support formats such as objects, maps, JSON, and arrays, so that the desired data types can be directly defined in the configuration file without additional conversion. This is one of the reasons why programmers like to use application.yml.

3. The difference between Properties and YML configuration files

The configuration file in Spring Boot has two formats, Properties or YML. In general, the two can be used at will, and we can choose the appropriate configuration file format according to our usage habits. Are the two exactly the same? Certainly not, the difference between YML and Properties configuration files is as follows:

1) YML files are data-centric, and are very friendly to data support and display.

2) The format requirements of the Properties file are not so strict, while the YML file uses indentation of spaces to control the hierarchical relationship. The requirements for the format are relatively high, and it is easy to make mistakes when the indentation format is incorrect.

3) Properties files support @PropertySource annotations, but YML files do not.

4) YML files support the use of multi-document blocks, which is very flexible to use.

5) Properties configuration has a higher priority than YML files. Because the YML file is loaded before the Properties file, if the two files have the same configuration, the configuration in the Properties loaded later will overwrite the configuration in the previous YML.

4. Actual combat: Customize the startup pattern of the system

We know that when the Spring Boot program starts, the console will output the startup pattern (Banner) and version information of the Spring symbol consisting of a string of characters (see the figure below).

insert image description here

Can the startup pattern that comes with Spring Boot be customized?

The answer is yes. The following example demonstrates how to customize Spring Boot's startup pattern.

Step 01 Create a new banner.txt under the resources directory of the project. The sample code is as follows:

insert image description here
In the above configuration, ${} is used to obtain relevant configuration information in the application.properties configuration file, such as Spring Boot version, application version, application name and other information.

${AnsiColor.BRIGHT_RED}:设置控制台中输出内容的颜色,可以自定义,具体参考org.springframework.boot.ansi.AnsiColor。
${application.version}:用来获取MANIFEST.MF文件中的版本号,这就是在Application.java中指定SpringVersion.class的原因。
${application.formatted-version}:格式化后的{
    
    application.version}版本信息。
${spring-boot.version}:Spring Boot的版本号。
${spring-boot.formatted-version}:格式化后的{
    
    spring-boot.version}版本信息。步骤02 在application.properties中配置banner.txt的路径等信息。

Step 02 Configure information such as the path of banner.txt in application.properties.

insert image description here

In the above configuration, set the path of the banner.txt file, the version of the application, the version of Spring Boot and other information in the application.

Step 03 Start the project and check whether the modified startup banner pattern takes effect, as shown in the figure.

insert image description here

From the startup log output by the system, we can see that the startup pattern of the system has become our custom one, that is, the default startup pattern of Spring Boot has been changed to a custom startup pattern.

Spring Boot also supports the use of image files in GIF, JPG, and PNG formats to define banner patterns. Of course, the picture is not directly output to the console, but the pixels in the picture are parsed and converted into ASCII encoded characters before being output to the console.

Guess you like

Origin blog.csdn.net/weixin_45627039/article/details/131686093