Springboot生產配置檔案

2021-08-26 14:48:06 字數 1592 閱讀 6318

一、springboot生產服務需要配置檔案在專案jar包外面,所以需要重新建立乙個目錄。

二、springboot的載入配置檔案一共有4種方法。

1.第一種:

獲取屬性值

@value("$")

private string name;

imagesurl.name=
2.第二種

獲取屬性值

import org.springframework.context.annotation.propertysource;

//預設訪問本地的resource路徑的配置檔案

public static void main(string args)

}

aaa=你好
3.第三種

獲取屬性的值

import org.springframework.beans.factory.annotation.autowired;

import org.springframework.core.env.environment;

import org.springframework.stereotype.component;

@component

public class config

}

string.port=80
4.第四種

獲取值

// 載入yml格式自定義配置檔案

@bean

public static propertysourcesplaceholderconfigurer properties()

@value引入屬性值注入bean

@component

@configurationproperties(prefix = "prefix")

public class config ")

private string username;

}

prefix:

name:

list:

name: tech

key: 123

source: beijing

name: skill

key: 987

source: shanghai

your:

username: test

注意:

因為我是用的是eclipse,所以 file:$ 在windows環境下會取到eclipse路徑下,而在linxu伺服器上則會取到你當前放置war包的weblogic的domain下,之後拼接你的路徑即可。 當然如果許可權足夠的話,也可以用file:$來獲取properties的值,windows的話是document/../..的路徑,如果是linxu則是根目錄下home的路徑。 當然如果你要取包內的properties,用classpath:就可以解決了,是取classes下的路徑。 這樣修改之後就能完成(war包或者jar包)和配置檔案的分離。

《參考:

Spring Boot讀取配置檔案與配置檔案優先順序

spring boot讀取配置檔案 package com.ivan.config.controller import org.springframework.beans.factory.annotation.autowired import org.springframework.core.env...

Spring Boot 菜鳥教程 28 多配置檔案

src scrolling 0 width 110 height 20 開發過程中可能會有如下需求 開發和部署的配置資訊可能不同,常規的方式就是在配置檔案裡面先寫好開發配置,在部署的時候再去修改這些配置,這樣可能會出現很多問題,比如使用者名稱 密碼忘記了修改或者改錯了等問題。author je哥 e...

SpringBoot之配置檔案YAML檔案簡單介紹

1.基本語法 k 空格 v 表示一對鍵值對,空格必須有 例如 lucy前方要有空格 name lucy以空格縮排控制層級關係,左對齊屬性表示同一級別 person name fangxinde age 28 job 大資料 server port 80802.值的寫法 1 字面量的表示方法 字串預設...