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.properties
-
application.yml(application.yaml)
1. application.properties The configuration file
We can create application.properties Configuration file to overwrite SpringBoot Default configuration , Can be in resources Create under folder application.properties 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 application.properties Configuration file to modify the default configuration :
server.port=8888
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 application.properties 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:
person:
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:
map:
key1: value1
key2: value2
Configure array data :
-
grammar 1
-
key:
-
value1
-
value2
-
grammar 2
-
key: [value1,value2]
-
eg:
city:
-
beijing
-
tianjin
-
shanghai
-
chongqing
# perhaps
city: [beijing,tianjin,shanghai,chongqing]
# The elements in the collection are in the form of objects
student:
- 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 :
person:
name: oneStar
age: 18
- Entity Bean The code is as follows :
@Controller
public class QuickController {
@Value("${person.name}")
private String name;
@Value("${person.age}")
private int age;
@RequestMapping("/quick")
@ResponseBody
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 》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 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 :
person:
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.
https://en.cdmana.com/2022/01/202201262134016000.html
The sidebar is recommended
- Spring IOC container loading process
- [thinking] the difference between singleton mode and static method - object-oriented programming
- Hadoop environment setup (MySQL environment configuration)
- 10 minutes, using node JS creates a real-time early warning system for bad weather!
- Git tool
- Force deduction algorithm - 92 Reverse linked list II
- What is the sub problem of dynamic programming?
- C / C + +: static keyword summary
- Idea does not have the artifacts option when configuring Tomcat
- Anaconda can't open it
guess what you like
-
I don't know how to start this
-
Matlab simulation of transportation optimization algorithm based on PSO
-
MySQL slow log optimization
-
[Vue] as the window is stretched (larger, smaller, wider and higher), the text will not be displayed
-
Popular Linux distributions for embedded computing
-
Suzhou computer research
-
After installing SSL Certificate in Windows + tomcat, the domain name request is not successful. Please answer!!
-
Implementation time output and greetings of jQuery instance
-
The 72 year old uncle became popular. Wu Jing and Guo fan made his story into a film, which made countless dreamers blush
-
How to save computer research
Random recommended
- Springboot implements excel import and export, which is easy to use, and poi can be thrown away
- The final examination subjects of a class are mathematical programming, and the scores are sorted and output from high to low
- Two pronged approach, Tsinghua Professor Pro code JDK and hotspot source code notes, one-time learning to understand
- C + + recursive knapsack problem
- The use of GIT and GitHub and the latest git tutorial are easy to understand -- Video notes of crazy God speaking
- PostgreSQL statement query
- Ignition database test
- Context didn't understand why he got a high salary?, Nginxfair principle
- Bootstrap switch switch control user's guide, springcloud actual combat video
- A list that contains only strings. What other search methods can be used except sequential search
- [matlab path planning] multi ant colony algorithm grid map path planning [including GUI source code 650]
- [matlab path planning] improved genetic algorithm grid map path planning [including source code phase 525]
- Iinternet network path management system
- Appium settings app is not running after 5000ms
- Reactnative foundation - 07 (background image, status bar, statusbar)
- Reactnative foundation - 04 (custom rpx)
- If you want an embedded database (H2, hsql or Derby), please put it on the classpath
- When using stm32g070 Hal library, if you want to write to flash, you must perform an erase. If you don't let it, you can't write continuously.
- Linux checks where the software is installed and what files are installed
- SQL statement fuzzy query and time interval filtering
- 69. Sqrt (x) (c + + problem solving version with vs runnable source program)
- Fresh students are about to graduate. Do you choose Java development or big data?
- Java project: OA management system (java + SSM + bootstrap + MySQL + JSP)
- Titanic passenger survival prediction
- Vectorization of deep learning formula
- Configuration and use of private image warehouse of microservice architect docker
- Relearn JavaScript events
- For someone, delete return 1 and return 0
- How does Java dynamically obtain what type of data is passed? It is used to judge whether the data is the same, dynamic data type
- How does the database cow optimize SQL?
- [data structure] chain structure of binary tree (pre order traversal) (middle order traversal) (post order traversal) (sequence traversal)
- Webpack packaging optimization solution
- 5. Operation element
- Detailed explanation of red and black trees
- redhat7. 9 install database 19C
- Blue Bridge Cup notes: (the given elements are not repeated) complete arrangement (arrangement cannot be repeated, arrangement can be repeated)
- Detailed explanation of springboot default package scanning mechanism and @ componentscan specified scanning path
- How to solve the run-time exception of test times
- Detailed explanation of k8s management tool kubectl
- Android system view memory command