configuration file
Spring Boot uses a global configuration file whose name is fixed
- application.properties
- application.yml
Function of configuration file: modify the default value of Spring Boot auto configuration, that is, modify the value that Spring Boot has configured for us at the bottom
YAML(YAML Ain't Markup Language)
Markup Language:
Previous configuration files; most used xxx.xml Documents;
YAML: data centric, more suitable for configuration files than json and xml
YAML configuration example (space must be added after colon)
server: port: 8081
XML: Configuration Example
<server> <port>8081</port> </server>
1.YAML syntax
1. Basic grammar
The relationship between attributes and values: k: (space) v: indicates a pair of key value pairs (space cannot be omitted);
Control hierarchy by indenting spaces: all left aligned data are at the same level
server: port: 8081 path: /hello
Attributes and values are also case sensitive;
2. Description of value
Literal: normal value (number, string, Boolean)
k: Write directly in the V word;
In general, strings do not need to be enclosed with single quotation marks or double quotation marks;
"Double quotation mark": it will escape the special characters in the string, and the special characters will be taken as the meaning it wants to identify
name: "zhangsan /n list" Output: zhangsan linefeed list
'single quotation mark': special characters will not be escaped. The special characters are only common string data
name: 'zhangsan /n list' //Output: zhangsan /n list
Object, Map (property and value) (key value pair):
k: v writes the relationship between the property and value of the object on the next line; pay attention to using space indentation
friends: lastName: zhangsan age: 20
In line:
friends: {lastName: zhangsan,age: 18}
Array (List, Set):
Represents an element in an array with a - (space) value
pets: - cat - dog - pig
In line writing
pets: {cat,dog,pig}
3. Profile value injection
1.@ConfigurationProperties (the default value is taken from the global configuration file)
configuration file
server: port: 8081 person: lastName: zhangsan age: 19 boss: false birth: 2020/5/20 maps: {k1: v1,k2: 12} lists: - lisi - zhaoliu dog: name: Wangcai age: 2
javaBean
/** * Map the value of each property configured in the configuration file to this component * @ConfigurationProperties:Tell springBoot to bind all the properties in this class to the configuration related to the configuration file; * prefix = "person":Which of the following attributes in the configuration file is mapped one by one * Only when this component is a component in the container can the functions provided by the container be used; **/ @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;
After importing the profile processor, there will be a prompt when the profile is bound (the prompt in the configuration meets the switching of hump naming last name = last name)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
2.@Value
/** * Spring The underlying Value, previously in the Spring configuration file * <bean> * <property name="lastName" value="Literal amount / ${key} - get value / {spel} "> < property / > * </bean> * Previously supported, supported in @ Value **/ @Value("${person.last-name}") private String lastName; @Value("#{11*2}") private Integer age; @Value("true") private Boolean boss;
3. Comparison between @ value and @ ConfigurationProperties
@ConfigutationProperties | @Value | |
---|---|---|
Batch injection | support | I won't support it |
Loose binding (loose syntax: lastName=last_name=last-name) | support | I won't support it |
SpEL | I won't support it | support |
JSR303 data verification | support | I won't support it |
Complex type encapsulation (object, map) | support | I won't support it |
The configuration file can get the value whether it is ymal or properties
Summary:
If you only get a Value in the configuration file in a business logic (Controller, Service), use @ Value
If we write a java bean to map with the configuration file, use @ ConfigurationProperties
4. Configuration file injection value data verification
@Component @ConfigurationProperties(prefix = "person") @Validated public class Person { @Email private String lastName;
5.@PropertySource&ImportResource
- @PropertySource: load the specified configuration file (if there is a matching prefix property in the global configuration file, the global file will be loaded first, and the file marked by PropertySource will not be found again)
@PropertySource("classpath:person.properties") @Component @ConfigurationProperties(prefix = "person")
- @Import resource: 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 we wrote 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 Spring's configuration file for it to take effect
-
Recommended ways to add components in Spring Boot
1. Do not write xml file, use full annotation method (configuration class = =) configuration file)
2. Use @ Bean annotation to add components to the container
/** * Indicates that the current class is a configuration class * Add components with bean s in the configuration file * Use @ Bean in configuration class **/ @Configuration public class MyAppConfig { //Add the return value of the method to the container: the default id of this component in the container is the method name @Bean public HelloService helloService(){ System.out.println("@Bean Add build to container"); return new HelloService(); } }
4. Placeholder for configuration file
1. Random number
${random.value},${random.int},${random.long} ${random.int(10)},${random.int[1024,65536]}
2. The placeholder gets the previously configured value. If it is not available, specify the default value
You can refer to the previously configured properties in the configuration file (all the previously configured properties of priority can be used here). If the properties cannot be found, use ${key: value} to specify the default value
person.last-name=Zhang San ${random.uuid} person.age=${random.int} person.birth=2017/12/15 person.boss=false person.dog.name=${person.hello: hello}_dog person.dog.age=2 person.maps.k1=v1 person.maps.k2=15 person.lists=1,2,b
5.Profile
1. Multiple Profile files
When we write the main configuration file, the file name can be application-{profile}.properties/yml
Default use application.properties ;
2.yml supports multi document block mode
Three - can be divided into blocks
profiles: specify which environment (custom), active: activate which environment
server: port: 8081 spring: profiles: active: prod --- server: port: 8082 spring: profiles: prod --- server: port: 8083 spring: profiles: dev ---
3. Activate the specified profile
1. In the default global configuration file spring.profiles.active=dev activation
2. Command line activation:
-
cmd in the jar package directory: Java jar xxx.jar -- spring.profies.active=dev
-
Program arguments of IDEA running configuration:-- spring.profies.active=dev
-
VM options for IDEA running configuration:- Dspring.profiles.active=dev