Search in sources :

Example 6 with NoSuchBeanDefinitionException

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

the class ContextAnnotationAutowireCandidateResolver method buildLazyResolutionProxy.

protected Object buildLazyResolutionProxy(final DependencyDescriptor descriptor, final String beanName) {
    Assert.state(getBeanFactory() instanceof DefaultListableBeanFactory, "BeanFactory needs to be a DefaultListableBeanFactory");
    final DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) getBeanFactory();
    TargetSource ts = new TargetSource() {

        @Override
        public Class<?> getTargetClass() {
            return descriptor.getDependencyType();
        }

        @Override
        public boolean isStatic() {
            return false;
        }

        @Override
        public Object getTarget() {
            Object target = beanFactory.doResolveDependency(descriptor, beanName, null, null);
            if (target == null) {
                throw new NoSuchBeanDefinitionException(descriptor.getResolvableType(), "Optional dependency not present for lazy injection point");
            }
            return target;
        }

        @Override
        public void releaseTarget(Object target) {
        }
    };
    ProxyFactory pf = new ProxyFactory();
    pf.setTargetSource(ts);
    Class<?> dependencyType = descriptor.getDependencyType();
    if (dependencyType.isInterface()) {
        pf.addInterface(dependencyType);
    }
    return pf.getProxy(beanFactory.getBeanClassLoader());
}
Also used : TargetSource(org.springframework.aop.TargetSource) ProxyFactory(org.springframework.aop.framework.ProxyFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Example 7 with NoSuchBeanDefinitionException

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

the class AbstractApplicationEventMulticaster method retrieveApplicationListeners.

/**
	 * Actually retrieve the application listeners for the given event and source type.
	 * @param eventType the event type
	 * @param sourceType the event source type
	 * @param retriever the ListenerRetriever, if supposed to populate one (for caching purposes)
	 * @return the pre-filtered list of application listeners for the given event and source type
	 */
private Collection<ApplicationListener<?>> retrieveApplicationListeners(ResolvableType eventType, Class<?> sourceType, ListenerRetriever retriever) {
    LinkedList<ApplicationListener<?>> allListeners = new LinkedList<>();
    Set<ApplicationListener<?>> listeners;
    Set<String> listenerBeans;
    synchronized (this.retrievalMutex) {
        listeners = new LinkedHashSet<>(this.defaultRetriever.applicationListeners);
        listenerBeans = new LinkedHashSet<>(this.defaultRetriever.applicationListenerBeans);
    }
    for (ApplicationListener<?> listener : listeners) {
        if (supportsEvent(listener, eventType, sourceType)) {
            if (retriever != null) {
                retriever.applicationListeners.add(listener);
            }
            allListeners.add(listener);
        }
    }
    if (!listenerBeans.isEmpty()) {
        BeanFactory beanFactory = getBeanFactory();
        for (String listenerBeanName : listenerBeans) {
            try {
                Class<?> listenerType = beanFactory.getType(listenerBeanName);
                if (listenerType == null || supportsEvent(listenerType, eventType)) {
                    ApplicationListener<?> listener = beanFactory.getBean(listenerBeanName, ApplicationListener.class);
                    if (!allListeners.contains(listener) && supportsEvent(listener, eventType, sourceType)) {
                        if (retriever != null) {
                            retriever.applicationListenerBeans.add(listenerBeanName);
                        }
                        allListeners.add(listener);
                    }
                }
            } catch (NoSuchBeanDefinitionException ex) {
            // Singleton listener instance (without backing bean definition) disappeared -
            // probably in the middle of the destruction phase
            }
        }
    }
    AnnotationAwareOrderComparator.sort(allListeners);
    return allListeners;
}
Also used : ApplicationListener(org.springframework.context.ApplicationListener) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) LinkedList(java.util.LinkedList)

Example 8 with NoSuchBeanDefinitionException

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

the class XmlBeanFactoryTests method testBogusParentageFromParentFactory.

/**
	 * Check that a prototype can't inherit from a bogus parent.
	 * If a singleton does this the factory will fail to load.
	 */
@Test
public void testBogusParentageFromParentFactory() throws Exception {
    DefaultListableBeanFactory parent = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(parent).loadBeanDefinitions(PARENT_CONTEXT);
    DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
    new XmlBeanDefinitionReader(child).loadBeanDefinitions(CHILD_CONTEXT);
    try {
        child.getBean("bogusParent", TestBean.class);
        fail();
    } catch (BeanDefinitionStoreException ex) {
        // check exception message contains the name
        assertTrue(ex.getMessage().indexOf("bogusParent") != -1);
        assertTrue(ex.getCause() instanceof NoSuchBeanDefinitionException);
    }
}
Also used : BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Test(org.junit.Test)

Example 9 with NoSuchBeanDefinitionException

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

the class QualifierAnnotationAutowireCandidateResolver method checkQualifier.

/**
	 * Match the given qualifier annotation against the candidate bean definition.
	 */
