首頁  >  文章  >  Java  >  如何在Spring應用程式上下文中存取系統環境變數?

如何在Spring應用程式上下文中存取系統環境變數?

Susan Sarandon
Susan Sarandon原創
2024-10-27 08:51:02689瀏覽

How to Access System Environment Variables in Spring Application Context?

讀取Spring 應用程式上下文中的系統環境變數

在Spring 中,可以使用Spring 表達式語言來存取應用程式上下文中的系統環境變數(SpEL) 在Spring 3.0 中引入。為此,請按照以下步驟操作:

  • 在應用程式上下文中使用 SpEL 表達式的 ${} 語法。
  • 存取 systemProperties 映射以檢索系統環境變數。例如,要存取env 變數:
#{systemProperties.env}
  • 將此表達式整合到屬性位置屬性中:
<code class="xml"><util:properties id="dbProperties" location="classpath:config_#{systemProperties.env}/db.properties" /></code>

透過執行此程式碼- Denv=QA,可根據系統環境變數動態變更屬性位置。

存取作業系統層級變數

存取作業系統上的系統環境變數層級(而非JVM 中設定的層級):

#{systemEnvironment['ENV_VARIABLE_NAME']}

例如,要存取ENV_VARIABLE_NAME 環境變數:

<code class="xml"><util:properties id="dbProperties" location="classpath:config_#{systemEnvironment.ENV_VARIABLE_NAME}/db.properties" /></code>

以上是如何在Spring應用程式上下文中存取系統環境變數?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn