Spring Boot Property Configuration and use

Source: Internet
Author: User
Tags dname

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
    1. Spring Boot Starter

    2. Spring Boot Property Configuration and use

    3. Spring Boot Integrated MyBatis

    4. Spring Boot static resource processing

    5. Spring Boot-Configure sort Dependency Tips

    6. Spring Boot-devtools Introduction

Spring Boot supports multiple external configuration methods

These methods are prioritized as follows:

    1. Command-line arguments
    2. java:comp/envthe Jndi property from
    3. Java System Properties ( System.getProperties() )
    4. Operating system Environment variables
    5. RandomValuePropertySourceThe configured random.* property value
    6. jarPackage external application-{profile}.properties or application.yml (with spring.profile ) configuration file
    7. jarPackage internal application-{profile}.properties or application.yml (with spring.profile ) configuration file
    8. jarPackage external application.properties or application.yml (without spring.profile ) configuration file
    9. jarPackage internal application.properties or application.yml (without spring.profile ) configuration file
    10. @ConfigurationOn the annotation class.@PropertySource
    11. By SpringApplication.setDefaultProperties specifying the default property
command-line arguments

Java -jar app.jar --name="Spring" --server.port=9090pass 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]}   
    • 1
    • 2
    • 3
    • 4
    • 5
    • 1
    • 2
    • 3
    • 4
    • 5

random.int*valueparameters 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
    • 1
    • 2
    • 1
    • 2

.ymlThe format of the configuration file is as follows:

name: Isea533server:    port: 8080
    • 1
    • 2
    • 3
    • 1
    • 2
    • 3

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 .

/configPrior 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);
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
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
    • 1
    • 2
    • 3
    • 4
    • 1
    • 2
    • 3
    • 4

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...
    • 1
    • 2
    • 3
    • 4
    • 1
    • 2
    • 3
    • 4

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

jdbcThe properties that begin are injected into the Jdbc object.

using @configurationproperties on the @bean method

For example:

@ConfigurationProperties(prefix = "foo")@Beanpublic FooComponent fooComponent() { ...}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 1
    • 2
    • 3
    • 4
    • 5

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
    • 1
    • 2
    • 1
    • 2

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}
    • 1
    • 1

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;}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

firstNameYou can use the following property names:

    1. person.firstName, the standard hump-type naming
    2. person.first-name, dashed ( - ) split mode, recommended for .properties use in and .yml configuration files
    3. 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}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
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

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.