Search in sources :

Example 21 with BeanFactory

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

the class AbstractApplicationEventMulticaster method retrieveApplicationListeners.

/**
	 * Actually retrieve the application listeners for the given event and source type.
	 * @param eventType the event type
	 * @param sourceType the event source type
	 * @param retriever the ListenerRetriever, if supposed to populate one (for caching purposes)
	 * @return the pre-filtered list of application listeners for the given event and source type
	 */
private Collection<ApplicationListener<?>> retrieveApplicationListeners(ResolvableType eventType, Class<?> sourceType, ListenerRetriever retriever) {
    LinkedList<ApplicationListener<?>> allListeners = new LinkedList<>();
    Set<ApplicationListener<?>> listeners;
    Set<String> listenerBeans;
    synchronized (this.retrievalMutex) {
        listeners = new LinkedHashSet<>(this.defaultRetriever.applicationListeners);
        listenerBeans = new LinkedHashSet<>(this.defaultRetriever.applicationListenerBeans);
    }
    for (ApplicationListener<?> listener : listeners) {
        if (supportsEvent(listener, eventType, sourceType)) {
            if (retriever != null) {
                retriever.applicationListeners.add(listener);
            }
            allListeners.add(listener);
        }
    }
    if (!listenerBeans.isEmpty()) {
        BeanFactory beanFactory = getBeanFactory();
        for (String listenerBeanName : listenerBeans) {
            try {
                Class<?> listenerType = beanFactory.getType(listenerBeanName);
                if (listenerType == null || supportsEvent(listenerType, eventType)) {
                    ApplicationListener<?> listener = beanFactory.getBean(listenerBeanName, ApplicationListener.class);
                    if (!allListeners.contains(listener) && supportsEvent(listener, eventType, sourceType)) {
                        if (retriever != null) {
                            retriever.applicationListenerBeans.add(listenerBeanName);
                        }
                        allListeners.add(listener);
                    }
                }
            } catch (NoSuchBeanDefinitionException ex) {
            // Singleton listener instance (without backing bean definition) disappeared -
            // probably in the middle of the destruction phase
            }
        }
    }
    AnnotationAwareOrderComparator.sort(allListeners);
    return allListeners;
}
Also used : ApplicationListener(org.springframework.context.ApplicationListener) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) LinkedList(java.util.LinkedList)

Example 22 with BeanFactory

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

the class BeanFactoryDataSourceLookupTests method testLookupWhereBeanFactoryYieldsNonDataSourceType.

@Test
public void testLookupWhereBeanFactoryYieldsNonDataSourceType() throws Exception {
    final BeanFactory beanFactory = mock(BeanFactory.class);
    given(beanFactory.getBean(DATASOURCE_BEAN_NAME, DataSource.class)).willThrow(new BeanNotOfRequiredTypeException(DATASOURCE_BEAN_NAME, DataSource.class, String.class));
    try {
        BeanFactoryDataSourceLookup lookup = new BeanFactoryDataSourceLookup(beanFactory);
        lookup.getDataSource(DATASOURCE_BEAN_NAME);
        fail("should have thrown DataSourceLookupFailureException");
    } catch (DataSourceLookupFailureException ex) {
    /* expected */
    }
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) BeanNotOfRequiredTypeException(org.springframework.beans.factory.BeanNotOfRequiredTypeException) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 23 with BeanFactory

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

the class ConfigurationClassProcessingTests method simplestPossibleConfig.

@Test
public void simplestPossibleConfig() {
    BeanFactory factory = initBeanFactory(SimplestPossibleConfig.class);
    String stringBean = factory.getBean("stringBean", String.class);
    assertEquals(stringBean, "foo");
}
Also used : ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory) Test(org.junit.Test)

Example 24 with BeanFactory

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

the class ConfigurationClassProcessingTests method aliasesAreRespected.

private void aliasesAreRespected(Class<?> testClass, Supplier<TestBean> testBeanSupplier, String beanName) {
    TestBean testBean = testBeanSupplier.get();
    BeanFactory factory = initBeanFactory(testClass);
    assertSame(testBean, factory.getBean(beanName));
    Arrays.stream(factory.getAliases(beanName)).map(factory::getBean).forEach(alias -> assertSame(testBean, alias));
    // method name should not be registered
    exception.expect(NoSuchBeanDefinitionException.class);
    factory.getBean("methodName");
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory)

Example 25 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project sakuli by ConSol.

the class BeanLoader method loadMultipleBeans.

/**
     * @param classDef class definition of the expected beans
     * @param <T>      generic type of the returned {@link List}.
     * @return all available beans of type {@code <T>}. If no beans are available, the method returns an empty List.
     */
public static <T> java.util.Map<String, T> loadMultipleBeans(Class<T> classDef) {
    BeanFactory beanFactory = getBeanFactory();
    Map<String, T> beans = null;
    if (beanFactory instanceof ListableBeanFactory) {
        beans = ((ListableBeanFactory) beanFactory).getBeansOfType(classDef);
    }
    return beans != null ? beans : Collections.emptyMap();
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory) ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory)

Aggregations

BeanFactory (org.springframework.beans.factory.BeanFactory)62 Test (org.junit.Test)28 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)17 ITestBean (org.springframework.tests.sample.beans.ITestBean)11 ListableBeanFactory (org.springframework.beans.factory.ListableBeanFactory)10 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)10 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)8 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)8 AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)7 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)7 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)5 FactoryBean (org.springframework.beans.factory.FactoryBean)4 SmartFactoryBean (org.springframework.beans.factory.SmartFactoryBean)4 CallCountingTransactionManager (org.springframework.tests.transaction.CallCountingTransactionManager)4 DataSource (javax.sql.DataSource)3 Ignite (org.apache.ignite.Ignite)3 TestInjectionLifecycleBean (org.apache.ignite.TestInjectionLifecycleBean)3 BeansException (org.springframework.beans.BeansException)3 BeanNotOfRequiredTypeException (org.springframework.beans.factory.BeanNotOfRequiredTypeException)3 HierarchicalBeanFactory (org.springframework.beans.factory.HierarchicalBeanFactory)3