Search in sources :

Example 1 with NoSuchBeanDefinitionException

use of cn.taketoday.beans.factory.NoSuchBeanDefinitionException in project today-framework by TAKETODAY.

the class CommonAnnotationBeanPostProcessorTests method testExtendedResourceInjectionWithOverriding.

@Test
public void testExtendedResourceInjectionWithOverriding() {
    StandardBeanFactory bf = new StandardBeanFactory();
    CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerDependency(BeanFactory.class, bf);
    PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
    Properties props = new Properties();
    props.setProperty("tb", "testBean3");
    ppc.setProperties(props);
    ppc.postProcessBeanFactory(bf);
    BeanDefinition annotatedBd = new RootBeanDefinition(ExtendedResourceInjectionBean.class);
    TestBean tb5 = new TestBean();
    annotatedBd.getPropertyValues().add("testBean2", tb5);
    bf.registerBeanDefinition("annotatedBean", annotatedBd);
    bf.registerBeanDefinition("annotatedBean2", new RootBeanDefinition(NamedResourceInjectionBean.class));
    TestBean tb = new TestBean();
    bf.registerSingleton("testBean", tb);
    TestBean tb2 = new TestBean();
    bf.registerSingleton("testBean2", tb2);
    TestBean tb3 = new TestBean();
    bf.registerSingleton("testBean3", tb3);
    TestBean tb4 = new TestBean();
    bf.registerSingleton("testBean4", tb4);
    NestedTestBean tb6 = new NestedTestBean();
    bf.registerSingleton("xy", tb6);
    ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
    assertThat(bean.initCalled).isTrue();
    assertThat(bean.init2Called).isTrue();
    assertThat(bean.getTestBean()).isSameAs(tb);
    assertThat(bean.getTestBean2()).isSameAs(tb5);
    assertThat(bean.getTestBean3()).isSameAs(tb4);
    assertThat(bean.getTestBean4()).isSameAs(tb3);
    assertThat(bean.testBean5).isSameAs(tb6);
    assertThat(bean.testBean6).isSameAs(tb6);
    assertThat(bean.beanFactory).isSameAs(bf);
    try {
        bf.getBean("annotatedBean2");
    } catch (BeanCreationException ex) {
        boolean condition = ex.getRootCause() instanceof NoSuchBeanDefinitionException;
        assertThat(condition).isTrue();
        NoSuchBeanDefinitionException innerEx = (NoSuchBeanDefinitionException) ex.getRootCause();
        assertThat(innerEx.getBeanName()).isEqualTo("testBean9");
    }
    bf.destroySingletons();
    assertThat(bean.destroyCalled).isTrue();
    assertThat(bean.destroy2Called).isTrue();
}
Also used : PropertyPlaceholderConfigurer(cn.taketoday.beans.factory.config.PropertyPlaceholderConfigurer) BeanCreationException(cn.taketoday.beans.factory.BeanCreationException) NestedTestBean(cn.taketoday.beans.testfixture.beans.NestedTestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) INestedTestBean(cn.taketoday.beans.testfixture.beans.INestedTestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) NestedTestBean(cn.taketoday.beans.testfixture.beans.NestedTestBean) INestedTestBean(cn.taketoday.beans.testfixture.beans.INestedTestBean) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) Properties(java.util.Properties) BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) NoSuchBeanDefinitionException(cn.taketoday.beans.factory.NoSuchBeanDefinitionException) Test(org.junit.jupiter.api.Test)

Example 2 with NoSuchBeanDefinitionException

use of cn.taketoday.beans.factory.NoSuchBeanDefinitionException in project today-framework by TAKETODAY.

the class ConstructorResolver method resolveAutowiredArgument.

/**
 * Template method for resolving the specified argument which is supposed to be autowired.
 */
@Nullable
private Object resolveAutowiredArgument(MethodParameter param, String beanName, @Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter, boolean fallback) {
    Class<?> paramType = param.getParameterType();
    if (InjectionPoint.class.isAssignableFrom(paramType)) {
        InjectionPoint injectionPoint = currentInjectionPoint.get();
        if (injectionPoint == null) {
            throw new IllegalStateException("No current InjectionPoint available for " + param);
        }
        return injectionPoint;
    }
    try {
        DependencyResolvingContext context = new DependencyResolvingContext(param.getExecutable(), beanFactory, beanName);
        context.setTypeConverter(typeConverter);
        context.setDependentBeans(autowiredBeanNames);
        return injector.resolveValue(new DependencyDescriptor(param, true), context);
    } catch (NoUniqueBeanDefinitionException ex) {
        throw ex;
    } catch (NoSuchBeanDefinitionException ex) {
        if (fallback) {
            // for e.g. a vararg or a non-null List/Set/Map parameter.
            if (paramType.isArray()) {
                return Array.newInstance(paramType.getComponentType(), 0);
            } else if (CollectionUtils.isApproximableCollectionType(paramType)) {
                return CollectionUtils.createCollection(paramType, 0);
            } else if (CollectionUtils.isApproximableMapType(paramType)) {
                return CollectionUtils.createMap(paramType, 0);
            }
        }
        throw ex;
    }
}
Also used : InjectionPoint(cn.taketoday.beans.factory.InjectionPoint) DependencyDescriptor(cn.taketoday.beans.factory.config.DependencyDescriptor) NoSuchBeanDefinitionException(cn.taketoday.beans.factory.NoSuchBeanDefinitionException) NoUniqueBeanDefinitionException(cn.taketoday.beans.factory.NoUniqueBeanDefinitionException) Nullable(cn.taketoday.lang.Nullable)

