add springboot 配置文件j加载顺序
This commit is contained in:
		
							
								
								
									
										38
									
								
								java/Springboot配置文件与参数加载优先级.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								java/Springboot配置文件与参数加载优先级.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,38 @@
 | 
				
			|||||||
 | 
					## Springboot 配置文件 与参数加载优先级
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### 加载顺序
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					在 Spring Boot 中,配置文件的加载顺序是按照以下规则:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					1. **bootstrap.yml 或 bootstrap.properties**: 这是最先加载的配置文件,用于配置应用程序上下文的基础设施,例如外部配置源和加密/解密。
 | 
				
			||||||
 | 
					2. **application.yml 或 application.properties**: 这是主配置文件,包含应用程序的常规配置。
 | 
				
			||||||
 | 
					3. **application-{profile}.yml 或 application-{profile}.properties**: 针对不同的环境(profile)加载相应的配置文件。例如,`application-dev.yml` 用于开发环境,`application-prod.yml` 用于生产环境。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### 配置文件目录
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					SpringBoot配置文件可以放置在多种路径下,不同路径下的配置优先级有所不同。
 | 
				
			||||||
 | 
					可放置目录**(优先级从高到低)**
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- **file:./config/** (当前项目路径config目录下);
 | 
				
			||||||
 | 
					- **file:./** (当前项目路径下);
 | 
				
			||||||
 | 
					- **classpath:/config/** (类路径config目录下);
 | 
				
			||||||
 | 
					- **classpath:/** (类路径config下).
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					优先级由高到底,高优先级的配置会覆盖低优先级的配置;
 | 
				
			||||||
 | 
					SpringBoot会从这四个位置全部加载配置文件并互补配置;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### 命令行参数、环境变量
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Spring Boot 的配置加载顺序(**后者覆盖前者**):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					1. **`application.yml` / `application.properties`**(默认配置)
 | 
				
			||||||
 | 
					2. **`-D` JVM 参数**(如 `-Dserver.port=8080`)
 | 
				
			||||||
 | 
					3. **`--` 命令行参数**(如 `--server.port=8081`)
 | 
				
			||||||
 | 
					4. **环境变量**(如 `SERVER_PORT=8082`)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		Reference in New Issue
	
	Block a user