The configuration of the properties is often used in projects, and Spring Boot has a much simplified configuration of properties relative to the Spring framework configuration file.
One: Attribute configuration priority
1. Command-line arguments 2. Jndi attribute 3.Java from Java:comp/env System Properties (System.getproperties () 4. Operating system environment variable 5.RandomValuePropertySource configuration random.* property value 6.jar Package external application-{profile}.properties or APPLICATION.YML ( With spring.profile) configuration file 7.jar application-{profile}.properties or application.yml (with Spring.profile) inside the package Application.properties or Application.yml (without spring.profile) outside of the configuration file 8.jar package Config file 9.jar application.properties or application.yml (without spring.profile) profile in the package [email protected] The @propertysource11 on the annotation class. Default properties specified by Springapplication.setdefaultproperties
(1.1) Command line parameters
java -jar myApp.jar --name="Spring Boot" --server.port=8081
pass parameters by way. Command-line arguments are set after Myapp.jar.
(1.2) Java EE contextual environment variables
(1.3) Java System variables
java -Dname="ws" -jar myApp.jar
, set the Name property to WS
If you set Java-dname= "ws"-jar app.jar--name= "boot" because the command line parameters are high priority, the Name property is the boot
(1.4) Operating system environment variables
Operating system environment variables such as Java_home,maven_home that are configured when installing Java and MAVEN are environment variables for the operating system.
(1.5) Random values
# random string com.ws.blog.value=${random.value}# random intcom.ws.blog.number=${random.int}# random longcom.ws.blog.bignumber=${ Random number within random.long}# 10 Com.ws.blog.test1=${random.int (10)}# 10-20 random number com.ws.blog.test2=${random.int[10,20]}
(1.6) @PropertySource
This annotation allows you to specify a specific property configuration text
Two: application.properties file load Order
1. The/config subdirectory under the current path. 2. Current path. 3./config sub-path under Classpath path. 4. Classpath Path
Three: Configuring attribute classes
(3.1) configuration with @value ("${xxx}")
1 @Configuration2 Public classMyconfig {3@Value ("${ws.name}")4 PrivateString name;5@Value ("${ws.age}")6 PrivateInteger age;7 8 PublicString GetName () {9 returnname;Ten } One A Public voidsetName (String name) { - This. Name =name; - } the - PublicInteger getage () { - returnAge ; - } + - Public voidsetage (Integer age) { + This. Age =Age ; A } at}
(3.2) with @configurationproperties configuration
First, add the following dependencies to the POM
<Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-configuration-processor</Artifactid> <Optional>True</Optional></Dependency>
Add the following configuration to the Application.properties file
#My Config[email PROTECTED]WS.AGE=18WS.LISTADDRESS[0]=ADD1WS.LISTADDRESS[1]=ADD2
Configuring the Myconfig Class
Importorg.springframework.boot.context.properties.ConfigurationProperties;Importorg.springframework.context.annotation.Configuration;Importjava.util.List, @Configuration @configurationproperties (prefix= "WS") Public classMyconfig {PrivateString name; PrivateInteger age; PrivateList<string>listaddress; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicInteger getage () {returnAge ; } Public voidsetage (Integer age) { This. Age =Age ; } PublicList<string>getlistaddress () {returnlistaddress; } Public voidSetlistaddress (list<string>listaddress) { This. listaddress =listaddress; }}
Spring Boot Property Configuration