Spring Boot allows you to use the code of the same application in different environments through external configuration, simply by using a configuration file to inject properties or to modify the default configuration.
Spring Boot Series
Spring Boot Starter
Spring Boot Property Configuration and use
Spring Boot Integrated MyBatis
Spring Boot static resource processing
Spring Boot-Configure sort Dependency Tips
Spring Boot-devtools Introduction
Spring Boot supports multiple external configuration methods
These methods are prioritized as follows:
- Command-line arguments
java:comp/env
the Jndi property from
- Java System Properties (
System.getProperties()
)
- Operating system Environment variables
RandomValuePropertySource
The configured random.*
property value
jar
Package external application-{profile}.properties
or application.yml
(with spring.profile
) configuration file
jar
Package internal application-{profile}.properties
or application.yml
(with spring.profile
) configuration file
jar
Package external application.properties
or application.yml
(without spring.profile
) configuration file
jar
Package internal application.properties
or application.yml
(without spring.profile
) configuration file
@Configuration
On the annotation class.@PropertySource
- By
SpringApplication.setDefaultProperties
specifying the default property
command-line arguments
Java -jar app.jar --name="Spring" --server.port=9090
pass parameters by way.
Parameters --xxx=xxx
are passed in the form of a.
The parameters that can be used can either be defined by us, or they can be the default parameters in spring boot.
A lot of people might be concerned about how the Web port is configured, these are the parameters provided in spring boot, and some of the available parameters are as follows:
# logginglogging. path=/var/logslogging. File=myapp. loglogging. config=# location of the config file (default classpath:logback.xml for logback) logging. level.*=# levels for loggers, e.g. "Logging.level.org.springframework=debug" (TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF)# EMBEDDED server CONFIGURATION (serverproperties) server .port=8080server# bind to a specific Nicserver# session timeout in Secondsserver . context-parameters.*= # Servlet context init parameters, e.g. Server.context-parameters.a=alphaserver.context-path= # the context path, defaults to '/' Server# the servlet path, defaults to '/' /span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
For more common application properties, please visit here
Note: command-line arguments are app.jar
behind!
You can SpringApplication.setAddCommandLineProperties(false)
configure by disabling the command line.
Java System Properties
Note the Java System attribute location java -Dname="isea533" -jar app.jar
, the properties that can be configured are the same, and the precedence is different.
For example java -Dname="isea533" -jar app.jar --name="Spring!"
, name
a value ofSpring!
operating system Environment variables
You should know this one if you have configured the Java_home.
It is important to note here that some OS can not support the use of such .
names, such as server.port
This can be used SERVER_PORT
to configure.
How the exact name matches, see later in this article.
Randomvaluepropertysource
Where a random number is used in the system, for example:
my.secret=${random.value}my.int}my.bignumber=${random.long}my.number.less .than.ten=${random .int (10)}my.number< Span class= "Hljs-preprocessor" >.in.range=${random .int[1024,65536]}
random.int*
value
parameters and parameters are supported ,max
, which max
is the minimum value when the parameter is supplied value
.
app configuration file (. properties or. yml)
Write directly in the configuration file:
name=Isea533server.port=8080
.yml
The format of the configuration file is as follows:
name: Isea533server: port: 8080
Using a format configuration file is simpler when there is a prefix .yml
. For .yml
configuration file usage See here
Note: .yml
when used, the value of the property name and the middle of the colon must have a space, such as name: Isea533
correct, name:Isea533
is wrong.
location of the property configuration file
Spring will look either from the /config
directory under Classpath or from the root directory of the classpath application.properties
application.yml
.
/config
Prior toclasspath根目录
@PropertySource
This annotation can specify a specific property profile with a lower priority.
springapplication.setdefaultproperties
For example:
new SpringApplication(Application.class);Map<String, Object> defaultMap = new HashMap<String, Object>();defaultMap.put("name", "Isea-Blog");//还可以是Properties对象application.setDefaultProperties(defaultMap);application.run(args);
apply (use) property
@Value ("${xxx}")
This is the simplest way to @Value
inject property values through annotations.
@ConfigurationProperties
Spring Boot makes it easy to inject attributes into a configuration object. For example:
my.name=Isea533my.port=8080my.servers[0]=dev.bar.commy.servers[1]=foo.bar.com
corresponding objects:
@ConfigurationProperties (prefix= "my") public class config {private String name; private Integer port; private list<string> servers = new ArrayList<String > (); public String gename () {return Span class= "Hljs-keyword" >this.name; } public Integer geport () {return Span class= "Hljs-keyword" >this.port; } public list<string> getservers () {return this.servers;}}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
Spring Boot automatically prefix="my"
my
injects the attributes prefixed to it.
Spring Boot automatically converts the type, and when used, List
you need to be careful to initialize it in the configuration List
!
Spring Boot also supports nested attribute injection, such as:
name=isea533jdbc.username=rootjdbc.password=root...
The corresponding configuration class:
@ Configurationpropertiespublic Class config {private String name; private jdbc jdbc, class Jdbc {private String username; private String password; //getter ...} public Integer geport () {return Span class= "Hljs-keyword" >this.port; } public Jdbc getjdbc () {return Span class= "Hljs-keyword" >THIS.JDBC; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
jdbc
The properties that begin are injected into the Jdbc
object.
using @configurationproperties on the @bean method
For example:
@ConfigurationProperties(prefix = "foo")@Beanpublic FooComponent fooComponent() { ...}
Spring Boot injects the foo
properties that begin with the name match into the FooComponent
object.
Property Placeholder
For example:
app.name=MyAppapp.description=${app.name} is a Spring Boot application
The previously configured properties can be referenced in the configuration file (the priority is configured here to work).
${app.name:默认名称}
You can also set a default value by using the method, and when the referenced property is not found, the default property is used.
Because the ${}
way will be handled by MAVEN. If your Pom inherits spring-boot-starter-parent
, Spring Boot has changed the maven-resources-plugins
default ${}
way @ @
, for example @[email protected]
.
If you are introducing spring Boot, you can modify the delimiter using the other
You can also shorten command parameters by using property placeholders
For example, modifying the Web default port needs to be used --server.port=9090
if it is written in the configuration:
server.port=${port:8080}
Then you can use a shorter --port=9090
, default value when the parameter is not provided 8080
.
property name matching rules
For example, you have the following configuration objects:
@Component@ConfigurationProperties(prefix="person")public class ConnectionSettings { private String firstName;}
firstName
You can use the following property names:
person.firstName
, the standard hump-type naming
person.first-name
, dashed ( -
) split mode, recommended for .properties
use in and .yml
configuration files
PERSON_FIRST_NAME
, uppercase underline, recommended for use in system environment variables
Property Validation
Annotations can be used for JSR-303
validation, such as:
@Component@ConfigurationProperties(prefix="connection")public class ConnectionSettings { @NotNull private InetAddress remoteAddress; // ... getters and setters}
finally
These are the contents of the Spring Boot property configuration and usage, some of which are not comprehensive or if the reader has more questions, you can view the spring boot full document or the externalized configuration.
Spring Boot Property Configuration and use