国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

淺談Spring中幾個PostProcessor的區別與聯系

瀏覽:4日期:2023-12-01 13:12:09
目錄Spring幾個PostProcessor的區別首先明確 Bean 的生命周期:查看 IOC 容器創建時的調用流程spring-postProcessor的執行時機BeanPostProcessor:postProcessAfterInitialization調用時機:InstantiationAwareBeanPostProcessor總結: 執行順序Spring幾個PostProcessor的區別首先明確 Bean 的生命周期: 首先注冊 Bean 的定義信息; 然后創建 Bean 的實例; 最后初始化 Bean ,放入容器中。

按照執行的順序,可以分為以下幾個步驟:

BeanDefinitionRegistryPostProcessor 是在注冊 Bean 定義信息前后調用;

BeanFactoryPostProcessor 是在創建 Bean 前后調用;

BeanPostProcessor 是在初始化 Bean 前后調用;

其中 BeanDefinitionRegistryPostProcessor 是 BeanFactoryPostProcessor 的子類,所以可以使用前者代替后者實現功能。

查看 IOC 容器創建時的調用流程

refresh 方法的代碼如下:

// Allows post-processing of the bean factory in context subclasses.postProcessBeanFactory(beanFactory);// Invoke factory processors registered as beans in the context.invokeBeanFactoryPostProcessors(beanFactory);// Register bean processors that intercept bean creation.registerBeanPostProcessors(beanFactory);// Initialize message source for this context.initMessageSource();// Initialize event multicaster for this context.initApplicationEventMulticaster();// Initialize other special beans in specific context subclasses.onRefresh();// Check for listener beans and register them.registerListeners();// Instantiate all remaining (non-lazy-init) singletons.finishBeanFactoryInitialization(beanFactory);// Last step: publish corresponding event.finishRefresh();

其中的 invokeBeanFactoryPostProcessors 就執行了注冊定義信息和創建 Bean 的方法;而 finishBeanFactoryInitialization 執行了初始化 Bean 的方法。

具體的執行順序大家可以自行打斷點調試,由于涉及的源碼過多,這里不再展示。

spring-postProcessor的執行時機

spring bean 的生命周期粗糙的分為以下步驟。

實例化(創建一個屬性都為空的對象)---------》屬性填充(populateBean,下文中這個步驟我都稱為初始化)-----------》init方法的執行(invokerInitMethods,下文稱為init)

postprocessor的方法就是穿插在這三個大的步驟中。

BeanPostProcessor:

淺談Spring中幾個PostProcessor的區別與聯系

postProcessBeforeInitialization調用時機

淺談Spring中幾個PostProcessor的區別與聯系

向上找調用者:

淺談Spring中幾個PostProcessor的區別與聯系

繼續向上:

淺談Spring中幾個PostProcessor的區別與聯系

看以看出populateBean(初始化bean)-------------------》beanpostBeforeInitialization---------------------------------->invokeinitMethods(配置的init-method)

postProcessAfterInitialization調用時機:

淺談Spring中幾個PostProcessor的區別與聯系

向上:

淺談Spring中幾個PostProcessor的區別與聯系

可以看出在init-method方法之后

看以看出populateBean(初始化bean)-------------------》beanpostBeforeInitialization---------------------------------->invokeinitMethods(配置的init-method)------->postProcessAfterInitialization