Example 3 with NoSuchBeanDefinitionException

use of cn.taketoday.beans.factory.NoSuchBeanDefinitionException in project today-framework by TAKETODAY.

the class NoUniqueBeanDefinitionFailureAnalyzer method buildMessage.

private void buildMessage(StringBuilder message, String beanName) {
    try {
        BeanDefinition definition = BeanFactoryUtils.requiredDefinition(beanFactory, beanName);
        message.append(getDefinitionDescription(beanName, definition));
    } catch (NoSuchBeanDefinitionException ex) {
        message.append(String.format("\t- %s: a programmatically registered singleton", beanName));
    }
}
Also used : BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition) NoSuchBeanDefinitionException(cn.taketoday.beans.factory.NoSuchBeanDefinitionException)

Example 4 with NoSuchBeanDefinitionException

use of cn.taketoday.beans.factory.NoSuchBeanDefinitionException in project today-framework by TAKETODAY.

the class QualifierAnnotationAutowireCandidateResolver method checkQualifier.

/**
 * Match the given qualifier annotation against the candidate bean definition.
 */
protected boolean checkQualifier(BeanDefinition definition, Annotation annotation, SimpleTypeConverter typeConverter) {
    Class<? extends Annotation> type = annotation.annotationType();
    RootBeanDefinition bd = (RootBeanDefinition) definition;
    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(definition.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 && definition.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(cn.taketoday.beans.factory.support.RootBeanDefinition) NoSuchBeanDefinitionException(cn.taketoday.beans.factory.NoSuchBeanDefinitionException) AutowireCandidateQualifier(cn.taketoday.beans.factory.support.AutowireCandidateQualifier) Map(java.util.Map) Annotation(java.lang.annotation.Annotation)

Example 5 with NoSuchBeanDefinitionException

use of cn.taketoday.beans.factory.NoSuchBeanDefinitionException in project today-framework by TAKETODAY.

the class ContextAnnotationAutowireCandidateResolver method buildLazyResolutionProxy.

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

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

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

        @Override
        public Object getTarget() {
            Set<String> autowiredBeanNames = beanName != null ? new LinkedHashSet<>(1) : null;
            Object target = dlbf.doResolveDependency(descriptor, beanName, autowiredBeanNames, null);
            if (target == null) {
                Class<?> type = getTargetClass();
                if (Map.class == type) {
                    return Collections.emptyMap();
                } else if (List.class == type) {
                    return Collections.emptyList();
                } else if (Set.class == type || Collection.class == type) {
                    return Collections.emptySet();
                }
                throw new NoSuchBeanDefinitionException(descriptor.getResolvableType(), "Optional dependency not present for lazy injection point");
            }
            if (autowiredBeanNames != null) {
                for (String autowiredBeanName : autowiredBeanNames) {
                    if (dlbf.containsBean(autowiredBeanName)) {
                        dlbf.registerDependentBean(autowiredBeanName, beanName);
                    }
                }
            }
            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(dlbf.getBeanClassLoader());
}
Also used : TargetSource(cn.taketoday.aop.TargetSource) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) BeanFactory(cn.taketoday.beans.factory.BeanFactory) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) Collection(java.util.Collection) List(java.util.List) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) NoSuchBeanDefinitionException(cn.taketoday.beans.factory.NoSuchBeanDefinitionException)

Aggregations

NoSuchBeanDefinitionException (cn.taketoday.beans.factory.NoSuchBeanDefinitionException)5 BeanDefinition (cn.taketoday.beans.factory.config.BeanDefinition)2 RootBeanDefinition (cn.taketoday.beans.factory.support.RootBeanDefinition)2 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)2 TargetSource (cn.taketoday.aop.TargetSource)1 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)1 BeanCreationException (cn.taketoday.beans.factory.BeanCreationException)1 BeanFactory (cn.taketoday.beans.factory.BeanFactory)1 InjectionPoint (cn.taketoday.beans.factory.InjectionPoint)1 NoUniqueBeanDefinitionException (cn.taketoday.beans.factory.NoUniqueBeanDefinitionException)1 DependencyDescriptor (cn.taketoday.beans.factory.config.DependencyDescriptor)1 PropertyPlaceholderConfigurer (cn.taketoday.beans.factory.config.PropertyPlaceholderConfigurer)1 AutowireCandidateQualifier (cn.taketoday.beans.factory.support.AutowireCandidateQualifier)1 INestedTestBean (cn.taketoday.beans.testfixture.beans.INestedTestBean)1 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)1 NestedTestBean (cn.taketoday.beans.testfixture.beans.NestedTestBean)1 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)1 Nullable (cn.taketoday.lang.Nullable)1 Annotation (java.lang.annotation.Annotation)1 Collection (java.util.Collection)1