Java中的@PostConstruct注解
@PostConstruct基本:
@PostConstruct注解好多人以為是Spring提供的。其實(shí)是Java自己的注解。
Java中該注解的說(shuō)明:@PostConstruct該注解被用來(lái)修飾一個(gè)非靜態(tài)的void()方法。被@PostConstruct修飾的方法會(huì)在服務(wù)器加載Servlet的時(shí)候運(yùn)行,并且只會(huì)被服務(wù)器執(zhí)行一次。PostConstruct在構(gòu)造函數(shù)之后執(zhí)行,init()方法之前執(zhí)行。
通常我們會(huì)是在Spring框架中使用到@PostConstruct注解 該注解的方法在整個(gè)Bean初始化中的執(zhí)行順序:
Constructor(構(gòu)造方法) -> @Autowired(依賴注入) -> @PostConstruct(注釋的方法)
應(yīng)用:在靜態(tài)方法中調(diào)用依賴注入的Bean中的方法。
package com.example.studySpringBoot.util;
import com.example.studySpringBoot.service.MyMethorClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class MyUtils {
private static MyUtils staticInstance = new MyUtils();
@Autowired
private MyMethorClassService myService;
@PostConstruct
public void init(){
staticInstance.myService = myService;
}
public static Integer invokeBean(){
return staticInstance.myService.add(10,20);
}
}那么Java提供的@PostConstruct注解,Spring是如何實(shí)現(xiàn)的呢?
需要先學(xué)習(xí)下BeanPostProcessor這個(gè)接口:
public interface BeanPostProcessor {
/**
* Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean
* initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
* or a custom init-method). The bean will already be populated with property values.
* The returned bean instance may be a wrapper around the original.
*
* 任何Bean實(shí)例化,并且Bean已經(jīng)populated(填充屬性) 就會(huì)回調(diào)這個(gè)方法
*
*/
Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
/**
* Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean
* initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
* or a custom init-method). The bean will already be populated with property values.
* The returned bean instance may be a wrapper around the original.
*
* 任何Bean實(shí)例化,并且Bean已經(jīng)populated(填充屬性) 就會(huì)回調(diào)這個(gè)方法
*
*/
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;那Spring初始化是那里回調(diào)這些方法呢?
AbstractApplicationContext.finishBeanFactoryInitialization(...);
beanFactory.preInstantiateSingletons();
DefaultListableBeanFactory.getBean(beanName);
AbstractBeanFactory.doGetBean();
AbstractAutowireCapableBeanFactory.createBean(....)
populateBean(beanName, mbd, instanceWrapper);
initializeBean(...)
//調(diào)用BeanPostProcessor.postProcessBeforeInitialization()方法
applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
//調(diào)用BeanPostProcessor.postProcessBeforeInitialization()方法
applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);BeanPostProcessor有個(gè)實(shí)現(xiàn)類CommonAnnotationBeanPostProcessor,就是專門處理@PostConstruct @PreDestroy注解。
CommonAnnotationBeanPostProcessor的父類InitDestroyAnnotationBeanPostProcessor()
InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization()
InitDestroyAnnotationBeanPostProcessor.findLifecycleMetadata()
// 組裝生命周期元數(shù)據(jù)
InitDestroyAnnotationBeanPostProcessor.buildLifecycleMetadata()
// 查找@PostConstruct注釋的方法
InitDestroyAnnotationBeanPostProcessor.initAnnotationType
// 查找@PreDestroy注釋方法
InitDestroyAnnotationBeanPostProcessor.destroyAnnotationType
// 反射調(diào)用
metadata.invokeInitMethods(bean, beanName); 到此這篇關(guān)于Java中的@PostConstruct注解的文章就介紹到這了,更多相關(guān)Java @PostConstruct注解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Shiro整合Springboot和redis,jwt過(guò)程中的錯(cuò)誤shiroFilterChainDefinition問(wèn)
這篇文章主要介紹了Shiro整合Springboot和redis,jwt過(guò)程中的錯(cuò)誤shiroFilterChainDefinition問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
從0開(kāi)始教你開(kāi)發(fā)一個(gè)springboot應(yīng)用
這篇文章主要為大家介紹了從0開(kāi)始開(kāi)發(fā)一個(gè)springboot應(yīng)用教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
SpringBoot JavaMailSender發(fā)送郵件功能
這篇文章主要為大家詳細(xì)介紹了SpringBoot JavaMailSender發(fā)送郵件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
Java微信公眾平臺(tái)開(kāi)發(fā)(10) 微信自定義菜單的創(chuàng)建實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Java微信公眾平臺(tái)開(kāi)發(fā)第十步,微信自定義菜單的創(chuàng)建實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Idea 配置國(guó)內(nèi) Maven 源的圖文教程
這篇文章主要介紹了Idea 配置國(guó)內(nèi) Maven 源的教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-11-11
淺談Spring Data如何簡(jiǎn)化數(shù)據(jù)操作的方法
這篇文章主要介紹了看Spring Data如何簡(jiǎn)化數(shù)據(jù)操作的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04
淺談springioc實(shí)例化bean的三個(gè)方法
下面小編就為大家?guī)?lái)一篇淺談springioc實(shí)例化bean的三個(gè)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就想給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09

