Search in sources :

Example 1 with ListableBeanFactory

use of org.springframework.beans.factory.ListableBeanFactory in project spring-framework by spring-projects.

the class ConfigurationClassProcessingTests method configWithFactoryBeanReturnType.

@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]);
}
Also used : BeanClassLoaderAware(org.springframework.beans.factory.BeanClassLoaderAware) List(java.util.List) ListFactoryBean(org.springframework.beans.factory.config.ListFactoryBean) FactoryBean(org.springframework.beans.factory.FactoryBean) ListFactoryBean(org.springframework.beans.factory.config.ListFactoryBean) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory) Test(org.junit.Test)

Example 2 with ListableBeanFactory

use of org.springframework.beans.factory.ListableBeanFactory in project spring-framework by spring-projects.

the class JmsListenerAnnotationBeanPostProcessor method afterSingletonsInstantiated.

@Override
public void afterSingletonsInstantiated() {
    // Remove resolved singleton classes from cache
    this.nonAnnotatedClasses.clear();
    if (this.beanFactory instanceof ListableBeanFactory) {
        // Apply JmsListenerConfigurer beans from the BeanFactory, if any
        Map<String, JmsListenerConfigurer> instances = ((ListableBeanFactory) this.beanFactory).getBeansOfType(JmsListenerConfigurer.class);
        for (JmsListenerConfigurer configurer : instances.values()) {
            configurer.configureJmsListeners(this.registrar);
        }
    }
    if (this.registrar.getEndpointRegistry() == null) {
        // Determine JmsListenerEndpointRegistry bean from the BeanFactory
        if (this.endpointRegistry == null) {
            Assert.state(this.beanFactory != null, "BeanFactory must be set to find endpoint registry by bean name");
            this.endpointRegistry = this.beanFactory.getBean(JmsListenerConfigUtils.JMS_LISTENER_ENDPOINT_REGISTRY_BEAN_NAME, JmsListenerEndpointRegistry.class);
        }
        this.registrar.setEndpointRegistry(this.endpointRegistry);
    }
    if (this.containerFactoryBeanName != null) {
        this.registrar.setContainerFactoryBeanName(this.containerFactoryBeanName);
    }
    // Set the custom handler method factory once resolved by the configurer
    MessageHandlerMethodFactory handlerMethodFactory = this.registrar.getMessageHandlerMethodFactory();
    if (handlerMethodFactory != null) {
        this.messageHandlerMethodFactory.setMessageHandlerMethodFactory(handlerMethodFactory);
    }
    // Actually register all listeners
    this.registrar.afterPropertiesSet();
}
Also used : JmsListenerEndpointRegistry(org.springframework.jms.config.JmsListenerEndpointRegistry) DefaultMessageHandlerMethodFactory(org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory) MessageHandlerMethodFactory(org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory) ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory)

Example 3 with ListableBeanFactory

use of org.springframework.beans.factory.ListableBeanFactory in project spring-framework by spring-projects.

the class EntityManagerFactoryAccessor method setBeanFactory.

/**
	 * Retrieves an EntityManagerFactory by persistence unit name, if none set explicitly.
	 * Falls back to a default EntityManagerFactory bean if no persistence unit specified.
	 * @see #setPersistenceUnitName
	 */
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    if (getEntityManagerFactory() == null) {
        if (!(beanFactory instanceof ListableBeanFactory)) {
            throw new IllegalStateException("Cannot retrieve EntityManagerFactory by persistence unit name " + "in a non-listable BeanFactory: " + beanFactory);
        }
        ListableBeanFactory lbf = (ListableBeanFactory) beanFactory;
        setEntityManagerFactory(EntityManagerFactoryUtils.findEntityManagerFactory(lbf, getPersistenceUnitName()));
    }
}
Also used : ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory)

Example 4 with ListableBeanFactory

use of org.springframework.beans.factory.ListableBeanFactory in project spring-framework by spring-projects.

the class JpaTransactionManager method setBeanFactory.

/**
	 * Retrieves an EntityManagerFactory by persistence unit name, if none set explicitly.
	 * Falls back to a default EntityManagerFactory bean if no persistence unit specified.
	 * @see #setPersistenceUnitName
	 */
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    if (getEntityManagerFactory() == null) {
        if (!(beanFactory instanceof ListableBeanFactory)) {
            throw new IllegalStateException("Cannot retrieve EntityManagerFactory by persistence unit name " + "in a non-listable BeanFactory: " + beanFactory);
        }
        ListableBeanFactory lbf = (ListableBeanFactory) beanFactory;
        setEntityManagerFactory(EntityManagerFactoryUtils.findEntityManagerFactory(lbf, getPersistenceUnitName()));
    }
}
Also used : ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory)

