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