Two Spring Boot Configuration

Source: Internet
Author: User

1. Configuration files

Spring Boot uses a global configuration file, and the frontal cortex file name is fixed;

    • Application.yml

The role of the configuration file: Modify the default value of the Springboot auto-configuration; Springboot at the bottom of the automatic configuration for us;

Yaml (Yaml Ain ' t Markup Language)

YAML a Markup Language: is a markup language

YAML isn ' t Markup Language: not a markup language;

Markup Language:

The previous configuration file; Most of the xxx.xml files are used;

YAML:YML is a file of Yaml (Yaml Ain ' t Markup Language) language, data-centric, more suitable for configuration files than JSON, XML, syntax specification

YAML: Sample Configuration

1 Server: 2   port:8081


1 < Server > 2     < Port >8081</port>3</Server >

The configuration file is placed under the Src/main/resources directory or classpath/config

Some default configuration values can be modified by the global configuration file

2. YAML syntax

1. Basic syntax

K: (Space) V: Indicates a pair of key-value pairs (spaces must have);

Controls the hierarchical relationship with the indentation of a space , as long as the left-aligned column of data is the same level of

Server:    port:8081    Path:/hello

Attributes and values are also case sensitive;

2. The notation of the value

Literal: Normal value (number, String, Boolean)

K: (space) V: literally directly to write;

String default without single or double quotation marks

"": double quotation marks; A special character string that is not escaped; special characters are meant to be represented by themselves.

Name: "Zhangsan \ n Lisi"; output: Zhangsan line Wrap Lisi

": single quotes; escapes special characters, and special characters end up as just a normal string data

Name: "Zhangsan \ Lisi"; output: Zhangsan \ n Lisi

Objects (attributes and values) (key-value pairs);

K:V: The next line to write the relationship of the object's properties and values; notice indentation

Object or the K:v way


1 Friends: 2 Lastname:zhangsan 3 age:20

In-line notation:

1 friends: {lastname:zhangsan,age:18}

Array (List,set)

An element in an array of bid-value bids

1 Pets: 2 -Cat 3 -Dog 4  -Pig

In-line notation

Pets: [Cat,pig,dog]

3. configuration file Value Injection

Configuration file

1 Person : 2   Lastname:zhangsan  3   age:18 4   false  5   birth:2017/ 12/12 6   maps: {k1:v1,k2:12}  7  lists:  8    - Lisi 9    - Zhaoliudog            : one name: Puppy Age:2


1 /**2 * Map the value of each property configured in the configuration file to this component3  *4 * @ConfigurationProperties: Tell Springboot to bind all the properties in this class and the configuration in the configuration file;5 * prefix = "person": Which of the following properties in the configuration file is one by one mapped6  *7 * Only this component is a component in the container to provide the @configurationproperties functionality8  */9 @ComponentTen@ConfigurationProperties (prefix = "person") One  Public classPerson { A  -     PrivateString LastName; -     PrivateInteger age; the     PrivateBoolean boss; -     PrivateDate Birth; -  -     PrivateMap<string, object>maps; +     PrivateList<object>lists; -     PrivateDog Dog;

We can import the configuration file processor, and we'll be prompted to write the configuration later.

1 <!--Import Profile processor, configuration file binding will be prompt-2 <dependency>3     <groupId> Org.springframework.boot</groupid>4     <artifactid>spring-boot-configuration-processor </artifactId>5     <optional>true</optional>6 </dependency>            

Properties Config file default utf-8 may be garbled in idea

@Value get values and @configurationproperties comparisons

@ConfigurationProperties @Value
Function Bulk injection properties in a configuration file Specify
Loosely bound (loose syntax) Support Not supported
Spel Not supported Support
JSR303 Data validation Support Not supported
Complex Type Encapsulation Support Not supported

Configuration file Yml or properties they can get the value;

If we just need to get a value from a configuration file in a business logic, use @value;

If we specifically write a javaben to map the configuration file, we use the @configurationproperties directly;

@PropertySource & @ImportResource

@PropertySource: Load the specified configuration file

1 /**2 * Map the value of each property configured in the configuration file to this component3  *4 * @ConfigurationProperties: Tell Springboot to bind all the properties in this class and the configuration in the configuration file;5 * prefix = "person": Which of the following properties in the configuration file is one by one mapped6  *7 * Only this component is a component in the container to provide the @configurationproperties functionality8 * @ConfigurationProperties (prefix = "person") default value from global configuration file9  */Ten@PropertySource (value = {"")}) One @Component A@ConfigurationProperties (prefix = "person") - //@Validated -  Public classPerson { the  -     /** - * <bean class= "person" > - * <property name= "lastName" value= "literal/${key} Get values from environment variables, configuration files/#{spel}" ></property> + * </bean> -      */ +  A     //LastName must be a mailbox format at     //@Email -     //@Value ("${person.last-name}") -     PrivateString LastName; -     //@Value ("#{11*2}") -     PrivateInteger age; -     //@Value ("true") in     PrivateBoolean boss; -     PrivateDate Birth; to  +     PrivateMap<string, object>maps; -     PrivateList<object>lists; the     PrivateDog Dog;

@ImportResource: Import the Spring configuration file and let the contents of the configuration file take effect;

Spring boot does not have a spring configuration file, and the configuration files we write ourselves are not automatically recognized;

Do not write the spring configuration file

1 <?xml version= "1.0" encoding= "UTF-8"?>2 <beans xmlns= " Schema/beans "3        xmlns:xsi=" Http:// "4        xsi: schemalocation= "Http:// Spring-beans.xsd ">56     class=" Com.young.springboot.service.HelloService "> </bean>78 </beans>

Want spring's configuration file to take effect, load it in, @ImportResource label on a configuration class

1 @ImportResource (locations = {"Classpath:beans.xml"})2 Import the spring configuration file for it to take effect

Springboot recommended ways to add components to a container; full annotation is recommended

1. Configuring the class =====spring configuration file

2. Adding components to the container using @bean

1 /**2 * @Configuration: Indicates that the current class is a configuration class, that is, to replace the previous spring configuration file3  *4 * Add components in the configuration file <bean></bean> tags5  */6 @Configuration7  Public classMyappconfig {8 9     //Add the return value of the method to the container; The default ID of this component in the container is the method nameTen @Bean One      PublicHelloService HelloService () { ASYSTEM.OUT.PRINTLN ("Configuration class @bean adding components to the container ..."); -         return NewHelloService (); -     } the  -}

Configuration file Placeholder

1. Random number

1 ${random.value}, ${random.  int}, ${random.  Long}2 ${random.  int(10)}, ${random.  int[1024,65536]}

2. Placeholder gets the previously configured value, if none can be used: Specify a default value

1 person.last-name= Zhang San ${random.uuid}2 person.age=${random.  int}3 person.birth=2017/12/154 person.boss=false5 person.maps.k1=v16 person.maps.k2=v27 person.lists=a,b,c 8${person.hello:hello}_dog9


1. Multiple profiles

When we write the main configuration file, the file name can be application-{profile}.properties/yml

Default use of configuration;

2.YML supports multi-document block mode

1 Server:2port:80813 Spring:4 Profiles:5 Active:prod6---7 Server:8port:80839 Spring:Ten Profiles:dev One--- A Server: -port:8084 - Spring: theProfiles:prod #指定属于哪个环境

3. Activate the specified profile

A. Specifying in the configuration file

B. Command line:

Java-jar; can be directly at the time of testing; Configure incoming command-line arguments

C. Virtual machine parameters:

Configuration file Load Location

Springboot boot scans the application.proprties or application.yml file at the following location as the default profile for spring boot

-File:. /config/




Priority is high-to-low, high-priority configuration overrides low-priority configurations

Springboot will load the master configuration file from four locations, and the complementary configuration;

We can also change the default configuration file location via Spring.config.location

After the project is packaged, we can use the form of command-line arguments to specify a new location for the configuration file when the project is started, specifying the configuration file and the default load of those profiles together to create a complementary configuration

Two Spring Boot Configuration

Related Article

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: 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.