当前位置: 首页>>代码示例>>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;未经允许,请勿转载。