使用@PostConstruct
該注解方法是在Spring實例化該Bean之后馬上執行此方法,只執行一次,定義在void方法中
具體使用
1.定義兩個變量,其中一個變量靜態,一個非靜態,使用@Value注解獲取到配置文件的值
private static String name
@Value("{name}")
private String _name;
2.定義一個viod方法加PostConstruct注解,然后再方法里將非靜態變量的值賦予靜態變量
public void getName() {
name = this._name
}
注意:如果想要在Application啟動的使用就得到值,需要在類中添加@Component(裝配至spring bean容器中),