Spring Boot 讀取配置文件

来源:https://www.cnblogs.com/gdjlc/archive/2019/09/23/11575743.html
-Advertisement-
Play Games

一、預設配置文件 二、指定配置文件 三、使用profile指定配置 ...


開發環境:IntelliJ IDEA 2019.2.2
Spring Boot版本:2.1.8

新建一個名稱為demo的Spring Boot項目。

一、預設配置文件

Spring Boot會讀取名稱application.properties(yml)的配置文件。
如果有多個同名文件,預設情況下,按照下麵順序讀取:
(1)項目根目錄的config目錄
(2)項目根目錄
(3)項目classpath下的config目錄
(4)項目classpath根目錄
如果同一個配置項出現在多份配置文件中,後面讀取的值不會覆蓋前面的。

測試:
在項目的4個位置各建立application.properties,內容如下:
(1)config/application.properties

test = config/application.properties
test1 = test1

(2)application.properties

test = application.properties
test2 = test2

(3)src/main/resources/config/application.properties

test = src/main/resources/config/application.properties
test3 = test3

(4)src/main/resources/application.properties

test = src/main/resources/application.properties
test4 = test4

 修改預設生成的啟動類 DemoApplication.cs 代碼:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Autowired
    private Environment env;

    @RequestMapping("/")
    public String getProp(){
        String test = env.getProperty("test");
        String test1 = env.getProperty("test1");
        String test2 = env.getProperty("test2");
        String test3 = env.getProperty("test3");
        String test4 = env.getProperty("test4");
        return test + "," + test1 + "," + test2 + "," + test3 + "," + test4;
    }
}

訪問 http://localhost:8080/
輸出:config/application.properties,test1,test2,test3,test4

二、指定配置文件

讀取指定的配置文件,不使用預設的application.properties。

測試:
(1)src/main/resources/application.properties 內容:

test1 = application.properties

(2)在項目的src/main/resources新建目錄config,新建配置文件myConfig.properties,內容:

test2= myConfig.properties

修改預設生成的啟動類 DemoApplication.cs 代碼:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {

    public static void main(String[] args) {
        //SpringApplication.run(DemoApplication.class, args);
        new SpringApplicationBuilder(DemoApplication.class).properties(
                "spring.config.location=classpath:/config/myConfig.properties"
        ).run(args);
    }

    @Autowired
    private Environment env;

    @RequestMapping("/")
    public String getProp(){
        String test1 = env.getProperty("test1");
        String test2 = env.getProperty("test2");
        return test1 + "," + test2;
    }
}

訪問 http://localhost:8080/
輸出:null,myConfig.properties
可見application.properties已讀取不到,成功讀取到配置文件myConfig.properties。

也可以使用spring.config.name指定配置文件的名稱,如下麵代碼指定了myConfig,Spring Boot會到classpath下尋找myConfig.properties(yml)。

    public static void main(String[] args) {
        //SpringApplication.run(DemoApplication.class, args);
        /*new SpringApplicationBuilder(DemoApplication.class).properties(
                "spring.config.location=classpath:/config/myConfig.properties"
        ).run(args);*/
        new SpringApplicationBuilder(DemoApplication.class).properties(
                "spring.config.name=myConfig").run(args);
    }

三、使用profile指定配置

使用profile可以根據特定的環境來激活不同的配置。

src/main/resources/application.yml 內容如下:

spring:
  profiles: mysql
jdbc:
  driver:
    com.mysql.jdbc.Driver
---
spring:
  profiles: oracle
jdbc:
  driver:
    oracle.jdbc.driver.OracleDriver

修改預設生成的啟動類 DemoApplication.cs 代碼:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Scanner;

@SpringBootApplication
@RestController
public class DemoApplication {

    public static void main(String[] args) {
        //SpringApplication.run(DemoApplication.class, args);

        Scanner scan = new Scanner(System.in);
        String profile = scan.nextLine();
        new SpringApplicationBuilder(DemoApplication.class).properties(
                "spring.config.location=classpath:/application.yml"
        ).profiles(profile).run(args);
    }

    @Autowired
    private Environment env;

    @RequestMapping("/")
    public String getProp(){
        String res = env.getProperty("jdbc.driver");
        return res;
    }
}

在IDEA中點擊Run按鈕後,在控制台先敲回車再輸入oracle,
訪問 http://localhost:8080/ 輸出:oracle.jdbc.driver.OracleDriver
重新Run,在控制台先敲回車再輸入mysql,
訪問 http://localhost:8080/ 輸出:com.mysql.jdbc.Driver

還可以通過不同配置文件的名稱來設置profile,創建下麵3個文件。
(1)src/main/resources/application.yml 內容:

spring:
  profiles:
    active: oracle

(2)src/main/resources/application-mysql.yml 內容:

jdbc:
  driver:
    com.mysql.jdbc.Driver

(3)src/main/resources/application-oracle.yml 內容:

jdbc:
  driver:
    oracle.jdbc.driver.OracleDriver

 修改預設生成的啟動類 DemoApplication.cs 代碼:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Scanner;

@SpringBootApplication
@RestController
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);     
    }

    @Autowired
    private Environment env;

    @RequestMapping("/")
    public String getProp(){
        String res = env.getProperty("jdbc.driver");
        return res;
    }
}