Example 5 with ListableBeanFactory

use of org.springframework.beans.factory.ListableBeanFactory in project spring-framework by spring-projects.

the class TestContextTransactionUtils method retrieveDataSource.

/**
	 * Retrieve the {@link DataSource} to use for the supplied {@linkplain TestContext
	 * test context}.
	 * <p>The following algorithm is used to retrieve the {@code DataSource} from
	 * the {@link org.springframework.context.ApplicationContext ApplicationContext}
	 * of the supplied test context:
	 * <ol>
	 * <li>Look up the {@code DataSource} by type and name, if the supplied
	 * {@code name} is non-empty, throwing a {@link BeansException} if the named
	 * {@code DataSource} does not exist.
	 * <li>Attempt to look up the single {@code DataSource} by type.
	 * <li>Attempt to look up the <em>primary</em> {@code DataSource} by type.
	 * <li>Attempt to look up the {@code DataSource} by type and the
	 * {@linkplain #DEFAULT_DATA_SOURCE_NAME default data source name}.
	 * @param testContext the test context for which the {@code DataSource}
	 * should be retrieved; never {@code null}
	 * @param name the name of the {@code DataSource} to retrieve; may be {@code null}
	 * or <em>empty</em>
	 * @return the {@code DataSource} to use, or {@code null} if not found
	 * @throws BeansException if an error occurs while retrieving an explicitly
	 * named {@code DataSource}
	 */
public static DataSource retrieveDataSource(TestContext testContext, String name) {
    Assert.notNull(testContext, "TestContext must not be null");
    BeanFactory bf = testContext.getApplicationContext().getAutowireCapableBeanFactory();
    try {
        // look up by type and explicit name
        if (StringUtils.hasText(name)) {
            return bf.getBean(name, DataSource.class);
        }
    } catch (BeansException ex) {
        logger.error(String.format("Failed to retrieve DataSource named '%s' for test context %s", name, testContext), ex);
        throw ex;
    }
    try {
        if (bf instanceof ListableBeanFactory) {
            ListableBeanFactory lbf = (ListableBeanFactory) bf;
            // look up single bean by type
            Map<String, DataSource> dataSources = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, DataSource.class);
            if (dataSources.size() == 1) {
                return dataSources.values().iterator().next();
            }
            try {
                // look up single bean by type, with support for 'primary' beans
                return bf.getBean(DataSource.class);
            } catch (BeansException ex) {
                logBeansException(testContext, ex, PlatformTransactionManager.class);
            }
        }
        // look up by type and default name
        return bf.getBean(DEFAULT_DATA_SOURCE_NAME, DataSource.class);
    } catch (BeansException ex) {
        logBeansException(testContext, ex, DataSource.class);
        return null;
    }
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory) ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) BeansException(org.springframework.beans.BeansException) DataSource(javax.sql.DataSource)

Aggregations

ListableBeanFactory (org.springframework.beans.factory.ListableBeanFactory)17 BeanFactory (org.springframework.beans.factory.BeanFactory)4 BeansException (org.springframework.beans.BeansException)2 FileSystemXmlApplicationContext (org.springframework.context.support.FileSystemXmlApplicationContext)2 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)2 BasicRefererInterfaceConfig (com.weibo.api.motan.config.BasicRefererInterfaceConfig)1 BasicServiceInterfaceConfig (com.weibo.api.motan.config.BasicServiceInterfaceConfig)1 List (java.util.List)1 Map (java.util.Map)1 DataSource (javax.sql.DataSource)1 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)1 Test (org.junit.Test)1 Scheduler (org.quartz.Scheduler)1 BeanClassLoaderAware (org.springframework.beans.factory.BeanClassLoaderAware)1 FactoryBean (org.springframework.beans.factory.FactoryBean)1 HierarchicalBeanFactory (org.springframework.beans.factory.HierarchicalBeanFactory)1 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)1 ListFactoryBean (org.springframework.beans.factory.config.ListFactoryBean)1 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1 PropertySourcesPlaceholderConfigurer (org.springframework.context.support.PropertySourcesPlaceholderConfigurer)1