spring boot 配置檔案讀取

2021-07-25 17:21:06 字數 2158 閱讀 3048

如圖所示,可以重新賦值予以覆蓋。

### mail setting ###

# 設定郵箱主機

email.host=smtp.163.com

email.port=25

# 設定使用者名稱

email.from=***[email protected]

# 設定密碼

email.frompassword=******

# 設定是否需要認證,如果為true,那麼使用者名稱和密碼就必須的,

# 如果設定false,可以不設定使用者名稱和密碼,當然也得看你的對接的平台是否支援無密碼進行訪問的。

email.smtpauth=false

然後可以通過如下方式獲取

@autowired

private enviroment env;

public void testprop()

package com.properties;

import org.springframework.boot.context.properties.configurationproperties;

@configurationproperties(prefix = "email")

public class emailprop

public void sethost(string host)

public string getport()

public void setport(string port)

public string getfrom()

public void setfrom(string from)

public string getfrompassword()

public void setfrompassword(string frompassword)

public string getsmtpauth()

public void setsmtpauth(string smtpauth)

}

使用的時候如下所示

@autowired

private emailprop emailprop;

和上面的**基本一致,只是註解configurationproperties中需要增加指定讀取配置檔案的屬性,

在resource下增加config目錄,建立email.properties檔案,如圖所示

**如下所示

package com.properties;

import org.springframework.boot.context.properties.configurationproperties;

@configurationproperties(prefix = "email", locations = )

public class emailprop

public void sethost(string host)

public string getport()

public void setport(string port)

public string getfrom()

public void setfrom(string from)

public string getfrompassword()

public void setfrompassword(string frompassword)

public string getsmtpauth()

public void setsmtpauth(string smtpauth)

}

在使用的過程中,記得在啟動類上增加如下注釋

讀配置檔案

tchar szpath max path memset szpath,0,sizeof szpath getmodulefilename null,szpath,max path cstring strpath szpath strpath strpath.left strpath.reverse...

Spring Boot配置檔案

方式 示例 檔案路徑 classpath或者classpath的 config目錄下 檔案內容 全域性通用配置 方式 通過environment獲取 示例 system.out println ctx.getenvironment getproperty env 方式一 configurationp...

springboot配置檔案

配置檔案的作用 修改springboot自動配置的預設值 yaml yaml ain t markup language yaml a markup language 是乙個標記語言 yaml isn t markup language 不是乙個標記語言 標記語言 以前的配置檔案大都是xml檔案,y...