老是忘記,就做個記錄
@Value
@Value
不能和static
和final
搭配使用
@Value("${XXX.XXX}")
public static String xxx; // 錯誤
@Value("${XXX.AAA}")
public finalString xxx; // 錯誤
- 類沒有加上@Component(或者@service等)
- 類被new新建了實例,而沒有使用@Autowired
@PostConstruct
這個不是spring的注解,是java自己的
@PostConstruct該注解被用來修飾一個非靜態的void()方法。被@PostConstruct修飾的方法會在服務器加載Servlet的時候運行,並且只會被服務器執行一次。PostConstruct在構造函數之后執行,init()方法之前執行。
舉例:
@Autowired
private Environment env;
private String ecUrl;
@PostConstruct
public void init(){
ecUrl = env.getProperty("ec.url");
}