invokeBeanFactoryPostProcessors(beanFactory);

整体invokeBeanFactoryPostProcessors(beanFactory);的内部实现

// 实例化并调用所有已注册的 BeanFactoryPostProcessor bean,
// 如果给出,则遵守显式顺序。
// 必须在单例实例化之前调用。
protected void 
	invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) 
{
	PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory,
	     getBeanFactoryPostProcessors());
}

invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());

getBeanFactoryPostProcessors();的内部实现如下:

// BeanFactoryPostProcessors to apply on refresh
private final 
    List<BeanFactoryPostProcessor> beanFactoryPostProcessors =
		new ArrayList<BeanFactoryPostProcessor>();
		
// 返回将应用于内部 BeanFactory 的 BeanFactoryPostProcessors 列表。
public List<BeanFactoryPostProcessor> getBeanFactoryPostProcessors() {
	return this.beanFactoryPostProcessors;
}

invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());的内部实现如下:

public static void 
    invokeBeanFactoryPostProcessors(
			ConfigurableListableBeanFactory beanFactory,
			List<BeanFactoryPostProcessor> beanFactoryPostProcessors)
{

	// Invoke BeanDefinitionRegistryPostProcessors first, if any.
	Set<String> processedBeans = new HashSet<String>();

	if (beanFactory instanceof BeanDefinitionRegistry) {
		BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
		List<BeanFactoryPostProcessor> regularPostProcessors = 
		    new LinkedList<BeanFactoryPostProcessor>();
		List<BeanDefinitionRegistryPostProcessor> registryPostProcessors =
				new LinkedList<BeanDefinitionRegistryPostProcessor>();

		for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) 
		{
			if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
				BeanDefinitionRegistryPostProcessor registryPostProcessor =
						(BeanDefinitionRegistryPostProcessor) postProcessor;
				registryPostProcessor.postProcessBeanDefinitionRegistry(registry);
				registryPostProcessors.add(registryPostProcessor);
			}
			else {
				regularPostProcessors.add(postProcessor);
			}
		}

		// 不要在此处初始化 FactoryBeans:我们需要保留所有常规 bean
        // 未初始化以让 bean 工厂后处理器应用于它们!
        // 在实现的 BeanDefinitionRegistryPostProcessors 之间分开
        // PriorityOrdered、Ordered 和其他。
    	String[] postProcessorNames =
			beanFactory.getBeanNamesForType(
				BeanDefinitionRegistryPostProcessor.class, true, false);

		// First, invoke the BeanDefinitionRegistryPostProcessors
		//  that implement PriorityOrdered.
		List<BeanDefinitionRegistryPostProcessor> priorityOrderedPostProcessors = 
		    new ArrayList<BeanDefinitionRegistryPostProcessor>();
		for (String ppName : postProcessorNames) {
			if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
				priorityOrderedPostProcessors.add(beanFactory.getBean(ppName,
				     BeanDefinitionRegistryPostProcessor.class));
				processedBeans.add(ppName);
			}
		}
		OrderComparator.sort(priorityOrderedPostProcessors);
		registryPostProcessors.addAll(priorityOrderedPostProcessors);
		invokeBeanDefinitionRegistryPostProcessors(priorityOrderedPostProcessors,
		     registry);

		// Next, invoke the BeanDefinitionRegistryPostProcessors 
		// that implement Ordered.
		postProcessorNames = beanFactory.getBeanNamesForType(
		    BeanDefinitionRegistryPostProcessor.class, true, false);
		List<BeanDefinitionRegistryPostProcessor> orderedPostProcessors =
		     new ArrayList<BeanDefinitionRegistryPostProcessor>();
		for (String ppName : postProcessorNames) {
			if (!processedBeans.contains(ppName) 
			    && beanFactory.isTypeMatch(ppName, Ordered.class)) 
			{
				orderedPostProcessors.add(beanFactory.getBean(ppName, 
				    BeanDefinitionRegistryPostProcessor.class));
				processedBeans.add(ppName);
			}
		}
		OrderComparator.sort(orderedPostProcessors);
		registryPostProcessors.addAll(orderedPostProcessors);
		invokeBeanDefinitionRegistryPostProcessors(orderedPostProcessors,
		     registry);

		// Finally, invoke all other BeanDefinitionRegistryPostProcessors 
		// until no further ones appear.
		boolean reiterate = true;
		while (reiterate) {
			reiterate = false;
			postProcessorNames = beanFactory.getBeanNamesForType(
			    BeanDefinitionRegistryPostProcessor.class, 
			    true, false);
			for (String ppName : postProcessorNames) {
				if (!processedBeans.contains(ppName)) {
					BeanDefinitionRegistryPostProcessor pp = 
					    beanFactory.getBean(ppName, 
					        BeanDefinitionRegistryPostProcessor.class);
					registryPostProcessors.add(pp);
					processedBeans.add(ppName);
					pp.postProcessBeanDefinitionRegistry(registry);
					reiterate = true;
				}
			}
		}

		// Now, invoke the postProcessBeanFactory callback of all 
		// processors handled so far.
		invokeBeanFactoryPostProcessors(registryPostProcessors, beanFactory);
		invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
	}

	else {
		// Invoke factory processors registered with the context instance.
		invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
	}

	// Do not initialize FactoryBeans here: We need to leave all regular beans
	// uninitialized to let the bean factory post-processors apply to them!
	String[] postProcessorNames =
			beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, 
			    true, false);

	// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
	// Ordered, and the rest.
	List<BeanFactoryPostProcessor> priorityOrderedPostProcessors =
	     new ArrayList<BeanFactoryPostProcessor>();
	List<String> orderedPostProcessorNames = new ArrayList<String>();
	List<String> nonOrderedPostProcessorNames = new ArrayList<String>();
	for (String ppName : postProcessorNames) {
		if (processedBeans.contains(ppName)) {
			// skip - already processed in first phase above
		}
		else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
			priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, 
			    BeanFactoryPostProcessor.class));
		}
		else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
			orderedPostProcessorNames.add(ppName);
		}
		else {
			nonOrderedPostProcessorNames.add(ppName);
		}
	}

	// First, invoke the BeanFactoryPostProcessors that 
	// implement PriorityOrdered.
	OrderComparator.sort(priorityOrderedPostProcessors);
	invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);

	// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
	List<BeanFactoryPostProcessor> orderedPostProcessors = 
	    new ArrayList<BeanFactoryPostProcessor>();
	for (String postProcessorName : orderedPostProcessorNames) {
		orderedPostProcessors.add(beanFactory.getBean(postProcessorName, 
		    BeanFactoryPostProcessor.class));
	}
	OrderComparator.sort(orderedPostProcessors);
	invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);

	// Finally, invoke all other BeanFactoryPostProcessors.
	List<BeanFactoryPostProcessor> nonOrderedPostProcessors = 
	    new ArrayList<BeanFactoryPostProcessor>();
	for (String postProcessorName : nonOrderedPostProcessorNames) {
		nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, 
		    BeanFactoryPostProcessor.class));
	}
	invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
}

beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);

/*
返回与给定类型(包括子类)匹配的 bean 的名称,
从 bean 定义或 FactoryBeans 的情况下的getObjectType值判断。
注意:此方法仅内省顶级 bean。 它不检查嵌套豆可能匹配指定类型为好。
如果设置了“allowEagerInit”标志,则考虑由 FactoryBeans 创建的对象,
这意味着 FactoryBeans 将被初始化。 
如果 FactoryBean 创建的对象不匹配,则原始 FactoryBean 本身将与类型匹配。
如果未设置“allowEagerInit”,则只会检查原始 FactoryBeans(不需要初始化每个 FactoryBean)。
不考虑该工厂可能参与的任何层次结构。
使用 BeanFactoryUtils 的beanNamesForTypeIncludingAncestors也将 bean 包含在祖先工厂中。
注意:不会忽略通过 bean 定义以外的其他方式注册的单例 bean。
此方法返回的bean名称应该始终按照后端配置中定义的顺序返回bean名称,尽可能。

参数:
  type – 要匹配的类或接口,或者所有 bean 名称为null
  includeNonSingletons – 是否也包含原型或作用域 bean 或仅包含单例(也适用于 FactoryBeans)
  allowEagerInit – 是否为类型检查初始化由 FactoryBeans 
               (或由带有“factory-bean”引用的工厂方法)
                创建的惰性初始化单例和对象。 
                请注意,FactoryBeans 需要急切地初始化以确定它们的类型:因此请注意,
                为此标志传入“true”将初始化 FactoryBeans 和“factory-bean”引用。
返回:
  与给定对象类型(包括子类)匹配的 bean(或由 FactoryBeans 创建的对象)的名称,
  如果没有,则为空数组*/

@Override
public String[] getBeanNamesForType(Class<?> type, 
                                    boolean includeNonSingletons,
                                    boolean allowEagerInit) 
{
	if (!isConfigurationFrozen() || type == null || !allowEagerInit) {
		return doGetBeanNamesForType(type, includeNonSingletons, allowEagerInit);
	}
	Map<Class<?>, String[]> cache =
			(includeNonSingletons ? this.allBeanNamesByType 
			    : this.singletonBeanNamesByType);
	String[] resolvedBeanNames = cache.get(type);
	if (resolvedBeanNames != null) {
		return resolvedBeanNames;
	}
	resolvedBeanNames = 
	         doGetBeanNamesForType(type, includeNonSingletons, allowEagerInit);
	if (ClassUtils.isCacheSafe(type, getBeanClassLoader())) {
		cache.put(type, resolvedBeanNames);
	}
	return resolvedBeanNames;
}

