Search in sources :

Example 51 with NoSuchBeanDefinitionException

use of org.springframework.beans.factory.NoSuchBeanDefinitionException 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 52 with NoSuchBeanDefinitionException

use of org.springframework.beans.factory.NoSuchBeanDefinitionException in project spring-boot by spring-projects.

the class ResetMocksTestExecutionListener method resetMocks.

private void resetMocks(ConfigurableApplicationContext applicationContext, MockReset reset) {
    ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
    String[] names = beanFactory.getBeanDefinitionNames();
    Set<String> instantiatedSingletons = new HashSet<>(Arrays.asList(beanFactory.getSingletonNames()));
    for (String name : names) {
        BeanDefinition definition = beanFactory.getBeanDefinition(name);
        if (definition.isSingleton() && instantiatedSingletons.contains(name)) {
            Object bean = beanFactory.getSingleton(name);
            if (reset.equals(MockReset.get(bean))) {
                Mockito.reset(bean);
            }
        }
    }
    try {
        MockitoBeans mockedBeans = beanFactory.getBean(MockitoBeans.class);
        for (Object mockedBean : mockedBeans) {
            if (reset.equals(MockReset.get(mockedBean))) {
                Mockito.reset(mockedBean);
            }
        }
    } catch (NoSuchBeanDefinitionException ex) {
    // Continue
    }
    if (applicationContext.getParent() != null) {
        resetMocks(applicationContext.getParent(), reset);
    }
}
Also used : BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) HashSet(java.util.HashSet)

Example 53 with NoSuchBeanDefinitionException

use of org.springframework.beans.factory.NoSuchBeanDefinitionException in project spring-framework by spring-projects.

the class AbstractBeanFactoryTests method aliasing.

// TODO: refactor in AbstractBeanFactory (tests for AbstractBeanFactory)
// and rename this class
@Test
public void aliasing() {
    BeanFactory bf = getBeanFactory();
    if (!(bf instanceof ConfigurableBeanFactory)) {
        return;
    }
    ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) bf;
    String alias = "rods alias";
    try {
        cbf.getBean(alias);
        fail("Shouldn't permit factory get on normal bean");
    } catch (NoSuchBeanDefinitionException ex) {
        // Ok
        assertTrue(alias.equals(ex.getBeanName()));
    }
    // Create alias
    cbf.registerAlias("rod", alias);
    Object rod = getBeanFactory().getBean("rod");
    Object aliasRod = getBeanFactory().getBean(alias);
    assertTrue(rod == aliasRod);
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Test(org.junit.Test)

Example 54 with NoSuchBeanDefinitionException

use of org.springframework.beans.factory.NoSuchBeanDefinitionException in project spring-framework by spring-projects.

the class DefaultListableBeanFactory method getBean.

@Override
public <T> T getBean(Class<T> requiredType, Object... args) throws BeansException {
    NamedBeanHolder<T> namedBean = resolveNamedBean(requiredType, args);
    if (namedBean != null) {
        return namedBean.getBeanInstance();
    }
    BeanFactory parent = getParentBeanFactory();
    if (parent != null) {
        return parent.getBean(requiredType, args);
    }
    throw new NoSuchBeanDefinitionException(requiredType);
}
Also used : ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Example 55 with NoSuchBeanDefinitionException

use of org.springframework.beans.factory.NoSuchBeanDefinitionException in project spring-framework by spring-projects.

the class AutowiredAnnotationBeanPostProcessor method determineCandidateConstructors.

