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 MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...