yml and properties configuration files, automatically prompt settings when writing the configuration of SpringBoot components, and Chinese garbled code

SpringBoot uses a global configuration file with a fixed name;

•application.properties

•application.yml

Function of configuration file: modify the default value of SpringBoot automatic configuration; SpringBoot is automatically configured at the bottom;

YAML(YAML Ain't Markup Language)

YAML A Markup Language: is a markup language

YAML isn't Markup Language: not a markup language;

Markup Language:

Previous configuration files; Most of them use xxxx.xml file;

YAML: data centric, more suitable for configuration files than json, xml, etc;

YAML: Configuration Example

server:
  port: 8081

XML:

<server>
    <port>8081</port>
</server>

2. YAML syntax:

1. Basic grammar

k: (space) v: indicates a pair of key value pairs (space must be);

Indent the space to control the hierarchical relationship; As long as a column of data is left aligned, it is at the same level

server:
   port: 8081
   path: /hello

Attributes and values are also case sensitive;

2. Value writing

Literal: ordinary value (number, string, Boolean)

k: v: write it literally;

The string does not need single or double quotation marks by default;

"": double quotation marks; Special characters in the string will not be escaped; Special characters will be used as the meaning they want to express

name: "zhangsan \n lisi": output; Line feed lisi

'': single quotation mark; Special characters will be escaped, and the special characters will eventually be just ordinary string data

name: 'Zhangsan \ n Lisi': output; zhangsan \n lisi

Object, Map (properties and values) (key value pairs):

k: v: write the relationship between the attribute and value of the object in the next line; Pay attention to indentation

Object or k: v

friends:
   lastName: zhangsan
   age: 20

Inline writing:

friends: {lastName: zhangsan,age: 18}

Array (List, Set):

An element in an array is represented by a - value

pets:
 - cat
 - dog
 - pig

Inline writing

pets: [cat,dog,pig]

3. Profile value injection

configuration file

person:
    lastName: hello
    age: 18
    boss: false
    birth: 2017/12/12
    maps: {k1: v1,k2: 12}
    lists:
      - lisi
      - zhaoliu
    dog:
       name: Puppy
      age: 12

javaBean:

/**
 * Map the value of each attribute configured in the configuration file to this component
 * @ConfigurationProperties: Tell SpringBoot to bind all the properties in this class to the relevant configuration in the configuration file;
 *      prefix = "person": Which of the following attributes in the configuration file is mapped one by one
 *
 * The @ ConfigurationProperties function provided by the container can only be used if this component is a component in the container;
 *
 */
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
​
    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;
​
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;
​}

We can import the configuration file processor, and we will be prompted to write the configuration later

<!-- When you import the configuration file processor, you will be prompted -- >
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

1. The default utf-8 of the properties configuration file in the idea may be garbled

Modify the configuration, Refer here

@Comparison between Value and @ ConfigurationProperties

@ConfigurationProperties@Value
functionProperties in batch injection profileSpecify one by one
Loose binding (loose syntax)supportI won't support it
SpELI won't support itsupport
JSR303 data verificationsupportI won't support it
Complex type encapsulationsupportI won't support it

They can get values from both configuration files yml and properties;

If we only need to obtain a Value in the configuration file in a business logic, use @ Value;

If we specifically write a javaBean to map with the configuration file, we will directly use @ ConfigurationProperties;

@PropertySource&@ImportResource&@Bean

@PropertySource: load the specified configuration file;

/**
 * Map the value of each attribute configured in the configuration file to this component
 * @ConfigurationProperties: Tell SpringBoot to bind all the properties in this class to the relevant configuration in the configuration file;
 *      prefix = "person": Which of the following attributes in the configuration file is mapped one by one
 *
 * The @ ConfigurationProperties function provided by the container can only be used if this component is a component in the container;
 *  @ConfigurationProperties(prefix = "person")Get the value from the global configuration file by default;
 *
 */
@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
//@Validated
public class Person {
​
    /**
     * <bean class="Person">
     *      <property name="lastName" value="Literal / ${key} get value from environment variable and configuration file / #{spiel} "> < / property >
     * <bean/>
     */
​
   //lastName must be in mailbox format
   // @Email
    //@Value("${person.last-name}")
    private String lastName;
    //@Value("#{11*2}")
    private Integer age;
    //@Value("true")
    private Boolean boss;
​}

@ImportResource: import the Spring configuration file to make the content in the configuration file effective;

There is no Spring configuration file in Spring Boot, and the configuration file written by ourselves cannot be recognized automatically;

To make the Spring configuration file effective, load it in; @ ImportResource is marked on a configuration class

@ImportResource(locations = {"classpath:beans.xml"})
Import the Spring configuration file to make it take effect

Activate the specified profile

1. Specify spring.profiles.active=dev in the configuration file

2. Command line:

java -jar ***.jar --spring.profiles.active=dev

You can configure the incoming command line parameters directly during the test

3. Virtual machine parameters;

-Dspring.profiles.active=dev

Tags: Spring MVC

Posted on Wed, 08 Sep 2021 20:15:05 -0400 by knighthawk1337