use of org.springframework.beans.factory.NoUniqueBeanDefinitionException in project BroadleafCommerce by BroadleafCommerce.
the class FrameworkMvcUriComponentsBuilder method getRequestMappingInfoHandlerMapping.
private static RequestMappingInfoHandlerMapping getRequestMappingInfoHandlerMapping() {
WebApplicationContext wac = getWebApplicationContext();
Assert.notNull(wac, "Cannot lookup handler method mappings without WebApplicationContext");
try {
return wac.getBean(RequestMappingInfoHandlerMapping.class);
} catch (NoUniqueBeanDefinitionException ex) {
throw new IllegalStateException("More than one RequestMappingInfoHandlerMapping beans found", ex);
} catch (NoSuchBeanDefinitionException ex) {
throw new IllegalStateException("No RequestMappingInfoHandlerMapping bean", ex);
}
}
use of org.springframework.beans.factory.NoUniqueBeanDefinitionException in project mule by mulesoft.
the class DefaultListableBeanFactory method getBean.
@Override
public <T> T getBean(Class<T> requiredType, Object... args) throws BeansException {
Assert.notNull(requiredType, "Required type must not be null");
String[] beanNames = getBeanNamesForType(requiredType);
if (beanNames.length > 1) {
ArrayList<String> autowireCandidates = new ArrayList<String>();
for (String beanName : beanNames) {
if (!containsBeanDefinition(beanName) || getBeanDefinition(beanName).isAutowireCandidate()) {
autowireCandidates.add(beanName);
}
}
if (autowireCandidates.size() > 0) {
beanNames = autowireCandidates.toArray(new String[autowireCandidates.size()]);
}
}
if (beanNames.length == 1) {
return getBean(beanNames[0], requiredType, args);
} else if (beanNames.length > 1) {
Map<String, Object> candidates = new HashMap<String, Object>();
for (String beanName : beanNames) {
candidates.put(beanName, getBean(beanName, requiredType, args));
}
String primaryCandidate = determinePrimaryCandidate(candidates, requiredType);
if (primaryCandidate != null) {
return getBean(primaryCandidate, requiredType, args);
}
String priorityCandidate = determineHighestPriorityCandidate(candidates, requiredType);
if (priorityCandidate != null) {
return getBean(priorityCandidate, requiredType, args);
}
throw new NoUniqueBeanDefinitionException(requiredType, candidates.keySet());
} else if (getParentBeanFactory() != null) {
return getParentBeanFactory().getBean(requiredType, args);
} else {
throw new NoSuchBeanDefinitionException(requiredType);
}
}
use of org.springframework.beans.factory.NoUniqueBeanDefinitionException in project spring-framework by spring-projects.
the class AutowiredAnnotationBeanPostProcessorTests method testSmartObjectFactoryInjectionWithTargetNotUnique.
@Test
public void testSmartObjectFactoryInjectionWithTargetNotUnique() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SmartObjectFactoryInjectionBean.class));
bf.registerBeanDefinition("testBean1", new RootBeanDefinition(TestBean.class));
bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class));
SmartObjectFactoryInjectionBean bean = (SmartObjectFactoryInjectionBean) bf.getBean("annotatedBean");
try {
bean.getTestBean();
fail("Should have thrown NoUniqueBeanDefinitionException");
} catch (NoUniqueBeanDefinitionException ex) {
// expected
}
try {
bean.getOptionalTestBean();
fail("Should have thrown NoUniqueBeanDefinitionException");
} catch (NoUniqueBeanDefinitionException ex) {
// expected
}
assertNull(bean.getUniqueTestBean());
bf.destroySingletons();
}
use of org.springframework.beans.factory.NoUniqueBeanDefinitionException in project spring-framework by spring-projects.
the class MvcUriComponentsBuilder method getRequestMappingInfoHandlerMapping.
private static RequestMappingInfoHandlerMapping getRequestMappingInfoHandlerMapping() {
WebApplicationContext wac = getWebApplicationContext();
Assert.notNull(wac, "Cannot lookup handler method mappings without WebApplicationContext");
try {
return wac.getBean(RequestMappingInfoHandlerMapping.class);
} catch (NoUniqueBeanDefinitionException ex) {
throw new IllegalStateException("More than one RequestMappingInfoHandlerMapping beans found", ex);
} catch (NoSuchBeanDefinitionException ex) {
throw new IllegalStateException("No RequestMappingInfoHandlerMapping bean", ex);
}
}
use of org.springframework.beans.factory.NoUniqueBeanDefinitionException in project spring-boot by spring-projects.
the class NoSuchBeanDefinitionFailureAnalyzerTests method failureAnalysisForMultipleBeans.
@Test
public void failureAnalysisForMultipleBeans() {
FailureAnalysis analysis = analyzeFailure(new NoUniqueBeanDefinitionException(String.class, 2, "Test"));
assertThat(analysis).isNull();
}
Aggregations