Java中的@PostConstruct注解的使用
問題
之前在做后端項目遇到一個奇怪的問題,我裝配到Spring容器中的一個Bean在另外一個類中無法被注入,該Bean的類型如下:
@Component
@Data
public class FeishuCrawlerConfig{...}我使用@Component注解將其裝配到Spring容器中,然后在另外一個類中將其自動注入,格式如下:
@Component
public class FeishuTenantTokenEntity {
@Autowired
private FeishuCrawlerConfig feishuConfig;
...
}正常來講feishuConfig這個變量會被自動注入完成初始化,但是后面我在使用這個變量時卻拋出了NPE。后來經(jīng)過跟同事的討論才發(fā)現(xiàn)我是在FeishuTenantTokenEntity的構(gòu)造方法中調(diào)用到了feishuConfig的方法,導(dǎo)致了NPE的產(chǎn)生。構(gòu)造方法如下:
public FeishuTenantTokenEntity() IOException {
// refreshTokenValue()方法中使用到feishuConfig的方法
this.tokenValue = refreshTokenValue();
this.accessTime = System.currentTimeMillis();
}產(chǎn)生這個問題的原因是Spring中的Bean在初始化時,會先執(zhí)行其構(gòu)造方法,再注入@Autowired注解標(biāo)注的其他Bean。我們的代碼中在構(gòu)造方法中就使用到了@Autowired注入的feishuConfig對象,這時它還沒有初始化完成,自然會拋出NPE。這個問題的解決方法就是使用@PostConstruct注解標(biāo)注的方法替代FeishuTenantTokenEntity的構(gòu)造方法,該方法如下:
@PostConstruct
public void init() throws IOException {
this.tokenValue = refreshTokenValue();
this.accessTime = System.currentTimeMillis();
}Spring在初始化Bean時,會在注入@Autowired注解標(biāo)注的Bean后執(zhí)行@PostConstruct注解標(biāo)注的方法。我們在init()方法中使用feishuConfig的方法顯然是沒問題的,并且它還能替代構(gòu)造方法的作用。Spring中Bean初始化的執(zhí)行順序是構(gòu)造方法>依賴注入( @Autowired )> @PostConstruct標(biāo)注的方法。
@PostConstruct注解
講到這里,不得不細(xì)說一下@PostConstruct注解。
/**
* The PostConstruct annotation is used on a method that needs to be executed
* after dependency injection is done to perform any initialization. This
* method MUST be invoked before the class is put into service. This
* annotation MUST be supported on all classes that support dependenc
* 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:
* <p>
* <ul>
* <li>The method MUST NOT have any parameters except in the case of
* interceptors in which case it takes an InvocationContext object as
* defined by the Interceptors specification.</li>
* <li>The method defined on an interceptor class MUST HAVE one of the
* following signatures:
* <p>
* void <METHOD>(InvocationContext)
* <p>
* Object <METHOD>(InvocationContext) throws Exception
* <p>
* <i>Note: A PostConstruct interceptor method must not throw application
* exceptions, but it may be declared to throw checked exceptions including
* the java.lang.Exception if the same interceptor method interposes on
* business or timeout methods in addition to lifecycle events. If a
* PostConstruct interceptor method returns a value, it is ignored by
* the container.</i>
* </li>
* <li>The method defined on a non-interceptor class MUST HAVE the
* following signature:
* <p>
* void <METHOD>()
* </li>
* <li>The method on which PostConstruct is applied MAY be public, protected,
* package private or private.</li>
* <li>The method MUST NOT be static except for the application client.</li>
* <li>The method MAY be final.</li>
* <li>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.</li></ul>
* @since Common Annotations 1.0
* @see javax.annotation.PreDestroy
* @see javax.annotation.Resource
*/
@Documented
@Retention (RUNTIME)
@Target(METHOD)
public @interface PostConstruct {
}這是Java官方對@PostConstruct注解的注釋文檔,可以看到@PostConstruct注解是用于初始化方法上,該方法在依賴注入完成后執(zhí)行。Java對被@PostConstruct標(biāo)注的方法做出了如下限制:
- 除攔截器方法外,該方法不能有任何參數(shù)
- 被
@PostConstruct標(biāo)注的攔截器方法不能拋出application異常 - 該方法可以被public,protected,package private和private修飾
- 該方法不能為靜態(tài)的,但是可以被final修飾 簡而言之,如果你的類中的構(gòu)造方法需要使用到依賴注入的變量,你可以用
@PostConstruct標(biāo)注的方法來替代構(gòu)造方法完成初始化。
Spring如何實現(xiàn) @PostConstruct注解
我們知道在Spring中,Bean的初始化一般分為實例化,屬性賦值,初始化和銷毀這四個過程。在實例化階段的前后,InstantiationAwareBeanPostProcessor接口的postProcessBeforeInstantiation和postProcessAfterInstantiation方法會被調(diào)用。在初始化階段的前后,BeanPostProcessor接口的postProcessBeforeInitialization和postProcessAfterInitialization方法會被調(diào)用。開發(fā)者也可以繼承這些接口拓展功能。如下圖所示:

