Spring Boot property Configuration and use

Source: Internet
Author: User
Tags dname

Click on the top "Java leader" and select "Pin public number"

Dry article first time delivery.


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 supports multiple external configuration methods

These methods are prioritized as follows:

Command-line arguments

Jndi Properties from Java:comp/env

Java System Properties (System.getproperties ())

Operating system Environment variables

random.* property value for Randomvaluepropertysource configuration

Application-{profile}.properties or Application.yml (with Spring.profile) profile external to the jar package

Application-{profile}.properties or Application.yml (with spring.profile) configuration file inside the jar package

Application.properties or Application.yml (without spring.profile) configuration file outside the jar package

Application.properties or Application.yml (without spring.profile) configuration file inside the jar package

@propertysource on the @Configuration annotation class

Default Property command-line arguments specified by springapplication.setdefaultproperties

The parameters are passed through the Java-jar app.jar--name= "Spring"--server.port=9090 way.

The parameters are passed in the form of--xxx=xxx.

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:

# LOGGING
Logging.path=/var/logs
Logging.file=myapp.log
logging.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, FA TAL, OFF)

# EMBEDDED SERVER CONFIGURATION (serverproperties)
server.port=8080
Server.address= # bind to a specific NIC
Server.session-timeout= # Session Timeout in seconds
server.context-parameters.*= # Servlet Context init parameters, e.g. Server.context-parameters.a=alpha
Server.context-path= # The context path, defaults to '/'
Server.servlet-path= # The servlet path, defaults to '/'


for more common application properties, please visit here (http://docs.spring.io/spring-boot/docs/1.2.3.RELEASE/reference/html/ common-application-properties.html)

Note: command-line arguments are behind App.jar.

Command-line configuration can be disabled by springapplication.setaddcommandlineproperties (false). Java System Properties

Note the Java System attribute location java-dname= "isea533"-jar App.jar, the properties that can be configured are the same, with different precedence.

For example java-dname= "isea533"-jar app.jar--name= "spring!" The name value is spring! operating system Environment variables

You should know this one if you have configured the Java_home.

Here are some things to note that some OS can not be used. This name, such as Server.port, can be configured using Server_port.

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.number=${random.int}
My.bignumber=${random.long}
My.number.less.than.ten=${random.int (10)}
MY.NUMBER.IN.RANGE=${RANDOM.INT[1024,65536]}


The random.int* supports the value parameter and the Max parameter, which is the minimum value when the max parameter is supplied. app configuration file (. properties or. yml)

Write directly in the configuration file:

name=isea533
server.port=8080


configuration files in. yml format such as:

name:isea533
Server
port:8080


When prefixed, a configuration file using the. yml format is simpler. For the. YML configuration file usage See here (http://docs.spring.io/spring-boot/docs/1.2.3.RELEASE/reference/html/ BOOT-FEATURES-EXTERNAL-CONFIG.HTML#BOOT-FEATURES-EXTERNAL-CONFIG-YAML)

Note: when using. YML, there must be a space between the value of the property name and the colon, such as name:isea533 correct, name:isea533 is wrong. location of the property configuration file

Spring will look for application.properties or application.yml from the/config directory under Classpath or from the root of classpath.

The/config takes precedence over the classpath root directory @PropertySource

This annotation can specify a specific property profile with a lower priority. springapplication.setdefaultproperties

For example:

Springapplication application = new Springapplication (application.class);
map<string, object> defaultmap = new hashmap<string, object> ();
Defaultmap.put ("name", "Isea-blog");
It can also be a Properties object
Application.setdefaultproperties (DEFAULTMAP);
Application.Run (args);


apply (use) property @Value ("${xxx}")

This is the simplest approach, with @value annotations that allow you to inject property values in. @ConfigurationProperties

Spring Boot makes it easy to inject attributes into a configuration object. For example:

my.name=isea533
my.port=8080
My.servers[0]=dev.bar.com
My.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 this.name;
}

Public Integer Geport () {
return this.port;
}
Public list<string> getservers () {
return this.servers;
}
}


Spring Boot automatically prefixes the prefix= "my" to

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.