SpringBoot之配置文件加載位置

一、SpringBoot啓動會掃描application.properties或者application.yml文件做爲springboot的配置文件。默認建立項目生成application.properties/yml位置在classpath目錄下,也能夠在如下4個地方建立優先級自上而下,可是各個配置文件成互補狀態存在

   * file: ./config/java

  * file: ./web

  * classpath: /config/spring

  * classpath:/springboot

測試1:classpath目錄下建立application.properties文件app

classpath:application.properties server.port=8888

 

測試2:classpath目錄下建立config目錄再建立application.properties文件ide

classpath:config/application.properties server.port=8001

 

測試3:項目根目錄下建立application.properties文件spring-boot

file:./application.properties server.port=8002

測試4:項目根目錄下建立config目錄再建立application.properties文件測試

file:./config/application.properties server.port=8003

 

二、在打包完成的狀況下,須要新增一些配置,這時該怎麼作呢? 能夠經過配置spring.config.location來改變默認配置。

  * G盤符下建立文件application.propertiesui

G:\application.propertiesidea

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...."; } }

  * 訪問出結果

相關文章
相關標籤/搜索