【spring系列】之8:屬性賦值
阿新 • • 發佈:2018-12-14
一:使用@Value賦值
- 基本數值
- 可以寫SpEL; #{}
- 可以寫${};取出配置檔案【properties】中的值(在執行環境變數裡面的值)
demo:set,get省略
@Value("張三")
private String name;
@Value("#{20-2}")
private Integer age;
@Value("${person.nickName}")
private String nickName;
二:使用外部PropertySource載入外部配置檔案
@PropertySource(value={"classpath:/person.properties"})
value是個陣列,可以引入多個配置檔案,這樣,我們可以很方便的引入多個外部配置檔案了
@PropertySource是一個可重複的註解,即我們可以在一個類上用多個@PropertySource引入多個配置檔案,也可以通過陣列的方法用@PropertySource一次性引入多個檔案
備註下:
ConfigurableEnvironment environment = applicationContext.getEnvironment(); String property = environment.getProperty("person.nickName");
其實所有的配置檔案中的值,最後都儲存在了ConfigurableEnvironment中,我們同樣可以在ConfigurableEnvironment中通過key獲取我們想要的值