Example usage for org.springframework.beans.factory NoUniqueBeanDefinitionException getBeanNamesFound

List of usage examples for org.springframework.beans.factory NoUniqueBeanDefinitionException getBeanNamesFound

Introduction

In this page you can find the example usage for org.springframework.beans.factory NoUniqueBeanDefinitionException getBeanNamesFound.

Prototype

@Nullable
public Collection<String> getBeanNamesFound() 

Source Link

Document

Return the names of all beans found when only one matching bean was expected.

Usage

From source file:org.springframework.aop.interceptor.AsyncExecutionAspectSupport.java

/**
 * Retrieve or build a default executor for this advice instance.
 * An executor returned from here will be cached for further use.
 * <p>The default implementation searches for a unique {@link TaskExecutor} bean
 * in the context, or for an {@link Executor} bean named "taskExecutor" otherwise.
 * If neither of the two is resolvable, this implementation will return {@code null}.
 * @param beanFactory the BeanFactory to use for a default executor lookup
 * @return the default executor, or {@code null} if none available
 * @since 4.2.6/*  w ww  .  ja v  a  2s .c  om*/
 * @see #findQualifiedExecutor(BeanFactory, String)
 * @see #DEFAULT_TASK_EXECUTOR_BEAN_NAME
 */
@Nullable
protected Executor getDefaultExecutor(@Nullable BeanFactory beanFactory) {
    if (beanFactory != null) {
        try {
            // Search for TaskExecutor bean... not plain Executor since that would
            // match with ScheduledExecutorService as well, which is unusable for
            // our purposes here. TaskExecutor is more clearly designed for it.
            return beanFactory.getBean(TaskExecutor.class);
        } catch (NoUniqueBeanDefinitionException ex) {
            logger.debug("Could not find unique TaskExecutor bean", ex);
            try {
                return beanFactory.getBean(DEFAULT_TASK_EXECUTOR_BEAN_NAME, Executor.class);
            } catch (NoSuchBeanDefinitionException ex2) {
                if (logger.isInfoEnabled()) {
                    logger.info("More than one TaskExecutor bean found within the context, and none is named "
                            + "'taskExecutor'. Mark one of them as primary or name it 'taskExecutor' (possibly "
                            + "as an alias) in order to use it for async processing: "
                            + ex.getBeanNamesFound());
                }
            }
        } catch (NoSuchBeanDefinitionException ex) {
            logger.debug("Could not find default TaskExecutor bean", ex);
            try {
                return beanFactory.getBean(DEFAULT_TASK_EXECUTOR_BEAN_NAME, Executor.class);
            } catch (NoSuchBeanDefinitionException ex2) {
                logger.info("No task executor bean found for async processing: "
                        + "no bean of type TaskExecutor and no bean named 'taskExecutor' either");
            }
            // Giving up -> either using local default executor or none at all...
        }
    }
    return null;
}

From source file:org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.java

private void finishRegistration() {
    if (this.scheduler != null) {
        this.registrar.setScheduler(this.scheduler);
    }//from   w  ww  . ja v  a2  s .  c  o  m

    if (this.beanFactory instanceof ListableBeanFactory) {
        Map<String, SchedulingConfigurer> beans = ((ListableBeanFactory) this.beanFactory)
                .getBeansOfType(SchedulingConfigurer.class);
        List<SchedulingConfigurer> configurers = new ArrayList<>(beans.values());
        AnnotationAwareOrderComparator.sort(configurers);
        for (SchedulingConfigurer configurer : configurers) {
            configurer.configureTasks(this.registrar);
        }
    }

    if (this.registrar.hasTasks() && this.registrar.getScheduler() == null) {
        Assert.state(this.beanFactory != null, "BeanFactory must be set to find scheduler by type");
        try {
            // Search for TaskScheduler bean...
            this.registrar.setTaskScheduler(resolveSchedulerBean(beanFactory, TaskScheduler.class, false));
        } catch (NoUniqueBeanDefinitionException ex) {
            logger.debug("Could not find unique TaskScheduler bean", ex);
            try {
                this.registrar.setTaskScheduler(resolveSchedulerBean(beanFactory, TaskScheduler.class, true));
            } catch (NoSuchBeanDefinitionException ex2) {
                if (logger.isInfoEnabled()) {
                    logger.info("More than one TaskScheduler bean exists within the context, and "
                            + "none is named 'taskScheduler'. Mark one of them as primary or name it 'taskScheduler' "
                            + "(possibly as an alias); or implement the SchedulingConfigurer interface and call "
                            + "ScheduledTaskRegistrar#setScheduler explicitly within the configureTasks() callback: "
                            + ex.getBeanNamesFound());
                }
            }
        } catch (NoSuchBeanDefinitionException ex) {
            logger.debug("Could not find default TaskScheduler bean", ex);
            // Search for ScheduledExecutorService bean next...
            try {
                this.registrar
                        .setScheduler(resolveSchedulerBean(beanFactory, ScheduledExecutorService.class, false));
            } catch (NoUniqueBeanDefinitionException ex2) {
                logger.debug("Could not find unique ScheduledExecutorService bean", ex2);
                try {
                    this.registrar.setScheduler(
                            resolveSchedulerBean(beanFactory, ScheduledExecutorService.class, true));
                } catch (NoSuchBeanDefinitionException ex3) {
                    if (logger.isInfoEnabled()) {
                        logger.info(
                                "More than one ScheduledExecutorService bean exists within the context, and "
                                        + "none is named 'taskScheduler'. Mark one of them as primary or name it 'taskScheduler' "
                                        + "(possibly as an alias); or implement the SchedulingConfigurer interface and call "
                                        + "ScheduledTaskRegistrar#setScheduler explicitly within the configureTasks() callback: "
                                        + ex2.getBeanNamesFound());
                    }
                }
            } catch (NoSuchBeanDefinitionException ex2) {
                logger.debug("Could not find default ScheduledExecutorService bean", ex2);
                // Giving up -> falling back to default scheduler within the registrar...
                logger.info("No TaskScheduler/ScheduledExecutorService bean found for scheduled processing");
            }
        }
    }

    this.registrar.afterPropertiesSet();
}