Search in sources :

Example 1 with NonUniqueBeanException

use of io.micronaut.context.exceptions.NonUniqueBeanException in project micronaut-core by micronaut-projects.

the class DefaultBeanContext method inject.

@Override
@NonNull
public <T> T inject(@NonNull T instance) {
    Objects.requireNonNull(instance, "Instance cannot be null");
    Collection<BeanDefinition> candidates = findBeanCandidatesForInstance(instance);
    if (candidates.size() == 1) {
        BeanDefinition<T> beanDefinition = candidates.stream().findFirst().get();
        try (BeanResolutionContext resolutionContext = newResolutionContext(beanDefinition, null)) {
            final BeanKey<T> beanKey = new BeanKey<>(beanDefinition.getBeanType(), null);
            resolutionContext.addInFlightBean(beanKey, instance);
            doInject(resolutionContext, instance, beanDefinition);
        }
    } else if (!candidates.isEmpty()) {
        final Iterator iterator = candidates.iterator();
        throw new NonUniqueBeanException(instance.getClass(), iterator);
    }
    return instance;
}
Also used : ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) NonUniqueBeanException(io.micronaut.context.exceptions.NonUniqueBeanException) ProxyBeanDefinition(io.micronaut.inject.ProxyBeanDefinition) BeanDefinition(io.micronaut.inject.BeanDefinition) ValidatedBeanDefinition(io.micronaut.inject.ValidatedBeanDefinition) DisposableBeanDefinition(io.micronaut.inject.DisposableBeanDefinition) InitializingBeanDefinition(io.micronaut.inject.InitializingBeanDefinition) NonNull(io.micronaut.core.annotation.NonNull)

Example 2 with NonUniqueBeanException

use of io.micronaut.context.exceptions.NonUniqueBeanException in project micronaut-core by micronaut-projects.

the class DefaultBeanContext method getBeanInternal.

