current position:Home>[springboot framework] - springboot configuration file, another golden nine silver ten year

[springboot framework] - springboot configuration file, another golden nine silver ten year

2022-01-26 21:34:03 m0_ fifty-four million eight hundred and sixty-one thousand fou


  • application.yml(application.yaml)

1. The configuration file

We can create Configuration file to overwrite SpringBoot Default configuration , Can be in resources Create under folder file , Here you can configure it manually Tomcat Port de coverage SpringBoot The default configuration is , We know Tomcat The default open port is 8080, We can go through Configuration file to modify the default configuration :


After adding the configuration , Run the project

properties The file is written in the form of key value pairs , You can see , By modifying the The configuration file , Have already put Tomcat Port to 8888, Overwrites the previous configuration , because  properties In the past, more documents were used , More common , I won't talk too much here , Mainly right yml Learn from the configuration file .

2. application.yml(application.yaml) The configuration file

YML The file format is YAML (YAML Aint Markup Language) The format of the document ,YAML It is an intuitive data serialization format that can be recognized by computer , And it's easy for humans to read , Easy to interact with scripting language , Can be supported YAML Library of different programming languages, program import , such as : C/C++, Ruby, Python, Java, Perl, C#, PHP etc. .YML Files are data centric , Than traditional xml The way is more concise .YML The file extension can use .yml perhaps .yaml.

【1】yml Profile syntax

yml The configuration of can be divided into the following , We first in resources Create under the folder  application.yml The configuration file

  • Configure common data

  • Configure object data

  • To configure Map data

  • Configure array data

Configure common data :

  • grammar : key: value

  • eg:

name: onestar

Be careful :value There is a space in front

Configure object data :

  • grammar 1:

  • key:

key1: value1

key2: value2

  • grammar 2:

  • key: {key1: value1,key2: value2}

  • eg:


name: oneStar

age: 18

addr: yichun

# perhaps

person: {name: oneStar,age: 18,addr: yihcun}

Be careful :key1 There is no limit to the number of spaces in front , stay yml In the syntax , The same indent represents the same level

To configure Map data :

  • To configure Map The data is the same as the configuration object data

  • eg:


key1: value1

key2: value2

Configure array data :

  • grammar 1

  • key:

  • value1

  • value2

  • grammar 2

  • key: [value1,value2]

  • eg:


  • beijing

  • tianjin

  • shanghai

  • chongqing

# perhaps

city: [beijing,tianjin,shanghai,chongqing]

# The elements in the collection are in the form of objects


  • name: oneStar

age: 18

score: 100

  • name: twoStar

age: 28

score: 88

  • name: threeStar

age: 38

score: 90

Be careful :value1 Between and - There is a space between

Two 、 Attribute mapping between configuration file and configuration class


Configuration in profile , Whether it's properties The file or yml file , There are some configurations SpringBoot Will automatically identify , such as server.port=8888 To configure , But there are some custom configurations ,SpringBoot It doesn't automatically recognize , How to make SpringBoot Know your own configuration information , There are two ways :

  • Using annotations @Value mapping

  • Using annotations @ConfigurationProperties mapping

1、 Using annotations @Value mapping

We can go through @Value Annotation maps the values in the configuration file to a Spring Managed Bean Field on , Here we use  yml The document explains

  • application.yml The configuration is as follows :


name: oneStar

age: 18

  • Entity Bean The code is as follows :


public class QuickController {


private String name;


private int age;



public String quick(){

return “name=”+name+",age="+age;



After operation , Browser access address :http://localhost:8080/quick give the result as follows

《 A big factory Java Analysis of interview questions + Back end development learning notes + The latest architecture explanation video + Practical project source code handout 》

【】 Full content open source sharing

2、 Using annotations @ConfigurationProperties mapping

Use Value Mapping can accurately match to a certain attribute , But if there are many properties , It brings inconvenience , therefore , You can also use  @ConfigurationProperties mapping , Through annotation @ConfigurationProperties(prefix=“ In the configuration file key The prefix of ”) The configuration in the configuration file can be automatically mapped to the entity

  • application.yml The configuration is as follows :


name: oneStar

age: 19

  • Entity Bean The code is as follows :

copyright notice
author[m0_ fifty-four million eight hundred and sixty-one thousand fou],Please bring the original link to reprint, thank you.

Random recommended