當前位置: 首頁>>代碼示例>>Java>>正文


Java FactoryBean類代碼示例

本文整理匯總了Java中org.springframework.beans.factory.FactoryBean的典型用法代碼示例。如果您正苦於以下問題:Java FactoryBean類的具體用法?Java FactoryBean怎麽用?Java FactoryBean使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


FactoryBean類屬於org.springframework.beans.factory包,在下文中一共展示了FactoryBean類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getType

import org.springframework.beans.factory.FactoryBean; //導入依賴的package包/類
@Override
public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
	String beanName = BeanFactoryUtils.transformedBeanName(name);

	Object bean = this.beans.get(beanName);
	if (bean == null) {
		throw new NoSuchBeanDefinitionException(beanName,
				"Defined beans are [" + StringUtils.collectionToCommaDelimitedString(this.beans.keySet()) + "]");
	}

	if (bean instanceof FactoryBean && !BeanFactoryUtils.isFactoryDereference(name)) {
		// If it's a FactoryBean, we want to look at what it creates, not the factory class.
		return ((FactoryBean<?>) bean).getObjectType();
	}
	return bean.getClass();
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:17,代碼來源:StaticListableBeanFactory.java

示例2: getBeanNamesForType

import org.springframework.beans.factory.FactoryBean; //導入依賴的package包/類
@Override
public String[] getBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean includeFactoryBeans) {
	boolean isFactoryType = (type != null && FactoryBean.class.isAssignableFrom(type));
	List<String> matches = new ArrayList<String>();
	for (String name : this.beans.keySet()) {
		Object beanInstance = this.beans.get(name);
		if (beanInstance instanceof FactoryBean && !isFactoryType) {
			if (includeFactoryBeans) {
				Class<?> objectType = ((FactoryBean<?>) beanInstance).getObjectType();
				if (objectType != null && (type == null || type.isAssignableFrom(objectType))) {
					matches.add(name);
				}
			}
		}
		else {
			if (type == null || type.isInstance(beanInstance)) {
				matches.add(name);
			}
		}
	}
	return StringUtils.toStringArray(matches);
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:23,代碼來源:StaticListableBeanFactory.java

示例3: getTypeForFactoryBean

import org.springframework.beans.factory.FactoryBean; //導入依賴的package包/類
/**
 * Determine the type for the given FactoryBean.
 * @param factoryBean the FactoryBean instance to check
 * @return the FactoryBean's object type,
 * or {@code null} if the type cannot be determined yet
 */
protected Class<?> getTypeForFactoryBean(final FactoryBean<?> factoryBean) {
	try {
		if (System.getSecurityManager() != null) {
			return AccessController.doPrivileged(new PrivilegedAction<Class<?>>() {
				@Override
				public Class<?> run() {
					return factoryBean.getObjectType();
				}
			}, getAccessControlContext());
		}
		else {
			return factoryBean.getObjectType();
		}
	}
	catch (Throwable ex) {
		// Thrown from the FactoryBean's getObjectType implementation.
		logger.warn("FactoryBean threw exception from getObjectType, despite the contract saying " +
				"that it should return null if the type of its object cannot be determined yet", ex);
		return null;
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:28,代碼來源:FactoryBeanRegistrySupport.java

示例4: isFactoryBean

import org.springframework.beans.factory.FactoryBean; //導入依賴的package包/類
@Override
public boolean isFactoryBean(String name) throws NoSuchBeanDefinitionException {
	String beanName = transformedBeanName(name);

	Object beanInstance = getSingleton(beanName, false);
	if (beanInstance != null) {
		return (beanInstance instanceof FactoryBean);
	}
	else if (containsSingleton(beanName)) {
		// null instance registered
		return false;
	}

	// No singleton instance found -> check bean definition.
	if (!containsBeanDefinition(beanName) && getParentBeanFactory() instanceof ConfigurableBeanFactory) {
		// No bean definition found in this factory -> delegate to parent.
		return ((ConfigurableBeanFactory) getParentBeanFactory()).isFactoryBean(name);
	}

	return isFactoryBean(beanName, getMergedLocalBeanDefinition(beanName));
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:22,代碼來源:AbstractBeanFactory.java

示例5: getTypeForFactoryBean

import org.springframework.beans.factory.FactoryBean; //導入依賴的package包/類
/**
 * Determine the bean type for the given FactoryBean definition, as far as possible.
 * Only called if there is no singleton instance registered for the target bean already.
 * <p>The default implementation creates the FactoryBean via {@code getBean}
 * to call its {@code getObjectType} method. Subclasses are encouraged to optimize
 * this, typically by just instantiating the FactoryBean but not populating it yet,
 * trying whether its {@code getObjectType} method already returns a type.
 * If no type found, a full FactoryBean creation as performed by this implementation
 * should be used as fallback.
 * @param beanName the name of the bean
 * @param mbd the merged bean definition for the bean
 * @return the type for the bean if determinable, or {@code null} else
 * @see org.springframework.beans.factory.FactoryBean#getObjectType()
 * @see #getBean(String)
 */
protected Class<?> getTypeForFactoryBean(String beanName, RootBeanDefinition mbd) {
	if (!mbd.isSingleton()) {
		return null;
	}
	try {
		FactoryBean<?> factoryBean = doGetBean(FACTORY_BEAN_PREFIX + beanName, FactoryBean.class, null, true);
		return getTypeForFactoryBean(factoryBean);
	}
	catch (BeanCreationException ex) {
		// Can only happen when getting a FactoryBean.
		if (logger.isDebugEnabled()) {
			logger.debug("Ignoring bean creation exception on FactoryBean type check: " + ex);
		}
		onSuppressedException(ex);
		return null;
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:33,代碼來源:AbstractBeanFactory.java

示例6: predictBeanType

import org.springframework.beans.factory.FactoryBean; //導入依賴的package包/類
@Override
protected Class<?> predictBeanType(String beanName, RootBeanDefinition mbd, Class<?>... typesToMatch) {
	Class<?> targetType = mbd.getTargetType();
	if (targetType == null) {
		targetType = (mbd.getFactoryMethodName() != null ? getTypeForFactoryMethod(beanName, mbd, typesToMatch) :
				resolveBeanClass(mbd, beanName, typesToMatch));
		if (ObjectUtils.isEmpty(typesToMatch) || getTempClassLoader() == null) {
			mbd.setTargetType(targetType);
		}
	}
	// Apply SmartInstantiationAwareBeanPostProcessors to predict the
	// eventual type after a before-instantiation shortcut.
	if (targetType != null && !mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
		for (BeanPostProcessor bp : getBeanPostProcessors()) {
			if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {
				SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp;
				Class<?> predicted = ibp.predictBeanType(targetType, beanName);
				if (predicted != null && (typesToMatch.length != 1 || !FactoryBean.class.equals(typesToMatch[0]) ||
						FactoryBean.class.isAssignableFrom(predicted))) {
					return predicted;
				}
			}
		}
	}
	return targetType;
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:27,代碼來源:AbstractAutowireCapableBeanFactory.java

示例7: getNonSingletonFactoryBeanForTypeCheck

import org.springframework.beans.factory.FactoryBean; //導入依賴的package包/類
/**
 * Obtain a "shortcut" non-singleton FactoryBean instance to use for a
 * {@code getObjectType()} call, without full initialization
 * of the FactoryBean.
 * @param beanName the name of the bean
 * @param mbd the bean definition for the bean
 * @return the FactoryBean instance, or {@code null} to indicate
 * that we couldn't obtain a shortcut FactoryBean instance
 */
private FactoryBean<?> getNonSingletonFactoryBeanForTypeCheck(String beanName, RootBeanDefinition mbd) {
	if (isPrototypeCurrentlyInCreation(beanName)) {
		return null;
	}
	Object instance = null;
	try {
		// Mark this bean as currently in creation, even if just partially.
		beforePrototypeCreation(beanName);
		// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
		instance = resolveBeforeInstantiation(beanName, mbd);
		if (instance == null) {
			BeanWrapper bw = createBeanInstance(beanName, mbd, null);
			instance = bw.getWrappedInstance();
		}
	}
	finally {
		// Finished partial creation of this bean.
		afterPrototypeCreation(beanName);
	}
	return getFactoryBean(beanName, instance);
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:31,代碼來源:AbstractAutowireCapableBeanFactory.java

示例8: contextMatch

import org.springframework.beans.factory.FactoryBean; //導入依賴的package包/類
private FuzzyBoolean contextMatch(Class<?> targetType) {
	String advisedBeanName = getCurrentProxiedBeanName();
	if (advisedBeanName == null) {  // no proxy creation in progress
		// abstain; can't return YES, since that will make pointcut with negation fail
		return FuzzyBoolean.MAYBE;
	}
	if (BeanFactoryUtils.isGeneratedBeanName(advisedBeanName)) {
		return FuzzyBoolean.NO;
	}
	if (targetType != null) {
		boolean isFactory = FactoryBean.class.isAssignableFrom(targetType);
		return FuzzyBoolean.fromBoolean(
				matchesBeanName(isFactory ? BeanFactory.FACTORY_BEAN_PREFIX + advisedBeanName : advisedBeanName));
	}
	else {
		return FuzzyBoolean.fromBoolean(matchesBeanName(advisedBeanName) ||
				matchesBeanName(BeanFactory.FACTORY_BEAN_PREFIX + advisedBeanName));
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:20,代碼來源:AspectJExpressionPointcut.java

示例9: entityManagerFactory

import org.springframework.beans.factory.FactoryBean; //導入依賴的package包/類
@Bean
public FactoryBean<EntityManagerFactory> entityManagerFactory() {
	LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
	emf.setDataSource(dataSource());
	emf.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
	emf.setPackagesToScan(TestJpaDomain.class.getPackage().getName());
	emf.setPersistenceUnitName("test");
	return emf;
}
 
開發者ID:holon-platform,項目名稱:holon-datastore-jpa,代碼行數:10,代碼來源:TestBase.java

示例10: should_resolve_factory_bean_definition

import org.springframework.beans.factory.FactoryBean; //導入依賴的package包/類
@Test
public void should_resolve_factory_bean_definition() {
	//given
	final String factoryBeanName = "factoryBean";
	final DoubleRegistry doubleRegistry = new DoubleRegistry(
			emptyList(),
			emptyList());
	final ApplicationContext appCtx = buildAppContext(Stream.of(
			bean(factoryBeanName, new AnyFactoryBean())
	));
	final BeanDefinitionFinder definitionFinder = new BeanDefinitionFinder(appCtx, doubleRegistry);

	//when
	Optional<BeanDefinition> definition = definitionFinder.tryToFindBeanDefinition(FACTORY_BEAN_PREFIX + factoryBeanName, FactoryBean.class);

	//then
	assertTrue(definition.isPresent());
}
 
開發者ID:pchudzik,項目名稱:springmock,代碼行數:19,代碼來源:BeanDefinitionFinderTest.java

示例11: notMockFactoryBean

import org.springframework.beans.factory.FactoryBean; //導入依賴的package包/類
@Bean(MOCK)
FactoryBean<Service> notMockFactoryBean() {
	return new FactoryBean<Service>() {
		@Override
		public Service getObject() throws Exception {
			return notMock;
		}

		@Override
		public Class<?> getObjectType() {
			return Service.class;
		}

		@Override
		public boolean isSingleton() {
			return true;
		}
	};
}
 
開發者ID:pchudzik,項目名稱:springmock,代碼行數:20,代碼來源:ShouldReplaceFactoryBeanWithDoubles.java

示例12: notSpyFactoryBean

import org.springframework.beans.factory.FactoryBean; //導入依賴的package包/類
@Bean(SPY)
FactoryBean<Service> notSpyFactoryBean() {
	return new FactoryBean<Service>() {
		@Override
		public Service getObject() throws Exception {
			return notSpy;
		}

		@Override
		public Class<?> getObjectType() {
			return Service.class;
		}

		@Override
		public boolean isSingleton() {
			return true;
		}
	};
}
 
開發者ID:pchudzik,項目名稱:springmock,代碼行數:20,代碼來源:ShouldReplaceFactoryBeanWithDoubles.java

示例13: repro

import org.springframework.beans.factory.FactoryBean; //導入依賴的package包/類
@Test
public void repro() {
	AnnotationConfigApplicationContext bf = new AnnotationConfigApplicationContext();
	bf.registerBeanDefinition("fooConfig", new RootBeanDefinition(FooConfig.class));
	bf.getBeanFactory().addBeanPostProcessor(new PredictingBPP());
	bf.refresh();

	assertThat(bf.getBean("foo"), instanceOf(Foo.class));
	assertThat(bf.getBean("&foo"), instanceOf(FooFactoryBean.class));

	assertThat(bf.isTypeMatch("&foo", FactoryBean.class), is(true));

	@SuppressWarnings("rawtypes")
	Map<String, FactoryBean> fbBeans = bf.getBeansOfType(FactoryBean.class);
	assertThat(1, equalTo(fbBeans.size()));
	assertThat("&foo", equalTo(fbBeans.keySet().iterator().next()));

	Map<String, AnInterface> aiBeans = bf.getBeansOfType(AnInterface.class);
	assertThat(1, equalTo(aiBeans.size()));
	assertThat("&foo", equalTo(aiBeans.keySet().iterator().next()));
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:22,代碼來源:ConfigurationClassSpr8954Tests.java

示例14: findsBeansByTypeIfNotInstantiated

import org.springframework.beans.factory.FactoryBean; //導入依賴的package包/類
@Test
public void findsBeansByTypeIfNotInstantiated() {
	AnnotationConfigApplicationContext bf = new AnnotationConfigApplicationContext();
	bf.registerBeanDefinition("fooConfig", new RootBeanDefinition(FooConfig.class));
	bf.getBeanFactory().addBeanPostProcessor(new PredictingBPP());
	bf.refresh();

	assertThat(bf.isTypeMatch("&foo", FactoryBean.class), is(true));

	@SuppressWarnings("rawtypes")
	Map<String, FactoryBean> fbBeans = bf.getBeansOfType(FactoryBean.class);
	assertThat(1, equalTo(fbBeans.size()));
	assertThat("&foo", equalTo(fbBeans.keySet().iterator().next()));

	Map<String, AnInterface> aiBeans = bf.getBeansOfType(AnInterface.class);
	assertThat(1, equalTo(aiBeans.size()));
	assertThat("&foo", equalTo(aiBeans.keySet().iterator().next()));
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:19,代碼來源:ConfigurationClassSpr8954Tests.java

示例15: configWithFactoryBeanReturnType

import org.springframework.beans.factory.FactoryBean; //導入依賴的package包/類
@Test
public void configWithFactoryBeanReturnType() {
	ListableBeanFactory factory = initBeanFactory(ConfigWithNonSpecificReturnTypes.class);
	assertEquals(List.class, factory.getType("factoryBean"));
	assertTrue(factory.isTypeMatch("factoryBean", List.class));
	assertEquals(FactoryBean.class, factory.getType("&factoryBean"));
	assertTrue(factory.isTypeMatch("&factoryBean", FactoryBean.class));
	assertFalse(factory.isTypeMatch("&factoryBean", BeanClassLoaderAware.class));
	assertFalse(factory.isTypeMatch("&factoryBean", ListFactoryBean.class));
	assertTrue(factory.getBean("factoryBean") instanceof List);

	String[] beanNames = factory.getBeanNamesForType(FactoryBean.class);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);

	beanNames = factory.getBeanNamesForType(BeanClassLoaderAware.class);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);

	beanNames = factory.getBeanNamesForType(ListFactoryBean.class);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);

	beanNames = factory.getBeanNamesForType(List.class);
	assertEquals("factoryBean", beanNames[0]);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:27,代碼來源:ConfigurationClassProcessingTests.java


注:本文中的org.springframework.beans.factory.FactoryBean類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。