SpringBoot讀取配置值的方式

2021-09-24 14:45:59 字數 1355 閱讀 3920

@value註解的方式取值

xiaoming.***=boy

xiaoming.age=18

xiaoming.score=98

使用@value取值

@restcontroller

public class personcontroller ")

private string ***;

@value("$")

private integer age;

@value("$")

private integer score;

public string get()

}

頁面展示

小明==》性別:boy-----年齡:18-----分數:98

使用@configurationproperties賦值給實體類

person:

name: xiaoming

age: 18

@configurationproperties賦值給實體類

@component

@configurationproperties(prefix = "person")

public class person

public void setname(string name)

public integer getage()

public void setage(integer age)

}

請求資訊

@autowired

private person person;

public string getperson()

頁面展示

姓名:xiaoming-----年齡:18
通過注入獲取environment物件,然後再獲取定義在配置檔案的屬性值

springboot.test=hello-springboot
獲取environment物件,然後再獲取定義在配置檔案的屬性值

private static final string hello = "springboot.test";

@autowired

private environment environment;

public string getenv()

頁面展示

測試environment:hello-springboot
原始碼位址 ​​​​​​​

SpringBoot 讀取配置檔案屬性值

springboot有兩種配置檔案properties和yml配置檔案,但讀取的方式都是一樣的。主要有以下的兩種方式,以yml檔案作為講解 yml檔案 auth authurl cookiename auth.session.id version 1.01 使用 value註解將配置檔案的屬性注入到...

spring boot 配置檔案讀取

如圖所示,可以重新賦值予以覆蓋。mail setting 設定郵箱主機 email.host smtp.163.com email.port 25 設定使用者名稱 email.from xx xx.com 設定密碼 email.frompassword 設定是否需要認證,如果為true,那麼使用者名...

springBoot讀取配置檔案

1.讀核心配置檔案 2.讀自定義配置檔案 建立乙個配置檔案對應的實體類新增如下註解 configuration propertysource value classpath mail.properties configurationproperties prefix mail locations c...