public class MBeanPostProcessor implements BeanPostProcessor { @Override //populateBean之后 invokeinitMethods之前 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println('post bean before! :'+beanName);return bean; } @Override //invokeinitMethods之后 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println('post bean after!'+beanName);return bean; }}

另一個重要的是:

InstantiationAwareBeanPostProcessor

淺談Spring中幾個PostProcessor的區別與聯系

postProcessBeforeInstantiation調用時機:

淺談Spring中幾個PostProcessor的區別與聯系

向上找調用者:

淺談Spring中幾個PostProcessor的區別與聯系

繼續向上:

淺談Spring中幾個PostProcessor的區別與聯系

可以看出是在實例化之前:(也就是反射創建對象之前,如果postProcessBeforeInstantiation創建了一個非空的對象,則不會走實例化步驟。

postProcessAfterInstantiation調用時機:

protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) { PropertyValues pvs = mbd.getPropertyValues(); if (bw == null) { if (!pvs.isEmpty()) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, 'Cannot apply property values to null instance'); } else { // Skip property population phase for null instance. return; } } // Give any InstantiationAwareBeanPostProcessors the opportunity to modify the // state of the bean before properties are set. This can be used, for example, // to support styles of field injection. boolean continueWithPropertyPopulation = true; if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; //在這里執行 if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) { continueWithPropertyPopulation = false; break; } } } } if (!continueWithPropertyPopulation) { return; } 省略。。。。。 applyPropertyValues(beanName, mbd, bw, pvs);}

可以看出是在在初始化之前,具體是屬性填充之前。(初始化之前,實例化之后) 如果返回fales,則不會繼續初始化,即不會屬性填充。

postProcessPropertyValues調用時機:

protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) { PropertyValues pvs = mbd.getPropertyValues(); if (bw == null) { if (!pvs.isEmpty()) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, 'Cannot apply property values to null instance'); } else { // Skip property population phase for null instance. return; } } // Give any InstantiationAwareBeanPostProcessors the opportunity to modify the // state of the bean before properties are set. This can be used, for example, // to support styles of field injection. boolean continueWithPropertyPopulation = true; if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) { continueWithPropertyPopulation = false; break; } } } } if (!continueWithPropertyPopulation) { return; } if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME || mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) { MutablePropertyValues newPvs = new MutablePropertyValues(pvs); // Add property values based on autowire by name if applicable. if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) { autowireByName(beanName, mbd, bw, newPvs); } // Add property values based on autowire by type if applicable. if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) { autowireByType(beanName, mbd, bw, newPvs); } pvs = newPvs; } boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors(); boolean needsDepCheck = (mbd.getDependencyCheck() != RootBeanDefinition.DEPENDENCY_CHECK_NONE); if (hasInstAwareBpps || needsDepCheck) { PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching); if (hasInstAwareBpps) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName); if (pvs == null) { return; } } } } if (needsDepCheck) { checkDependencies(beanName, mbd, filteredPds, pvs); } } applyPropertyValues(beanName, mbd, bw, pvs);}

在postProcessAfterInstantiation之后,applyPropertyValues之前。(屬性填充之前修改屬性值)

總結: 執行順序 1.postProcessBeforeInstantiation(實現這個方法可以做自定義實例化) 2.實例化 3.postProcessAfterInstantiation(是否要初始化) 4.postProcessPropertyValues(修改屬性) 5.初始化(屬性填充)(populateBean) 6.postProcesstBeforeInitialization( 自定義init方法執行之前) 7.invokeinitMethods(執行自定義的init方法) 8.postProcessAfterInitialization(自定義init方法執行之后)

如果加上aware

淺談Spring中幾個PostProcessor的區別與聯系

1.postProcessBeforeInstantiation(實現這個方法可以做自定義實例化) 2.實例化 3.postProcessAfterInstantiation(是否要初始化) 4.postProcessPropertyValues(修改屬性) 5.初始化(屬性填充)(populateBean) 6.postProcesstBeforeInitialization( 自定義init方法執行之前) 7.invokeAwareMethod 8.invokeinitMethods(執行自定義的init方法) 9.postProcessAfterInitialization(自定義init方法執行之后)

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 欧美特黄一片aa大片免费看 | 一区二区三区四区视频在线观看 | 自拍1页 | 经典香港一级a毛片免费看 精品400部自拍视频在线播放 | 亚洲一区二区三区在线 | 日韩理论视频 | 美国全免费特一级毛片 | 亚洲欧洲视频在线 | 日韩一区视频在线 | 欧美二区在线观看 | 在线免费观看国产视频 | 大片刺激免费播放视频 | 欧美成人影院在线观看三级 | 国产美女视频做爰 | 欧美一级欧美一级在线播放 | 特级黄色毛片在放 | 亚洲性生活视频 | 亚洲欧美另类色妞网站 | 亚洲精品国产第一区第二区国 | 九九九九在线精品免费视频 | 国产精品亚洲专一区二区三区 | 一级片在线观看视频 | 精品在线观看视频 | 国产精品手机在线播放 | 男人桶女人逼 | 久久精品国产精品青草不卡 | 精品亚洲视频在线观看 | 日韩高清不卡在线 | 日韩特级| 国产精品久久久久久久久 | 亚洲精品欧美精品一区二区 | 精品久久精品久久 | 亚洲黄色成人 | 欧美另类老妇 | 在线色网址 | 日韩视频一区二区 | 日韩日b视频 | 伊人成人在线视频 | 免费国产成人 | 精品久久久久久久九九九精品 | 国产高清一区二区三区四区 |