1、SpringBoot啟動會掃描application.properties或者application.yml文件作為springboot的配置文件。默認創建項目生成application.properties/yml位置在classpath目錄下,也可以在以下4個地方創建,優先級自上而下,但是各個配置文件成互補狀態存在。
* file: ./config/
* file: ./
* classpath: /config/
* classpath:/
測試1:classpath目錄下創建application.properties文件
classpath:application.properties
server.port=8888

測試2:classpath目錄下創建config目錄再創建application.properties文件
classpath:config/application.properties
server.port=8001

測試3:項目根目錄下創建application.properties文件
file:./application.properties
server.port=8002

測試4:項目根目錄下創建config目錄再創建application.properties文件
file:./config/application.properties
server.port=8003

2、在打包完成的情況下,需要新增一些配置,這時該怎么做呢? 可以通過配置spring.config.location來改變默認配置。
* G盤符下創建文件application.properties
G:\application.properties
server.port=9999
server.servlet.context-path=/boot02
* 在idea中terminal執行語句:
java -jar spring-boot-02-config-0.0.1-SNAPSHOT.jar --spring.config.location=G:\application.properties
* controller代碼
package com.atguigu.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello") public String hello() { return "hello...."; } }
* 訪問出結果

