spring boot configuration file yml (yaml) properties

YML / yaml: server:

        port:  8080

properties::

server.port=8080

Profiles can modify the default configuration spring boot project

yml attributes and values ​​are case sensitive

--- wording values

 1 literal: Common values ​​(numbers, strings, Boolean)

    k: v: literally write directly to:

  String default without adding a single or double quotes

 

 

 

 

 

 

 

Writing the value of the array:

 

 

 

Use annotations @ConfigurationProperties tell SpringBoot all the attributes and configuration files in this class for the corresponding configuration bind

 

 

 

 

 

 

 

Solution: Import Dependence:

 

 

 

This class (assembly) was added to the vessel using @Component 

@ConfigurationProperties function is provided by the container, it must be added @Component

 

 

 

 

 

 

 

 

 @ConfigurationProperties is to obtain the value of the configuration file, prefix is ​​acquired prefix; @Component class is added to the vessel

 

 

 

Note: yml equivalent to the last-name lastName  

In configuration properties:

 

person.last-name = Joe Smith

person.age=18

person.birth=2017/12/15

person.boss=false

#idea properties using the default encoding utf-8's 
settings --- Encoding will have the following properties when converted to ascii file to run check on the

difference @ConfigurationProperties and @Value the

previous time needs the bean configuration file, to which component (class) to create a bean
<bean class = "the Person">
  <property name = "lastName" value = "?"> </ property>
</ bean> the Person is (name of the class) components
where name = "lastName" is an attribute value = "" is the value of the property

bean @ConfigurationProperties label with the same effect of this component is to Person (s) added to the vessel
wording wherein the support for value: literal (string, number, Boolean type value, etc.) ;

value = "literal / $ {key} from environment variables, configuration file to get the value / # {SpEL is an expression of spring boot}"

@Value with previous bean configuration files is the same role, the same rules follows

@Value ( "$ {} / # {1 * 2} / literal")

@ConfigurationProperties @Value bean and effect are the same profile


The difference @ConfigurationProperties @Value: 
Syntax loosely bound: (last-name lastName) @Value does not support the

data verification: @Validated showing the configuration file attribute class to be verified ;; Configuration support check, @ Value do not support verification

@Email (indicates that the property must be lastEmail mailbox format)
Private String lastEmail

 

 

 

 

 Profile yml or can get the value of properties

Complex type package: to a subject in need @ConfigurationProperties, map other types, @ Value does not support

If the batch acquisition, then use @ConfigurationProperties


Data check code:

 

 

@ConfigurationProperties obtain default values ​​from the global configuration file,

 The case of multiple configuration files; we need to use @PropertySource () to load the specified configuration file path

 @PropertySource: front class (value { "classpath person.propeities"}) needs to read the property value in the configuration file, the configuration file specified

 @ImportResource: imported spring boot configuration file.

We have written the configuration file (you can write beans.xml and other bean configuration files) to take effect, you need to use @ImportResource

Introducing @ImportResource on startup class (location = { "classpath: beans.xml"}) beans.xml refers to a configuration file to be loaded

SpringBoot recommended adding components to the container is: the recommended way (written before the tag bean bean configuration files) using the full annotation:

Write a configuration file folder, a special place profiles

MyAppConfig.java

 

 Test file:

 

 

 

 

 











 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/wsnan/p/11868305.html