First, the configuration file
SpringBoot use a global configuration file, the configuration file name is fixed;
- • application.properties
- • application.yml
Role configuration file: Modify the default value SpringBoot auto-configuration; (because he gave us in the bottom of the default SpringBoot automatically configured;)
YAML (YAML Is not Markup Language) YAML A Markup Language: is a markup language
YAML is not Markup Language: is not a markup language; markup languages:
Previous profile; mostly used xxxx.xml file;
YAML: data-centric than JSON, XML and other more suitable profile;
YAML: Configuration example
server:
port: 8081
XML: Configuration example
<server> <port>8081</port> </server>
Two, YAML syntax
2.1 , basic grammar
k :( space) v: designates a pair of key-value pairs (there must be a space);
If you need a space indentation hierarchy to control the hierarchy; as long as one is left-aligned data, are the same level
server:
port: 8081
path: /hello
And attribute values are case sensitive;
2.2 , the value of writing
Literal: Common values (numbers, strings, Boolean, date)
k: v: literally write directly;
No default string in single or double quotes;
Double quotes ; there will not escape the string of special characters; special characters as themselves wish of intention
name: "zhangsan \ n lisi": Output; zhangsan newline lisi ''
Single quotes ; will escape special characters, special characters finally just an ordinary string data
name: 'zhangsan \ n lisi': output; zhangsan \ n lisi
Object, Map (attributes and values) (key-value pairs):
k: v: the next line to write the relationship between attributes and values of an object; note indentation object or k: v way
friends:
lastName: zhangsan
age: 20
Inline wording:
friends: {lastName: zhangsan,age: 18}
Array (List, Set):
Represents an element in the array values - with
pets:
‐ cat
‐ dog
‐ pig
Inline wording
pets: [cat,dog,pig]
Third, the value of the injected profile
Profile yml
person:
lastName: hello
age: 18
boss: false
birth: 2017/12/12
maps: {k1: v1,k2: 12}
lists:
- lysis
- zhaoliu
dog:
name: Puppies
age: 12
javaBean:
/** * The value of the attribute of each configuration file is mapped to the component * @ ConfigurationProperties: Tell SpringBoot all the attributes and configuration files in this class for the corresponding configuration bind * Prefix = "person": profile in which all of the following attributes one mapping * This component is only a container of components, @ConfigurationProperties to function provided by 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, after writing configuration have prompted the
<! - introducing profile processor, binding profile will be prompted - > < dependency > < the groupId > org.springframework.boot </ the groupId > < the artifactId > Spring-Configuration-Processor Boot- </ the artifactId > < optional > to true </ optional > </ dependency >
3.1, properties utf-8 default configuration file may be garbled in the idea of
Adjustment
3.2, @ Value acquisition value and acquisition value comparison @Con fi gurationProperties
|
@ConfigurationProperties |
@Value |
Features |
Batch injection configuration file attributes |
A designated |
Loosely bound (loose syntax) |
stand by |
not support |
Game(#{}) |
not support |
stand by |
JSR303 data check |
stand by |
not support |
Complex type package (such as Map) |
stand by |
not support |
Profile yml or properties they can get to value;
If you say that we just need to get some business logic at a particular value in the configuration file, use @Value;
If we say that we wrote a javaBean dedicated to mapping and configuration files, we just use fi gurationProperties @Con; √
Injection profile data check value ( JSR303 Data Check )
@Component @ConfigurationProperties(prefix = "person") @Validated public class Person { // lastName must be a mailbox format @Email //@Value("${person.last‐name}") private String lastName; //@Value("#{11*2}") private Integer age; //@Value("true") private Boolean boss; /** * <bean class="Person"> * <Property name = "lastName" value = "literal / $ {key}, the value profile acquired from the environment variable / # {SpEL}"> </ property> * <bean/> */ private Date birth; private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
3.3、@PropertySource&@ImportResource&@Bean