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;
}
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 */
}
}
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");
}
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");
}
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();
}
Aggregations