@Override
public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, final String beanName) throws BeanCreationException {
    // Let's check for lookup methods here..
    if (!this.lookupMethodsChecked.contains(beanName)) {
        try {
            ReflectionUtils.doWithMethods(beanClass, new ReflectionUtils.MethodCallback() {

                @Override
                public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
                    Lookup lookup = method.getAnnotation(Lookup.class);
                    if (lookup != null) {
                        LookupOverride override = new LookupOverride(method, lookup.value());
                        try {
                            RootBeanDefinition mbd = (RootBeanDefinition) beanFactory.getMergedBeanDefinition(beanName);
                            mbd.getMethodOverrides().addOverride(override);
                        } catch (NoSuchBeanDefinitionException ex) {
                            throw new BeanCreationException(beanName, "Cannot apply @Lookup to beans without corresponding bean definition");
                        }
                    }
                }
            });
        } catch (IllegalStateException ex) {
            throw new BeanCreationException(beanName, "Lookup method resolution failed", ex);
        }
        this.lookupMethodsChecked.add(beanName);
    }
    // Quick check on the concurrent map first, with minimal locking.
    Constructor<?>[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);
    if (candidateConstructors == null) {
        // Fully synchronized resolution now...
        synchronized (this.candidateConstructorsCache) {
            candidateConstructors = this.candidateConstructorsCache.get(beanClass);
            if (candidateConstructors == null) {
                Constructor<?>[] rawCandidates;
                try {
                    rawCandidates = beanClass.getDeclaredConstructors();
                } catch (Throwable ex) {
                    throw new BeanCreationException(beanName, "Resolution of declared constructors on bean Class [" + beanClass.getName() + "] from ClassLoader [" + beanClass.getClassLoader() + "] failed", ex);
                }
                List<Constructor<?>> candidates = new ArrayList<Constructor<?>>(rawCandidates.length);
                Constructor<?> requiredConstructor = null;
                Constructor<?> defaultConstructor = null;
                for (Constructor<?> candidate : rawCandidates) {
                    AnnotationAttributes ann = findAutowiredAnnotation(candidate);
                    if (ann == null) {
                        Class<?> userClass = ClassUtils.getUserClass(beanClass);
                        if (userClass != beanClass) {
                            try {
                                Constructor<?> superCtor = userClass.getDeclaredConstructor(candidate.getParameterTypes());
                                ann = findAutowiredAnnotation(superCtor);
                            } catch (NoSuchMethodException ex) {
                            // Simply proceed, no equivalent superclass constructor found...
                            }
                        }
                    }
                    if (ann != null) {
                        if (requiredConstructor != null) {
                            throw new BeanCreationException(beanName, "Invalid autowire-marked constructor: " + candidate + ". Found constructor with 'required' Autowired annotation already: " + requiredConstructor);
                        }
                        boolean required = determineRequiredStatus(ann);
                        if (required) {
                            if (!candidates.isEmpty()) {
                                throw new BeanCreationException(beanName, "Invalid autowire-marked constructors: " + candidates + ". Found constructor with 'required' Autowired annotation: " + candidate);
                            }
                            requiredConstructor = candidate;
                        }
                        candidates.add(candidate);
                    } else if (candidate.getParameterCount() == 0) {
                        defaultConstructor = candidate;
                    }
                }
                if (!candidates.isEmpty()) {
                    // Add default constructor to list of optional constructors, as fallback.
                    if (requiredConstructor == null) {
                        if (defaultConstructor != null) {
                            candidates.add(defaultConstructor);
                        } else if (candidates.size() == 1 && logger.isWarnEnabled()) {
                            logger.warn("Inconsistent constructor declaration on bean with name '" + beanName + "': single autowire-marked constructor flagged as optional - " + "this constructor is effectively required since there is no " + "default constructor to fall back to: " + candidates.get(0));
                        }
                    }
                    candidateConstructors = candidates.toArray(new Constructor<?>[candidates.size()]);
                } else if (rawCandidates.length == 1 && rawCandidates[0].getParameterCount() > 0) {
                    candidateConstructors = new Constructor<?>[] { rawCandidates[0] };
                } else {
                    candidateConstructors = new Constructor<?>[0];
                }
                this.candidateConstructorsCache.put(beanClass, candidateConstructors);
            }
        }
    }
    return (candidateConstructors.length > 0 ? candidateConstructors : null);
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes) Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) LookupOverride(org.springframework.beans.factory.support.LookupOverride) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ReflectionUtils(org.springframework.util.ReflectionUtils) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) LookupOverride(org.springframework.beans.factory.support.LookupOverride) LookupOverride(org.springframework.beans.factory.support.LookupOverride)

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