在這四個過程中間,Bean的依賴注入發(fā)生在屬性賦值這個階段。Spring會在postProcessBeforeInstantiation方法中也就是依賴注入完成之后調(diào)用@PostConstruct注解標(biāo)注的方法,完成Bean的部分初始化工作。 Spring的具體做法就是在創(chuàng)建Bean時,會將它里面被@PostConstruct注解標(biāo)注的方法保存到Bean的元數(shù)據(jù)中,在后面調(diào)用postProcessBeforeInstantiation方法時,會利用反射調(diào)用Bean的元數(shù)據(jù)中被 @PostConstruct注解標(biāo)注的方法,從而完成部分初始化工作。感興趣的同學(xué)可以看看源碼。
注意
Java官方已在Java 9中棄用了@PostConstruct注解,并在Java 11中刪除了@PostConstruct注解。 實現(xiàn)InitializingBean接口并重寫其中的afterPropertiesSet方法也可以實現(xiàn)@PostConstruct注解相同的功能。
/**
* Interface to be implemented by beans that need to react once all their properties
* have been set by a {@link BeanFactory}: e.g. to perform custom initialization,
* or merely to check that all mandatory properties have been set.
*
* <p>An alternative to implementing {@code InitializingBean} is specifying a custom
* init method, for example in an XML bean definition. For a list of all bean
* lifecycle methods, see the {@link BeanFactory BeanFactory javadocs}.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @see DisposableBean
* @see org.springframework.beans.factory.config.BeanDefinition#getPropertyValues()
* @see org.springframework.beans.factory.support.AbstractBeanDefinition#getInitMethodName()
*/
public interface InitializingBean {
/**
* Invoked by the containing {@code BeanFactory} after it has set all bean properties
* and satisfied {@link BeanFactoryAware}, {@code ApplicationContextAware} etc.
* <p>This method allows the bean instance to perform validation of its overall
* configuration and final initialization when all bean properties have been set.
* @throws Exception in the event of misconfiguration (such as failure to set an
* essential property) or if initialization fails for any other reason
*/
void afterPropertiesSet() throws Exception;
}到此這篇關(guān)于Java中的@PostConstruct注解的使用的文章就介紹到這了,更多相關(guān)Java @PostConstruct注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java注解之Retention、Documented、Inherited介紹
- 詳解Java的Spring框架中的注解的用法
- Java使用@Validated注解進(jìn)行參數(shù)驗證的方法
- Java中l(wèi)ombok的@Builder注解的解析與簡單使用詳解
- 全面解析Java中的注解與注釋
- 5分鐘搞懂java注解@Annotation的具體使用
- java中@ModelAttribute注解的作用
- Java注解Annotation與自定義注解詳解
- 由@NotNull注解引出的關(guān)于Java空指針的控制
- java 注解annotation的使用以及反射如何獲取注解
- 詳解Java編程中Annotation注解對象的使用方法
- Java Spring注解之@Async的基本用法和示例
相關(guān)文章
方法參數(shù)屬性params,@PathVariable和@RequestParam用法及區(qū)別
這篇文章主要介紹了方法參數(shù)屬性params,@PathVariable和@RequestParam用法及區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
System.identityHashCode和hashCode的區(qū)別及說明
String調(diào)用hashCode()和System.identityHashCode()返回值不同是因為String重寫了hashCode()方法,而System.identityHashCode()返回對象的內(nèi)存地址哈希值;Test調(diào)用兩個方法返回值相同是因為Test沒有重寫hashCode()方法,因此兩者調(diào)用底層的JVM_IHashCode方法返回相同值2024-11-11
Mybatis開發(fā)要點-resultType和resultMap有什么區(qū)別詳解
本文主要介紹了Mybatis開發(fā)要點-resultType和resultMap有什么區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
Java實現(xiàn)Excel數(shù)據(jù)驗證功能
在Java中,開發(fā)者可以使用一些開源的庫(如Apache POI)來添加、修改和處理Excel中的數(shù)據(jù),下面我們就來看看如何使用Java實現(xiàn)添加,修改和刪除Excel數(shù)據(jù)驗證吧2023-10-10
Java實現(xiàn)漢字轉(zhuǎn)全拼音的方法總結(jié)
在軟件開發(fā)中,經(jīng)常會遇到需要將漢字轉(zhuǎn)換成拼音的場景,比如在搜索引擎優(yōu)化、數(shù)據(jù)存儲、國際化等方面,Java作為一種廣泛使用的編程語言,提供了多種方法來實現(xiàn)漢字到拼音的轉(zhuǎn)換,本文將詳細(xì)介紹幾種常用的Java漢字轉(zhuǎn)全拼音的方法,并提供具體的代碼示例和步驟2024-12-12
Spring Boot 開發(fā)私有即時通信系統(tǒng)(WebSocket)
本文利用Spring Boot作為基礎(chǔ)框架,Spring Security作為安全框架,WebSocket作為通信框架,實現(xiàn)點對點聊天和群聊天2017-04-04
Java對MySQL數(shù)據(jù)庫進(jìn)行連接、查詢和修改操作方法
這篇文章主要介紹了Java對MySQL數(shù)據(jù)庫進(jìn)行連接、查詢和修改操作方法,需要的朋友可以參考下2017-07-07

