当前位置: 首页>>代码示例>>Java>>正文


Java BeanFactory.isTypeMatch方法代码示例

本文整理汇总了Java中org.springframework.beans.factory.BeanFactory.isTypeMatch方法的典型用法代码示例。如果您正苦于以下问题:Java BeanFactory.isTypeMatch方法的具体用法?Java BeanFactory.isTypeMatch怎么用?Java BeanFactory.isTypeMatch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.springframework.beans.factory.BeanFactory的用法示例。


在下文中一共展示了BeanFactory.isTypeMatch方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getDataSourceBeanName

import org.springframework.beans.factory.BeanFactory; //导入方法依赖的package包/类
/**
 * Get the {@link DataSource} type bean name which corresponds to given data context id
 * @param registry Bean registry
 * @param beanFactory Bean factory
 * @param dataContextId Optional data context id
 * @return The DataSource bean name, or <code>null</code> if not found
 */
private static String getDataSourceBeanName(BeanDefinitionRegistry registry, BeanFactory beanFactory,
		String dataContextId) {
	// check unique DataSource if no data context id specified
	if (dataContextId == null && beanFactory instanceof ListableBeanFactory) {
		String[] dataSourceBeanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
				(ListableBeanFactory) beanFactory, DataSource.class, false, false);
		if (dataSourceBeanNames != null && dataSourceBeanNames.length == 1) {
			return dataSourceBeanNames[0];
		}
	}
	// check bean name using data context id
	String dsBeanName = BeanRegistryUtils.buildBeanName(dataContextId,
			EnableDataSource.DEFAULT_DATASOURCE_BEAN_NAME);
	if (registry.containsBeanDefinition(dsBeanName) && beanFactory.isTypeMatch(dsBeanName, DataSource.class)) {
		return dsBeanName;
	}
	return null;
}
 
开发者ID:holon-platform,项目名称:holon-datastore-jpa,代码行数:26,代码来源:JpaRegistrar.java

示例2: isTypeMatch

import org.springframework.beans.factory.BeanFactory; //导入方法依赖的package包/类
@Override
public boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException {
	String beanName = transformedBeanName(name);
	Class<?> typeToMatch = (targetType != null ? targetType : Object.class);

	// Check manually registered singletons.
	Object beanInstance = getSingleton(beanName, false);
	if (beanInstance != null) {
		if (beanInstance instanceof FactoryBean) {
			if (!BeanFactoryUtils.isFactoryDereference(name)) {
				Class<?> type = getTypeForFactoryBean((FactoryBean<?>) beanInstance);
				return (type != null && ClassUtils.isAssignable(typeToMatch, type));
			}
			else {
				return ClassUtils.isAssignableValue(typeToMatch, beanInstance);
			}
		}
		else {
			return !BeanFactoryUtils.isFactoryDereference(name) &&
					ClassUtils.isAssignableValue(typeToMatch, beanInstance);
		}
	}
	else if (containsSingleton(beanName) && !containsBeanDefinition(beanName)) {
		// null instance registered
		return false;
	}

	else {
		// No singleton instance found -> check bean definition.
		BeanFactory parentBeanFactory = getParentBeanFactory();
		if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
			// No bean definition found in this factory -> delegate to parent.
			return parentBeanFactory.isTypeMatch(originalBeanName(name), targetType);
		}

		// Retrieve corresponding bean definition.
		RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);

		Class<?>[] typesToMatch = (FactoryBean.class.equals(typeToMatch) ?
				new Class<?>[] {typeToMatch} : new Class<?>[] {FactoryBean.class, typeToMatch});

		// Check decorated bean definition, if any: We assume it'll be easier
		// to determine the decorated bean's type than the proxy's type.
		BeanDefinitionHolder dbd = mbd.getDecoratedDefinition();
		if (dbd != null && !BeanFactoryUtils.isFactoryDereference(name)) {
			RootBeanDefinition tbd = getMergedBeanDefinition(dbd.getBeanName(), dbd.getBeanDefinition(), mbd);
			Class<?> targetClass = predictBeanType(dbd.getBeanName(), tbd, typesToMatch);
			if (targetClass != null && !FactoryBean.class.isAssignableFrom(targetClass)) {
				return typeToMatch.isAssignableFrom(targetClass);
			}
		}

		Class<?> beanType = predictBeanType(beanName, mbd, typesToMatch);
		if (beanType == null) {
			return false;
		}

		// Check bean class whether we're dealing with a FactoryBean.
		if (FactoryBean.class.isAssignableFrom(beanType)) {
			if (!BeanFactoryUtils.isFactoryDereference(name)) {
				// If it's a FactoryBean, we want to look at what it creates, not the factory class.
				beanType = getTypeForFactoryBean(beanName, mbd);
				if (beanType == null) {
					return false;
				}
			}
		}
		else if (BeanFactoryUtils.isFactoryDereference(name)) {
			// Special case: A SmartInstantiationAwareBeanPostProcessor returned a non-FactoryBean
			// type but we nevertheless are being asked to dereference a FactoryBean...
			// Let's check the original bean class and proceed with it if it is a FactoryBean.
			beanType = predictBeanType(beanName, mbd, FactoryBean.class);
			if (beanType == null || !FactoryBean.class.isAssignableFrom(beanType)) {
				return false;
			}
		}

		return typeToMatch.isAssignableFrom(beanType);
	}
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:81,代码来源:AbstractBeanFactory.java


注:本文中的org.springframework.beans.factory.BeanFactory.isTypeMatch方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。