List of usage examples for org.springframework.beans.factory FactoryBean isSingleton
default boolean isSingleton()
From source file:org.sakaiproject.util.NoisierDefaultListableBeanFactory.java
public void preInstantiateSingletons() throws BeansException { if (logger.isDebugEnabled()) { logger.debug("Pre-instantiating singletons in factory [" + this + "]"); }//from w ww. jav a 2 s . c om // The superclass's variable by this name is declared private. String[] beanDefinitionNames = getBeanDefinitionNames(); String beanName = null; // Remember in case of an exception try { // for (Iterator it = this.beanDefinitionNames.iterator(); it.hasNext();) { for (int i = 0; i < beanDefinitionNames.length; i++) { beanName = beanDefinitionNames[i]; if (!containsSingleton(beanName) && containsBeanDefinition(beanName)) { RootBeanDefinition bd = getMergedBeanDefinition(beanName, false); if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) { if (bd.hasBeanClass() && FactoryBean.class.isAssignableFrom(bd.getBeanClass())) { FactoryBean factory = (FactoryBean) getBean(FACTORY_BEAN_PREFIX + beanName); if (factory.isSingleton()) { getBean(beanName); } } else { getBean(beanName); } } } } } catch (BeansException ex) { // Destroy already created singletons to avoid dangling resources. logger.error( "Failed to preinstantiate the singleton named " + beanName + ". Destroying all Spring beans.", ex); try { destroySingletons(); } catch (Throwable ex2) { logger.error( "Pre-instantiating singletons failed, " + "and couldn't destroy already created singletons", ex2); } throw ex; } }
From source file:com.otterca.common.crypto.X509CertificateBuilderFactoryTest.java
/** * Test factory bean.//from ww w . j a v a 2 s . co m * * @throws Exception */ @Test public void testFactory() throws Exception { FactoryBean<X509CertificateBuilder> factory = new X509CertificateBuilderFactory(); assertEquals(factory.getObjectType(), X509CertificateBuilder.class); assertFalse(factory.isSingleton()); Object obj = factory.getObject(); assertTrue(obj instanceof X509CertificateBuilder); }
From source file:org.impalaframework.spring.service.SpringServiceBeanUtils.java
/** * Checks that the bean with given name contained in the specified bean factory is a singleton. * Will return true if bean represented by a bean registered under the scope <code>singletone</code>. * If the bean is also a factory bean (see {@link FactoryBean}), then the {@link FactoryBean} * instance also needs to be a singleton. * /*from ww w .j a v a2 s .c o m*/ * Note that in order to work properly the {@link BeanFactory} must be able to recover the {@link BeanDefinition} * for a particular bean name. This in particular must mean implementing {@link BeanDefinitionRegistry} or * {@link BeanDefinitionExposing}. In the latter case, if null is returned, then the bean will be treated as a singleton. * * @return true if bean is singleton registered bean and, if applicable, a singleton {@link FactoryBean}. */ public static boolean isSingleton(BeanFactory beanFactory, String beanName) { Assert.notNull(beanFactory, "beanFactory cannot be null"); Assert.notNull(beanName, "beanName cannot be null"); boolean singleton = true; boolean isBeanFactory = beanFactory.containsBean(BeanFactory.FACTORY_BEAN_PREFIX + beanName); if (isBeanFactory) { FactoryBean<?> factoryBean = (FactoryBean<?>) beanFactory .getBean(BeanFactory.FACTORY_BEAN_PREFIX + beanName); singleton = factoryBean.isSingleton(); } if (singleton) { //ApplicationContext implements this implements this ListableBeanFactory registry = (ListableBeanFactory) beanFactory; //we're only interested in top level definitions //inner beans won't appear here, so boolean containsBeanDefinition = registry.containsBeanDefinition(beanName); if (containsBeanDefinition) { BeanDefinition beanDefinition = getBeanDefinition(registry, beanName); if (beanDefinition != null) { singleton = beanDefinition.isSingleton(); } } else { if (logger.isDebugEnabled()) { logger.debug("Cannot check whether bean definition " + beanName + " is singleton as it is not available as a top level bean"); } } } return singleton; }
From source file:org.springframework.beans.factory.support.AbstractBeanFactory.java
public boolean isSingleton(String name) throws NoSuchBeanDefinitionException { String beanName = transformedBeanName(name); Class beanClass = null;//from w ww . ja v a 2s . c o m boolean singleton = true; Object beanInstance = null; synchronized (this.singletonCache) { beanInstance = this.singletonCache.get(beanName); } if (beanInstance != null) { beanClass = beanInstance.getClass(); singleton = true; } else { // No singleton instance found -> check bean definition. if (getParentBeanFactory() != null && !containsBeanDefinition(beanName)) { // No bean definition found in this factory -> delegate to parent. return getParentBeanFactory().isSingleton(name); } RootBeanDefinition bd = getMergedBeanDefinition(beanName, false); if (bd.hasBeanClass()) { beanClass = bd.getBeanClass(); } singleton = bd.isSingleton(); } // In case of FactoryBean, return singleton status of created object if not a dereference. if (beanClass != null && FactoryBean.class.isAssignableFrom(beanClass) && !isFactoryDereference(name)) { FactoryBean factoryBean = (FactoryBean) getBean(FACTORY_BEAN_PREFIX + beanName); return factoryBean.isSingleton(); } return singleton; }