1、BeanPostProcessor這個是spring容器的拓展之一,是用於獲取bean的時候處理對應的對象;html
2、經常使用場景,在獲取bean的時候,從新初始化bean的屬性等。spring
3、實現方式(加入容器後,調用其餘bean的時候,經過BeanPostProcessor來進行處理)緩存
import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class BeanPostProcessorTest implements BeanPostProcessor{ /** * 在bean加載以前處理 * @param bean * @param beanName * @return * @throws BeansException */ @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("before"); return bean; } /** * 在bean加載以後處理 * @param bean * @param beanName * @return * @throws BeansException */ @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("after"); return bean; } }
4、源碼部分ide
1)容器中的註冊部分爲refresh()中的registerBeanPostProcessors()方法,注意這裏只是註冊,實際調用在getBean的時候post
this.registerBeanPostProcessors(beanFactory);
2)registerBeanPostProcessorsthis
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) { //在容器中獲取獲取實現BeanPostProcessor的bean String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false); int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length; beanFactory.addBeanPostProcessor(new AbstractApplicationContext.BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount)); //聲明不一樣BeanPostProcessor類型(priorityOrdered,internal,ordered,non 按照順序,和優先級執行) List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList(); List<BeanPostProcessor> internalPostProcessors = new ArrayList(); List<String> orderedPostProcessorNames = new ArrayList(); List<String> nonOrderedPostProcessorNames = new ArrayList(); String[] var11 = postProcessorNames; int var10 = postProcessorNames.length; BeanPostProcessor pp; for(int var9 = 0; var9 < var10; ++var9) { String ppName = var11[var9]; //判斷時什麼類型 if (this.isTypeMatch(ppName, PriorityOrdered.class)) { pp = (BeanPostProcessor)beanFactory.getBean(ppName, BeanPostProcessor.class); priorityOrderedPostProcessors.add(pp); //此接口存在postProcessMergedBeanDefinition的接口 if (pp instanceof MergedBeanDefinitionPostProcessor) { internalPostProcessors.add(pp); } } else if (this.isTypeMatch(ppName, Ordered.class)) { orderedPostProcessorNames.add(ppName); } else { nonOrderedPostProcessorNames.add(ppName); } } //排序 OrderComparator.sort(priorityOrderedPostProcessors); //添加到beanPostProcessors(private final List<BeanPostProcessor> beanPostProcessors = new ArrayList();) this.registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors); List<BeanPostProcessor> orderedPostProcessors = new ArrayList(); Iterator var16 = orderedPostProcessorNames.iterator(); while(var16.hasNext()) { String ppName = (String)var16.next(); BeanPostProcessor pp = (BeanPostProcessor)beanFactory.getBean(ppName, BeanPostProcessor.class); orderedPostProcessors.add(pp); if (pp instanceof MergedBeanDefinitionPostProcessor) { internalPostProcessors.add(pp); } } //排序 OrderComparator.sort(orderedPostProcessors); this.registerBeanPostProcessors(beanFactory, orderedPostProcessors); List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList(); Iterator var19 = nonOrderedPostProcessorNames.iterator(); while(var19.hasNext()) { String ppName = (String)var19.next(); pp = (BeanPostProcessor)beanFactory.getBean(ppName, BeanPostProcessor.class); nonOrderedPostProcessors.add(pp); if (pp instanceof MergedBeanDefinitionPostProcessor) { internalPostProcessors.add(pp); } } this.registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors); OrderComparator.sort(internalPostProcessors); this.registerBeanPostProcessors(beanFactory, internalPostProcessors); beanFactory.addBeanPostProcessor(new AbstractApplicationContext.ApplicationListenerDetector((AbstractApplicationContext.ApplicationListenerDetector)null)); } private void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory, List<BeanPostProcessor> postProcessors) { Iterator var4 = postProcessors.iterator(); while(var4.hasNext()) { BeanPostProcessor postProcessor = (BeanPostProcessor)var4.next(); //添加到beanPostProcessors(private final List<BeanPostProcessor> beanPostProcessors = new ArrayList();) beanFactory.addBeanPostProcessor(postProcessor); } }
3)addBeanPostProcessor(DefaultListableBeanFactory,中緩存的List<String> beanDefinitionNames = new ArrayList())url
public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) { Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null"); this.beanPostProcessors.remove(beanPostProcessor); //添加到beanPostProcessors中 this.beanPostProcessors.add(beanPostProcessor); if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) { this.hasInstantiationAwareBeanPostProcessors = true; } if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) { this.hasDestructionAwareBeanPostProcessors = true; } }
5、調用方式(在個getBean的時候回自動按照優先級調用BeanPostProcessor)spa
具體實現見:spring源碼-bean之加載-2的4、源碼實現3).d createBean部分code