isConfigurationFrozen();的内部实现

// 是否可以为所有 bean 缓存 bean 定义元数据
private boolean configurationFrozen = false;

// 返回这个工厂的 bean 定义是否被冻结,即不应该被进一步修改或后处理。
@Override
public boolean isConfigurationFrozen() {
	return this.configurationFrozen;
}

doGetBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit);

private String[] doGetBeanNamesForType(Class<?> type, 
                                       boolean includeNonSingletons,
                                       boolean allowEagerInit) 
{
	List<String> result = new ArrayList<String>();

	// Check all bean definitions.
	String[] beanDefinitionNames = getBeanDefinitionNames();
	for (String beanName : beanDefinitionNames) {
		// Only consider bean as eligible if the bean name
		// is not defined as alias for some other bean.
		if (!isAlias(beanName)) {
			try {
				RootBeanDefinition mbd = 
				   getMergedLocalBeanDefinition(beanName);
				// Only check bean definition if it is complete.
				if (!mbd.isAbstract() 
				     && (
				           allowEagerInit
						   || ((mbd.hasBeanClass() 
						   || !mbd.isLazyInit() 
						   || this.allowEagerClassLoading)
						 )
				     &&  !requiresEagerInitForType(mbd.getFactoryBeanName())))
				{
					// In case of FactoryBean, 
					// match object created by FactoryBean.
					boolean isFactoryBean = 
					    isFactoryBean(beanName, mbd);
					boolean matchFound = 
					  (allowEagerInit||!isFactoryBean||containsSingleton(beanName))			      
					   && (includeNonSingletons || isSingleton(beanName)) 
					   && isTypeMatch(beanName, type);
					if (!matchFound && isFactoryBean) {
						// In case of FactoryBean, 
						// try to match FactoryBean instance itself next.
						beanName = FACTORY_BEAN_PREFIX + beanName;
						matchFound = 
						    (includeNonSingletons || mbd.isSingleton()) 
						    && isTypeMatch(beanName, type);
					}
					if (matchFound) {
						result.add(beanName);
					}
				}
			}
			catch (CannotLoadBeanClassException ex) {
				if (allowEagerInit) {
					throw ex;
				}
				// Probably contains a placeholder:
				//  let's ignore it for type matching purposes.
				if (this.logger.isDebugEnabled()) {
					this.logger.debug(
					    "Ignoring bean class loading failure for bean '" 
					    + beanName + "'", ex);
				}
				onSuppressedException(ex);
			}
			catch (BeanDefinitionStoreException ex) {
				if (allowEagerInit) {
					throw ex;
				}
				// Probably contains a placeholder:
				// let's ignore it for type matching purposes.
				if (this.logger.isDebugEnabled()) {
					this.logger.debug(
					   "Ignoring unresolvable metadata in bean definition '" 
					   + beanName + "'", ex);
				}
				onSuppressedException(ex);
			}
		}
	}

	// Check singletons too, to catch manually registered singletons.
	String[] singletonNames = getSingletonNames();
	for (String beanName : singletonNames) {
		// Only check if manually registered.
		if (!containsBeanDefinition(beanName)) {
			// In case of FactoryBean, 
			// match object created by FactoryBean.
			if (isFactoryBean(beanName)) {
				if ((includeNonSingletons 
				      || isSingleton(beanName)) 
				      && isTypeMatch(beanName, type)) 
				{
					result.add(beanName);
					// Match found for this bean: 
					// do not match FactoryBean itself anymore.
					continue;
				}
				// In case of FactoryBean, 
				// try to match FactoryBean itself next.
				beanName = FACTORY_BEAN_PREFIX + beanName;
			}
			// Match raw bean instance 
			// (might be raw FactoryBean).
			if (isTypeMatch(beanName, type)) {
				result.add(beanName);
			}
		}
	}

	return StringUtils.toStringArray(result);
}
getBeanDefinitionNames();
@Override
public String[] getBeanDefinitionNames() {
	synchronized (this.beanDefinitionMap) {
		if (this.frozenBeanDefinitionNames != null) {
			return this.frozenBeanDefinitionNames;
		}
		else {
			return StringUtils.toStringArray(this.beanDefinitionNames);
		}
	}
}
getSingletonNames();
// Cache of singleton objects: bean name --> bean instance
private final Map<String, Object> singletonObjects = 
    new ConcurrentHashMap<<String, Object>(64);
    
@Override
public String[] getSingletonNames() {
	synchronized (this.singletonObjects) {
		return StringUtils.toStringArray(this.registeredSingletons);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值