javax.annotation.PostConstruct的使用說明
以下是原文翻譯,注釋已經說明得很清楚了
The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization.
PostConstruct註解使用在方法上,這個方法需要在依賴注入初始化之後執行。
This method MUST be invoked before the class is put into service.
這個方法必須在該服務類使用之前執行。
This annotation MUST be supported on all classes that support dependency injection.
支援依賴注入的所有類都必須支援該註解。
The method annotated with PostConstruct MUST be invoked even if the class does not request any resources to be injected.
該方法必須執行,即使所在的類沒有注入任何資源。
Only one method can be annotated with this annotation.
只能有一個方法使用該註解。
The method on which the PostConstruct annotation is applied MUST fulfill all of the following criteria - -
使用了該註解的方法必須全部符合以下準則:
The method MUST NOT have any parameters except in the case of EJB interceptors in which case it takes an Invocation Context object as defined by the EJB specification.
除EJB介面外不能擁有任何參數,
- The return type of the method MUST be void.
傳回值必須是void
- The method MUST NOT throw a checked exception.
該方法不能拋出異常
- The method on which PostConstruct is applied MAY be public, protected, package private or private.
可以使用public、protected等修飾符
- The method MUST NOT be static except for the application client.
方法不是是static修飾,除非是應用程式用戶端
- The method MAY be final.
方法可以使用final修飾
- If the method throws an unchecked exception the class MUST NOT be put into service except in the case of EJBs where the EJB can handle exceptions and even recover from them.
方法如果拋出未知異常,則類絕不能作為service類,除非是可以處理異常甚至能恢複正常的EJB組件