訪問 http://localhost:8080/ 輸出:oracle.jdbc.driver.OracleDriver


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一 語法 模板渲染的 "官方文檔" 關於模板渲染你只需要記兩種特殊符號(語法): {{ }}和 {% %} 變數相關的用{{}},邏輯相關的用{%%}。 二 變數 在Django的模板語言中按此語法使用:{{ 變數名 }}。 當模版引擎遇到一個變數,它將計算這個變數,然後用結果替換掉它本身。 變數的 ...
  • 黃燜雞米飯最熱賣的外賣之一,國人都喜歡吃,吃過黃燜雞米飯的應該都知道,除了黃燜雞米飯主體外,還可以添加各種配菜,如土豆、香菇、鵪鶉蛋、青菜等。如果需要你來設計一套黃燜雞米飯結賬系統,你該如何設計呢? 前置條件:主體:黃燜雞米飯 價格:16,配菜:土豆 價格:2、香菇 價格:2、鵪鶉蛋 價格:2、青菜 ...
  • 橋接模式(Bridge): 橋接是用於把抽象化與實現化解耦,使得兩者可以獨立變化。 橋接模式的角色: 1)抽象化角色(Abstraction):它是用於定義抽象介面,通常是抽象類而不是介面,其中定義了一個Implementor(實現介面)類型的對象並可以維護該對象,它與Implementor之間具有 ...
  • 1. 下載安裝包 1 2 3 https://www.python.org/ftp/python/2.7.14/python-2.7.14.amd64.msi # 2.7安裝包 https://www.python.org/ftp/python/3.6.4/python-3.6.4-amd64.ex ...
  • ​一、生成器 1.定義(generator):一邊迴圈一邊計算下一個元素的機制/演算法 2.滿三個條件 (1)每次調用都能產生出for迴圈需要的下一個元素 (2)如果達到最後一個後,能夠爆出StopIteration異常 (3)可以被next函數調用 3.如何生成一個生成器 (1)直接使用 (2)如果 ...
  • # 1.在伺服器上 tomcat 的 bin目錄下找到並打開 catalina.sh 在文件中搜索: ``` JPDA_ADDRESS= ``` 找一個伺服器上沒有被使用的埠,填入,如50005,保存並退出。 > 如何知道某埠有沒有被占用? > 命令: > ``` > lsof -i:50005 ...
  • idea搭建spring源碼閱讀環境 安裝gradle Github下載Spring源碼 新建學習spring源碼的項目 idea搭建spring源碼閱讀環境 安裝gradle Github下載Spring源碼 新建學習spring源碼的項目 安裝gradle Github下載Spring源碼 新建 ...
  • 2019-09-23-23:48:00 今日所學的內容有: ...
一周排行
    -Advertisement-
    Play Games
  • 基於.NET Framework 4.8 開發的深度學習模型部署測試平臺,提供了YOLO框架的主流系列模型,包括YOLOv8~v9,以及其系列下的Det、Seg、Pose、Obb、Cls等應用場景,同時支持圖像與視頻檢測。模型部署引擎使用的是OpenVINO™、TensorRT、ONNX runti... ...
  • 十年沉澱,重啟開發之路 十年前,我沉浸在開發的海洋中,每日與代碼為伍,與演算法共舞。那時的我,滿懷激情,對技術的追求近乎狂熱。然而,隨著歲月的流逝,生活的忙碌逐漸占據了我的大部分時間,讓我無暇顧及技術的沉澱與積累。 十年間,我經歷了職業生涯的起伏和變遷。從初出茅廬的菜鳥到逐漸嶄露頭角的開發者,我見證了 ...
  • C# 是一種簡單、現代、面向對象和類型安全的編程語言。.NET 是由 Microsoft 創建的開發平臺,平臺包含了語言規範、工具、運行,支持開發各種應用,如Web、移動、桌面等。.NET框架有多個實現,如.NET Framework、.NET Core(及後續的.NET 5+版本),以及社區版本M... ...
  • 前言 本文介紹瞭如何使用三菱提供的MX Component插件實現對三菱PLC軟元件數據的讀寫,記錄了使用電腦模擬,模擬PLC,直至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1. PLC開發編程環境GX Works2,GX Works2下載鏈接 https:// ...
  • 前言 整理這個官方翻譯的系列,原因是網上大部分的 tomcat 版本比較舊,此版本為 v11 最新的版本。 開源項目 從零手寫實現 tomcat minicat 別稱【嗅虎】心有猛虎,輕嗅薔薇。 系列文章 web server apache tomcat11-01-官方文檔入門介紹 web serv ...
  • 1、jQuery介紹 jQuery是什麼 jQuery是一個快速、簡潔的JavaScript框架,是繼Prototype之後又一個優秀的JavaScript代碼庫(或JavaScript框架)。jQuery設計的宗旨是“write Less,Do More”,即倡導寫更少的代碼,做更多的事情。它封裝 ...
  • 前言 之前的文章把js引擎(aardio封裝庫) 微軟開源的js引擎(ChakraCore))寫好了,這篇文章整點js代碼來測一下bug。測試網站:https://fanyi.youdao.com/index.html#/ 逆向思路 逆向思路可以看有道翻譯js逆向(MD5加密,AES加密)附完整源碼 ...
  • 引言 現代的操作系統(Windows,Linux,Mac OS)等都可以同時打開多個軟體(任務),這些軟體在我們的感知上是同時運行的,例如我們可以一邊瀏覽網頁,一邊聽音樂。而CPU執行代碼同一時間只能執行一條,但即使我們的電腦是單核CPU也可以同時運行多個任務,如下圖所示,這是因為我們的 CPU 的 ...
  • 掌握使用Python進行文本英文統計的基本方法,並瞭解如何進一步優化和擴展這些方法,以應對更複雜的文本分析任務。 ...
  • 背景 Redis多數據源常見的場景: 分區數據處理:當數據量增長時,單個Redis實例可能無法處理所有的數據。通過使用多個Redis數據源,可以將數據分區存儲在不同的實例中,使得數據處理更加高效。 多租戶應用程式:對於多租戶應用程式,每個租戶可以擁有自己的Redis數據源,以確保數據隔離和安全性。 ...