protected boolean checkQualifier(BeanDefinitionHolder bdHolder, Annotation annotation, TypeConverter typeConverter) {
    Class<? extends Annotation> type = annotation.annotationType();
    RootBeanDefinition bd = (RootBeanDefinition) bdHolder.getBeanDefinition();
    AutowireCandidateQualifier qualifier = bd.getQualifier(type.getName());
    if (qualifier == null) {
        qualifier = bd.getQualifier(ClassUtils.getShortName(type));
    }
    if (qualifier == null) {
        // First, check annotation on qualified element, if any
        Annotation targetAnnotation = getQualifiedElementAnnotation(bd, type);
        // Then, check annotation on factory method, if applicable
        if (targetAnnotation == null) {
            targetAnnotation = getFactoryMethodAnnotation(bd, type);
        }
        if (targetAnnotation == null) {
            RootBeanDefinition dbd = getResolvedDecoratedDefinition(bd);
            if (dbd != null) {
                targetAnnotation = getFactoryMethodAnnotation(dbd, type);
            }
        }
        if (targetAnnotation == null) {
            // Look for matching annotation on the target class
            if (getBeanFactory() != null) {
                try {
                    Class<?> beanType = getBeanFactory().getType(bdHolder.getBeanName());
                    if (beanType != null) {
                        targetAnnotation = AnnotationUtils.getAnnotation(ClassUtils.getUserClass(beanType), type);
                    }
                } catch (NoSuchBeanDefinitionException ex) {
                // Not the usual case - simply forget about the type check...
                }
            }
            if (targetAnnotation == null && bd.hasBeanClass()) {
                targetAnnotation = AnnotationUtils.getAnnotation(ClassUtils.getUserClass(bd.getBeanClass()), type);
            }
        }
        if (targetAnnotation != null && targetAnnotation.equals(annotation)) {
            return true;
        }
    }
    Map<String, Object> attributes = AnnotationUtils.getAnnotationAttributes(annotation);
    if (attributes.isEmpty() && qualifier == null) {
        // If no attributes, the qualifier must be present
        return false;
    }
    for (Map.Entry<String, Object> entry : attributes.entrySet()) {
        String attributeName = entry.getKey();
        Object expectedValue = entry.getValue();
        Object actualValue = null;
        // Check qualifier first
        if (qualifier != null) {
            actualValue = qualifier.getAttribute(attributeName);
        }
        if (actualValue == null) {
            // Fall back on bean definition attribute
            actualValue = bd.getAttribute(attributeName);
        }
        if (actualValue == null && attributeName.equals(AutowireCandidateQualifier.VALUE_KEY) && expectedValue instanceof String && bdHolder.matchesName((String) expectedValue)) {
            // Fall back on bean name (or alias) match
            continue;
        }
        if (actualValue == null && qualifier != null) {
            // Fall back on default, but only if the qualifier is present
            actualValue = AnnotationUtils.getDefaultValue(annotation, attributeName);
        }
        if (actualValue != null) {
            actualValue = typeConverter.convertIfNecessary(actualValue, expectedValue.getClass());
        }
        if (!expectedValue.equals(actualValue)) {
            return false;
        }
    }
    return true;
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) AutowireCandidateQualifier(org.springframework.beans.factory.support.AutowireCandidateQualifier) Map(java.util.Map) Annotation(java.lang.annotation.Annotation)

Example 10 with NoSuchBeanDefinitionException

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

the class QualifierAnnotationAutowireContextTests method autowiredFieldDoesNotResolveWithMultipleQualifierValuesAndConflictingDefaultValue.

@Test
public void autowiredFieldDoesNotResolveWithMultipleQualifierValuesAndConflictingDefaultValue() {
    GenericApplicationContext context = new GenericApplicationContext();
    ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
    cavs1.addGenericArgumentValue(JUERGEN);
    RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
    AutowireCandidateQualifier qualifier = new AutowireCandidateQualifier(TestQualifierWithMultipleAttributes.class);
    qualifier.setAttribute("number", 456);
    person1.addQualifier(qualifier);
    ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
    cavs2.addGenericArgumentValue(MARK);
    RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
    AutowireCandidateQualifier qualifier2 = new AutowireCandidateQualifier(TestQualifierWithMultipleAttributes.class);
    qualifier2.setAttribute("number", 123);
    qualifier2.setAttribute("value", "not the default");
    person2.addQualifier(qualifier2);
    context.registerBeanDefinition(JUERGEN, person1);
    context.registerBeanDefinition(MARK, person2);
    context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedFieldWithMultipleAttributesTestBean.class));
    AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
    try {
        context.refresh();
        fail("expected BeanCreationException");
    } catch (BeanCreationException e) {
        assertTrue(e.getRootCause() instanceof NoSuchBeanDefinitionException);
        assertEquals("autowired", e.getBeanName());
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) Test(org.junit.Test)

Aggregations

NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)64 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 HashMap (java.util.HashMap)8 EventBus (org.apache.cloudstack.framework.events.EventBus)8 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)8 SimpleDateFormat (java.text.SimpleDateFormat)7 Date (java.util.Date)7 EventBusException (org.apache.cloudstack.framework.events.EventBusException)7 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)7 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)6 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)6 BeanFactory (org.springframework.beans.factory.BeanFactory)5 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)5 ArrayList (java.util.ArrayList)4 Account (com.cloud.user.Account)3 Method (java.lang.reflect.Method)3 ApplicationContext (org.springframework.context.ApplicationContext)3