當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。