private <T> T getBeanInternal(@Nullable BeanResolutionContext resolutionContext, @NonNull Argument<T> beanType, Qualifier<T> qualifier, boolean throwNonUnique, boolean throwNoSuchBean) {
    // allow injection the bean context
    final Class<T> beanClass = beanType.getType();
    if (thisInterfaces.contains(beanClass)) {
        return (T) this;
    }
    if (InjectionPoint.class.isAssignableFrom(beanClass)) {
        final BeanResolutionContext.Path path = resolutionContext != null ? resolutionContext.getPath() : null;
        if (CollectionUtils.isNotEmpty(path)) {
            final Iterator<BeanResolutionContext.Segment<?>> i = path.iterator();
            final BeanResolutionContext.Segment<?> injectionPointSegment = i.next();
            if (i.hasNext()) {
                BeanResolutionContext.Segment segment = i.next();
                final BeanDefinition declaringBean = segment.getDeclaringType();
                if (declaringBean.hasStereotype(INTRODUCTION_TYPE)) {
                    if (!i.hasNext()) {
                        if (!injectionPointSegment.getArgument().isNullable()) {
                            throw new BeanContextException("Failed to obtain injection point. No valid injection path present in path: " + path);
                        } else {
                            return null;
                        }
                    } else {
                        segment = i.next();
                    }
                }
                T ip = (T) segment.getInjectionPoint();
                if (beanClass.isInstance(ip)) {
                    return ip;
                } else {
                    if (!injectionPointSegment.getArgument().isNullable()) {
                        throw new DependencyInjectionException(resolutionContext, "Failed to obtain injection point. No valid injection path present in path: " + path);
                    } else {
                        return null;
                    }
                }
            } else {
                if (!injectionPointSegment.getArgument().isNullable()) {
                    throw new DependencyInjectionException(resolutionContext, "Failed to obtain injection point. No valid injection path present in path: " + path);
                } else {
                    return null;
                }
            }
        } else {
            throw new DependencyInjectionException(resolutionContext, "Failed to obtain injection point. No valid injection path present in path: " + path);
        }
    }
    BeanKey<T> beanKey = new BeanKey<>(beanType, qualifier);
    if (LOG.isTraceEnabled()) {
        LOG.trace("Looking up existing bean for key: {}", beanKey);
    }
    T inFlightBean = resolutionContext != null ? resolutionContext.getInFlightBean(beanKey) : null;
    if (inFlightBean != null) {
        return inFlightBean;
    }
    BeanRegistration<T> beanRegistration = singletonObjects.get(beanKey);
    if (beanRegistration != null) {
        T bean = beanRegistration.bean;
        if (bean == null && throwNoSuchBean) {
            throw new NoSuchBeanException(beanType, qualifier);
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Resolved existing bean [{}] for type [{}] and qualifier [{}]", beanRegistration.bean, beanType, qualifier);
            }
            return bean;
        }
    } else if (LOG.isTraceEnabled()) {
        LOG.trace("No existing bean found for bean key: {}", beanKey);
    }
    synchronized (singletonObjects) {
        Optional<BeanDefinition<T>> concreteCandidate = findConcreteCandidate(resolutionContext, beanType, qualifier, throwNonUnique);
        T bean = null;
        if (concreteCandidate.isPresent()) {
            BeanDefinition<T> definition = concreteCandidate.get();
            if (definition.isContainerType() && beanClass != definition.getBeanType()) {
                throw new NonUniqueBeanException(beanClass, Collections.singletonList(definition).iterator());
            }
            if (definition.isSingleton()) {
                final BeanRegistration<T> registration = findExistingCompatibleSingleton(definition.asArgument(), beanType, qualifier, definition);
                if (registration != null) {
                    bean = registration.getBean();
                }
            }
            if (bean != null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Resolved existing bean [{}] for type [{}] and qualifier [{}]", bean, beanType, qualifier);
                }
                return bean;
            } else {
                bean = getBeanForDefinition(resolutionContext, beanType, qualifier, throwNoSuchBean, definition);
                if (bean == null && throwNoSuchBean) {
                    throw new NoSuchBeanException(beanType, qualifier);
                } else {
                    return bean;
                }
            }
        } else {
            final BeanRegistration<T> registration = findExistingCompatibleSingleton(beanType, null, qualifier, null);
            if (registration != null) {
                bean = registration.getBean();
            }
            if (bean == null && throwNoSuchBean) {
                throw new NoSuchBeanException(beanType, qualifier);
            } else {
                return bean;
            }
        }
    }
}
Also used : NoSuchBeanException(io.micronaut.context.exceptions.NoSuchBeanException) NonUniqueBeanException(io.micronaut.context.exceptions.NonUniqueBeanException) ProxyBeanDefinition(io.micronaut.inject.ProxyBeanDefinition) BeanDefinition(io.micronaut.inject.BeanDefinition) ValidatedBeanDefinition(io.micronaut.inject.ValidatedBeanDefinition) DisposableBeanDefinition(io.micronaut.inject.DisposableBeanDefinition) InitializingBeanDefinition(io.micronaut.inject.InitializingBeanDefinition) DependencyInjectionException(io.micronaut.context.exceptions.DependencyInjectionException) BeanContextException(io.micronaut.context.exceptions.BeanContextException)

Aggregations

NonUniqueBeanException (io.micronaut.context.exceptions.NonUniqueBeanException)2 BeanDefinition (io.micronaut.inject.BeanDefinition)2 DisposableBeanDefinition (io.micronaut.inject.DisposableBeanDefinition)2 InitializingBeanDefinition (io.micronaut.inject.InitializingBeanDefinition)2 ProxyBeanDefinition (io.micronaut.inject.ProxyBeanDefinition)2 ValidatedBeanDefinition (io.micronaut.inject.ValidatedBeanDefinition)2 BeanContextException (io.micronaut.context.exceptions.BeanContextException)1 DependencyInjectionException (io.micronaut.context.exceptions.DependencyInjectionException)1 NoSuchBeanException (io.micronaut.context.exceptions.NoSuchBeanException)1 NonNull (io.micronaut.core.annotation.NonNull)1 Iterator (java.util.Iterator)1 ListIterator (java.util.ListIterator)1