spring源码-BeanFactoryPostProcessor-3.2
一、BeanFactoryPostProcessor这个是spring容器的拓展之一,其目的是在容器初始化完成之前,通过beanFactory对上下文进行进行操作。
二、常用场景,需要对beanDefinition做处理,提供获取bean的方式。基本上涉及到的容器的加载修改,都可以操作。
三、实现方式
public class TestBeanFactoryPostProcessor implements BeanFactoryPostProcessor { //这里是要求必须实现的,也是我们操作的地方 @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException { } }
说明:这里的ConfigurableListableBeanFactory 也就是我们初始化中用到的DefaultListableBeanFactory
四、源码实现
1)在容器初始化的refresh中实现的方式是
//beanFactory的后置操作 this.invokeBeanFactoryPostProcessors(beanFactory);
2)invokeBeanFactoryPostProcessors
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)getBeanNamesForType
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); } } } }
五、说明(源码部分解说,比较简单。主要是实现过程不复杂,主要是其中的操作,太多了)
1、BeanFactoryPostProcessor的后置beanFactroy处理主要是在refresh的this.invokeBeanFactoryPostProcessors(beanFactory);上面完成的。
2、在invokeBeanFactoryPostProcessors中是处理的BeanFactoryPostProcessor的子接口BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry方法。
3、在BeanFactoryPostProcessor的使用过程中使用了2中排序方式PriorityOrder和Order的方式
4、通过beanFactory的getBeanNamesForType方法,匹配实现BeanFactoryPostProcessor的bean.
5、在getBeanNamesForType中isTypeMatch是主要匹配类型的关键。
6、总体来说就是xml解析完成过后,进行的后置beanFactory的处理。