Search in sources :

Example 1 with NoUniqueBeanDefinitionException

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);
    }
}
Also used : NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) NoUniqueBeanDefinitionException(org.springframework.beans.factory.NoUniqueBeanDefinitionException) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 2 with NoUniqueBeanDefinitionException

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);
    }
}
Also used : ArrayList(java.util.ArrayList) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) NoUniqueBeanDefinitionException(org.springframework.beans.factory.NoUniqueBeanDefinitionException)

Example 3 with NoUniqueBeanDefinitionException

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();
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) NoUniqueBeanDefinitionException(org.springframework.beans.factory.NoUniqueBeanDefinitionException) Test(org.junit.Test)

Example 4 with NoUniqueBeanDefinitionException

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);
    }
}
Also used : NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) NoUniqueBeanDefinitionException(org.springframework.beans.factory.NoUniqueBeanDefinitionException) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 5 with NoUniqueBeanDefinitionException

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();
}
Also used : FailureAnalysis(org.springframework.boot.diagnostics.FailureAnalysis) NoUniqueBeanDefinitionException(org.springframework.beans.factory.NoUniqueBeanDefinitionException) Test(org.junit.Test)

Aggregations

NoUniqueBeanDefinitionException (org.springframework.beans.factory.NoUniqueBeanDefinitionException)10 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)5 ArrayList (java.util.ArrayList)3 IdentityHashMap (java.util.IdentityHashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 WebApplicationContext (org.springframework.web.context.WebApplicationContext)2 List (java.util.List)1 FatalBeanException (org.springframework.beans.FatalBeanException)1 TypeConverter (org.springframework.beans.TypeConverter)1 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)1 DependencyDescriptor (org.springframework.beans.factory.config.DependencyDescriptor)1 NamedBeanHolder (org.springframework.beans.factory.config.NamedBeanHolder)1 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1 FailureAnalysis (org.springframework.boot.diagnostics.FailureAnalysis)1 ApplicationContext (org.springframework.context.ApplicationContext)1