SpringBoot之配置文件加載位置


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

  * 訪問出結果

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM