Search in sources :

Example 71 with NoSuchBeanDefinitionException

use of org.springframework.beans.factory.NoSuchBeanDefinitionException in project leopard by tanhaichao.

the class LeopardPropertyPlaceholderConfigurer method getBean.

public <T> T getBean(BeanFactory beanFactory, Class<T> requiredType) throws BeansException {
    DefaultListableBeanFactory factory = (DefaultListableBeanFactory) beanFactory;
    Map<String, T> matchingBeans = factory.getBeansOfType(requiredType);
    if (matchingBeans.isEmpty()) {
        throw new NoSuchBeanDefinitionException(requiredType);
    }
    if (matchingBeans.size() == 1) {
        return matchingBeans.entrySet().iterator().next().getValue();
    }
    for (Entry<String, T> entry : matchingBeans.entrySet()) {
        T bean = entry.getValue();
        // TODO 还没有支持Bean有AOP
        Primary primary = bean.getClass().getDeclaredAnnotation(Primary.class);
        if (primary != null) {
            return bean;
        }
    }
    throw new NoUniqueBeanDefinitionException(requiredType, matchingBeans.keySet());
}
Also used : Primary(org.springframework.context.annotation.Primary) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) NoUniqueBeanDefinitionException(org.springframework.beans.factory.NoUniqueBeanDefinitionException)

Aggregations

NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)71 Test (org.junit.Test)27 BeanCreationException (org.springframework.beans.factory.BeanCreationException)18 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)15 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)14 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)9 HashMap (java.util.HashMap)8 EventBus (org.apache.cloudstack.framework.events.EventBus)8 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)8 SimpleDateFormat (java.text.SimpleDateFormat)7 Date (java.util.Date)7 EventBusException (org.apache.cloudstack.framework.events.EventBusException)7 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)7 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)6 Method (java.lang.reflect.Method)5 BeanFactory (org.springframework.beans.factory.BeanFactory)5 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)5 ApplicationContext (org.springframework.context.ApplicationContext)5 ArrayList (java.util.ArrayList)4 Account (com.cloud.user.Account)3