![](/img/trans.png)
[英]Spring behaviour if a java method is annotated with both @Bean and @PostConstruct
[英]Spring: Why method annotated with @PostConstruct cannot be static?
我正在本網站上閱讀有關@PostConstruct
文檔: https : //www.baeldung.com/spring-postconstruct-predestroy
這個已經寫完了:
用@PostConstruct 注釋的方法可以有任何訪問級別,但不能是靜態的。
有人能告訴我為什么用這個注釋注釋的方法不能是靜態的嗎?
好吧,方法的名稱已經說明了它的作用。
PostConstruct ,此方法將在構造函數之后調用。 它不能是靜態的,因為靜態方法不能訪問非靜態變量、方法等。
如果您需要靜態運行一次,則可以使用靜態塊。
有人能告訴我為什么用這個注釋注釋的方法不能是靜態的嗎?
標有@PostConstruct
的方法是Spring 應該在創建bean 實例后調用的方法。 該方法一般用於對實例做一些后期構建配置。 將該方法設為靜態是沒有意義的,因為靜態方法可能不會與類的任何實例中的任何實例狀態交互。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.