1、BeanFactoryPostProcessor這個是spring容器的拓展之一,其目的是在容器初始化完成以前,經過beanFactory對上下文進行進行操做。spring
2、經常使用場景,須要對beanDefinition作處理,提供獲取bean的方式。基本上涉及到的容器的加載修改,均可以操做。ide
3、實現方式post
public class TestBeanFactoryPostProcessor implements BeanFactoryPostProcessor { //這裏是要求必須實現的,也是咱們操做的地方 @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException { } }
說明:這裏的ConfigurableListableBeanFactory 也就是咱們初始化中用到的DefaultListableBeanFactoryui
4、源碼實現this
1)在容器初始化的refresh中實現的方式是spa
//beanFactory的後置操做 this.invokeBeanFactoryPostProcessors(beanFactory);
2)invokeBeanFactoryPostProcessorsdebug
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) { Set<String> processedBeans = new HashSet(); Iterator var9; ArrayList orderedPostProcessors; //這裏判斷是不是DefaultListableBeanFactory的接口 if (beanFactory instanceof BeanDefinitionRegistry) { BeanDefinitionRegistry registry = (BeanDefinitionRegistry)beanFactory; List<BeanFactoryPostProcessor> regularPostProcessors = new LinkedList(); List<BeanDefinitionRegistryPostProcessor> registryPostProcessors = new LinkedList(); //獲取初始化是否就存在BeanFactoryPostProcessor的類型 Iterator var7 = this.getBeanFactoryPostProcessors().iterator(); BeanDefinitionRegistryPostProcessor postProcessor; while(var7.hasNext()) { BeanFactoryPostProcessor postProcessor = (BeanFactoryPostProcessor)var7.next(); //若是是BeanDefinitionRegistryPostProcessor註冊類型的調用postProcessBeanDefinitionRegistry if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) { postProcessor = (BeanDefinitionRegistryPostProcessor)postProcessor; //主要是註冊bean的方式,這個是BeanFactoryPostProcessor的子接口 postProcessor.postProcessBeanDefinitionRegistry(registry); registryPostProcessors.add(postProcessor); } else { //不是BeanDefinitionRegistryPostProcessor類型的直接放入regularPostProcessors regularPostProcessors.add(postProcessor); } } //這裏經過getBeansOfType方式加載繼承BeanDefinitionRegistryPostProcessor方式的全部bean Map<String, BeanDefinitionRegistryPostProcessor> beanMap = beanFactory.getBeansOfType(BeanDefinitionRegistryPostProcessor.class, true, false); orderedPostProcessors = new ArrayList(beanMap.values()); //根據@Order進行排序 OrderComparator.sort(orderedPostProcessors); var9 = orderedPostProcessors.iterator(); while(var9.hasNext()) { postProcessor = (BeanDefinitionRegistryPostProcessor)var9.next(); //執行相關操做 postProcessor.postProcessBeanDefinitionRegistry(registry); } //這裏執行對應的後置beanFactory後置操做 this.invokeBeanFactoryPostProcessors(registryPostProcessors, beanFactory); this.invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory); this.invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory); //添加BeanDefinitionRegistryPostProcessor的bean processedBeans.addAll((Collection)beanMap.keySet()); } else { //若是不是DefaultListableBeanFactory(固然還有其餘類型) this.invokeBeanFactoryPostProcessors(this.getBeanFactoryPostProcessors(), beanFactory); } //這裏纔是重點,主要是實現BeanFactoryPostProcessor的接口bean String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false); List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList(); //這裏分兩種,一中是有序的一中是無序的 List<String> orderedPostProcessorNames = new ArrayList(); List<String> nonOrderedPostProcessorNames = new ArrayList(); String[] var10 = postProcessorNames; int var19 = postProcessorNames.length; for(int var18 = 0; var18 < var19; ++var18) { String ppName = var10[var18]; if (!processedBeans.contains(ppName)) { //匹配對應排序 if (this.isTypeMatch(ppName, PriorityOrdered.class)) { priorityOrderedPostProcessors.add((BeanFactoryPostProcessor)beanFactory.getBean(ppName, BeanFactoryPostProcessor.class)); } else if (this.isTypeMatch(ppName, Ordered.class)) { orderedPostProcessorNames.add(ppName); } else { //通常使用無序的操做 nonOrderedPostProcessorNames.add(ppName); } } } //有序的操做 OrderComparator.sort(priorityOrderedPostProcessors); this.invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory); orderedPostProcessors = new ArrayList(); var9 = orderedPostProcessorNames.iterator(); while(var9.hasNext()) { String postProcessorName = (String)var9.next(); orderedPostProcessors.add((BeanFactoryPostProcessor)this.getBean(postProcessorName, BeanFactoryPostProcessor.class)); } OrderComparator.sort(orderedPostProcessors); this.invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory); //無序的BeanFactoryPostProcessor操做 List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList(); Iterator var23 = nonOrderedPostProcessorNames.iterator(); while(var23.hasNext()) { String postProcessorName = (String)var23.next(); nonOrderedPostProcessors.add((BeanFactoryPostProcessor)this.getBean(postProcessorName, BeanFactoryPostProcessor.class)); } this.invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory); } //執行BeanFactoryPostProcessor結構中的postProcessBeanFactory方法 private void invokeBeanFactoryPostProcessors(Collection<? extends BeanFactoryPostProcessor> postProcessors, ConfigurableListableBeanFactory beanFactory) { Iterator var4 = postProcessors.iterator(); while(var4.hasNext()) { BeanFactoryPostProcessor postProcessor = (BeanFactoryPostProcessor)var4.next(); //實現BeanFactoryPostProcessor的方法能夠經過beanFactory進行操做 postProcessor.postProcessBeanFactory(beanFactory); } }
3)getBeanNamesForTypecode
public String[] getBeanNamesForType(Class type, boolean includeNonSingletons, boolean allowEagerInit) { List<String> result = new ArrayList(); //獲取beanName String[] beanDefinitionNames = this.getBeanDefinitionNames(); String[] var9 = beanDefinitionNames; int var8 = beanDefinitionNames.length; for(int var7 = 0; var7 < var8; ++var7) { String beanName = var9[var7]; //檢查是不是別名 if (!this.isAlias(beanName)) { try { //獲取BeanDefinition RootBeanDefinition mbd = this.getMergedLocalBeanDefinition(beanName); if (!mbd.isAbstract() && (allowEagerInit || (mbd.hasBeanClass() || !mbd.isLazyInit() || this.allowEagerClassLoading) && !this.requiresEagerInitForType(mbd.getFactoryBeanName()))) { boolean isFactoryBean = this.isFactoryBean(beanName, mbd); boolean matchFound = (allowEagerInit || !isFactoryBean || this.containsSingleton(beanName)) && (includeNonSingletons || this.isSingleton(beanName)) && this.isTypeMatch(beanName, type); if (!matchFound && isFactoryBean) { beanName = "&" + beanName; //查看是否匹配 matchFound = (includeNonSingletons || mbd.isSingleton()) && this.isTypeMatch(beanName, type); } if (matchFound) { result.add(beanName); } } } catch (CannotLoadBeanClassException var13) { if (allowEagerInit) { throw var13; } if (this.logger.isDebugEnabled()) { this.logger.debug("Ignoring bean class loading failure for bean '" + beanName + "'", var13); } this.onSuppressedException(var13); } catch (BeanDefinitionStoreException var14) { if (allowEagerInit) { throw var14; } if (this.logger.isDebugEnabled()) { this.logger.debug("Ignoring unresolvable metadata in bean definition '" + beanName + "'", var14); } this.onSuppressedException(var14); } } } //查看單例中是否存在 String[] singletonNames = this.getSingletonNames(); String[] var18 = singletonNames; int var17 = singletonNames.length; for(var8 = 0; var8 < var17; ++var8) { String beanName = var18[var8]; if (!this.containsBeanDefinition(beanName)) { if (this.isFactoryBean(beanName)) { //查看是否匹配 if ((includeNonSingletons || this.isSingleton(beanName)) && this.isTypeMatch(beanName, type)) { result.add(beanName); continue; } beanName = "&" + beanName; } if (this.isTypeMatch(beanName, type)) { result.add(beanName); } } } return StringUtils.toStringArray(result); } public String[] getBeanDefinitionNames() { Map var1 = this.beanDefinitionMap; //這裏的鎖,主要是方式新的bean加入進來 synchronized(this.beanDefinitionMap) { return this.frozenBeanDefinitionNames != null ? this.frozenBeanDefinitionNames : StringUtils.toStringArray(this.beanDefinitionNames); } } public boolean isTypeMatch(String name, Class targetType) throws NoSuchBeanDefinitionException { String beanName = this.transformedBeanName(name); Class typeToMatch = targetType != null ? targetType : Object.class; //在單例中獲取 Object beanInstance = this.getSingleton(beanName, false); if (beanInstance != null) { if (beanInstance instanceof FactoryBean) { if (!BeanFactoryUtils.isFactoryDereference(name)) { Class type = this.getTypeForFactoryBean((FactoryBean)beanInstance); return type != null && typeToMatch.isAssignableFrom(type); } else { return typeToMatch.isAssignableFrom(beanInstance.getClass()); } } else { return !BeanFactoryUtils.isFactoryDereference(name) && typeToMatch.isAssignableFrom(beanInstance.getClass()); } } else if (this.containsSingleton(beanName) && !this.containsBeanDefinition(beanName)) { return false; } else { BeanFactory parentBeanFactory = this.getParentBeanFactory(); if (parentBeanFactory != null && !this.containsBeanDefinition(beanName)) { return parentBeanFactory.isTypeMatch(this.originalBeanName(name), targetType); } else { //經過BeanDefinitionHolder方式斷定 RootBeanDefinition mbd = this.getMergedLocalBeanDefinition(beanName); BeanDefinitionHolder dbd = mbd.getDecoratedDefinition(); Class type; if (dbd != null && !BeanFactoryUtils.isFactoryDereference(name)) { RootBeanDefinition tbd = this.getMergedBeanDefinition(dbd.getBeanName(), dbd.getBeanDefinition(), mbd); type = this.predictBeanType(dbd.getBeanName(), tbd, FactoryBean.class, typeToMatch); if (type != null && !FactoryBean.class.isAssignableFrom(type)) { //判斷是否匹配 return typeToMatch.isAssignableFrom(type); } } Class beanClass = this.predictBeanType(beanName, mbd, FactoryBean.class, typeToMatch); if (beanClass == null) { return false; } else if (FactoryBean.class.isAssignableFrom(beanClass)) { if (!BeanFactoryUtils.isFactoryDereference(name)) { //經過bean類型進行判斷 type = this.getTypeForFactoryBean(beanName, mbd); return type != null && typeToMatch.isAssignableFrom(type); } else { return typeToMatch.isAssignableFrom(beanClass); } } else { return !BeanFactoryUtils.isFactoryDereference(name) && typeToMatch.isAssignableFrom(beanClass); } } } }
5、說明(源碼部分解說,比較簡單。主要是實現過程不復雜,主要是其中的操做,太多了)orm
一、BeanFactoryPostProcessor的後置beanFactroy處理主要是在refresh的this.invokeBeanFactoryPostProcessors(beanFactory);上面完成的。xml
二、在invokeBeanFactoryPostProcessors中是處理的BeanFactoryPostProcessor的子接口BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry方法。
三、在BeanFactoryPostProcessor的使用過程當中使用了2中排序方式PriorityOrder和Order的方式
四、經過beanFactory的getBeanNamesForType方法,匹配實現BeanFactoryPostProcessor的bean.
五、在getBeanNamesForType中isTypeMatch是主要匹配類型的關鍵。
六、整體來講就是xml解析完成事後,進行的後置beanFactory的處理。