1. java正常獲取yml配置文件的值,可以使用@value注解獲取
2. 經常會寫util等類,里面的方法都是靜態static的,這種情況下我們就沒法像普通方法一樣獲取到yml的配置
//定義一個yml配置文件里的靜態變量
private static String environments;
//通過value獲取yml配置文件參數
@Value("${meinong.SUPPLIER_KEY_ENVIRONMENT}")
private String environment;
//利用@PostConstruct將yml中配置的值賦給本地的變量
@PostConstruct
public void getEnvironment(){
environments = this.environment;
}
被@PostConstruct修飾的方法會在服務器加載Servle的時候運行,並且只會被服務器執行一次。PostConstruct在構造函數之后執行,init()方法之前執行。PreDestroy()方法在destroy()方法執行執行之后執行。
