Search in sources :

Example 1 with DependencyDescriptor

use of cn.taketoday.beans.factory.config.DependencyDescriptor in project today-infrastructure by TAKETODAY.

the class ParameterResolutionDelegate method resolveDependency.

/**
 * Resolve the dependency for the supplied {@link Parameter} from the
 * supplied {@link AutowireCapableBeanFactory}.
 * <p>Provides comprehensive autowiring support for individual method parameters
 * on par with Framework's dependency injection facilities for autowired fields and
 * methods, including support for {@link Autowired @Autowired},
 * {@link Qualifier @Qualifier}, and {@link Value @Value} with support for property
 * placeholders and EL expressions in {@code @Value} declarations.
 * <p>The dependency is required unless the parameter is annotated or meta-annotated
 * with {@link Autowired @Autowired} with the {@link Autowired#required required}
 * flag set to {@code false}.
 * <p>If an explicit <em>qualifier</em> is not declared, the name of the parameter
 * will be used as the qualifier for resolving ambiguities.
 *
 * @param parameter the parameter whose dependency should be resolved (must not be
 * {@code null})
 * @param parameterIndex the index of the parameter in the constructor or method
 * that declares the parameter
 * @param containingClass the concrete class that contains the parameter; this may
 * differ from the class that declares the parameter in that it may be a subclass
 * thereof, potentially substituting type variables (must not be {@code null})
 * @param beanFactory the {@code AutowireCapableBeanFactory} from which to resolve
 * the dependency (must not be {@code null})
 * @return the resolved object, or {@code null} if none found
 * @throws BeansException if dependency resolution failed
 * @see #isAutowirable
 * @see Autowired#required
 * @see SynthesizingMethodParameter#forExecutable(Executable, int)
 * @see AutowireCapableBeanFactory#resolveDependency(DependencyDescriptor, String)
 */
@Nullable
public static Object resolveDependency(Parameter parameter, int parameterIndex, Class<?> containingClass, AutowireCapableBeanFactory beanFactory) throws BeansException {
    Assert.notNull(parameter, "Parameter must not be null");
    Assert.notNull(containingClass, "Containing class must not be null");
    Assert.notNull(beanFactory, "AutowireCapableBeanFactory must not be null");
    AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex);
    Autowired autowired = AnnotatedElementUtils.findMergedAnnotation(annotatedParameter, Autowired.class);
    boolean required = (autowired == null || autowired.required());
    MethodParameter methodParameter = SynthesizingMethodParameter.forExecutable(parameter.getDeclaringExecutable(), parameterIndex);
    DependencyDescriptor descriptor = new DependencyDescriptor(methodParameter, required);
    descriptor.setContainingClass(containingClass);
    return beanFactory.resolveDependency(descriptor, null);
}
Also used : DependencyDescriptor(cn.taketoday.beans.factory.config.DependencyDescriptor) AnnotatedElement(java.lang.reflect.AnnotatedElement) MethodParameter(cn.taketoday.core.MethodParameter) SynthesizingMethodParameter(cn.taketoday.core.annotation.SynthesizingMethodParameter) Nullable(cn.taketoday.lang.Nullable)

Example 2 with DependencyDescriptor

use of cn.taketoday.beans.factory.config.DependencyDescriptor in project today-infrastructure by TAKETODAY.

the class QualifierAnnotationAutowireBeanFactoryTests method testAutowireCandidateWithConstructorDescriptor.

@Disabled
@Test
public void testAutowireCandidateWithConstructorDescriptor() throws Exception {
    StandardBeanFactory lbf = new StandardBeanFactory();
    ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
    cavs1.addGenericArgumentValue(JUERGEN);
    RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
    person1.addQualifier(new AutowireCandidateQualifier(TestQualifier.class));
    lbf.registerBeanDefinition(JUERGEN, person1);
    ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
    cavs2.addGenericArgumentValue(MARK);
    RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
    lbf.registerBeanDefinition(MARK, person2);
    MethodParameter param = new MethodParameter(QualifiedTestBean.class.getDeclaredConstructor(Person.class), 0);
    DependencyDescriptor qualifiedDescriptor = new DependencyDescriptor(param, false);
    param.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
    assertThat(param.getParameterName()).isEqualTo("tpb");
    assertThat(lbf.isAutowireCandidate(JUERGEN, null)).isTrue();
    assertThat(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor)).isTrue();
    assertThat(lbf.isAutowireCandidate(MARK, qualifiedDescriptor)).isFalse();
}
Also used : LocalVariableTableParameterNameDiscoverer(cn.taketoday.core.LocalVariableTableParameterNameDiscoverer) DependencyDescriptor(cn.taketoday.beans.factory.config.DependencyDescriptor) MethodParameter(cn.taketoday.core.MethodParameter) ConstructorArgumentValues(cn.taketoday.beans.factory.config.ConstructorArgumentValues) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 3 with DependencyDescriptor

use of cn.taketoday.beans.factory.config.DependencyDescriptor in project today-infrastructure by TAKETODAY.

the class QualifierAnnotationAutowireBeanFactoryTests method testAutowireCandidateWithShortClassName.

@Test
public void testAutowireCandidateWithShortClassName() throws Exception {
    StandardBeanFactory lbf = new StandardBeanFactory();
    ConstructorArgumentValues cavs = new ConstructorArgumentValues();
    cavs.addGenericArgumentValue(JUERGEN);
    RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null);
    person.addQualifier(new AutowireCandidateQualifier(ClassUtils.getShortName(TestQualifier.class)));
    lbf.registerBeanDefinition(JUERGEN, person);
    DependencyDescriptor qualifiedDescriptor = new DependencyDescriptor(QualifiedTestBean.class.getDeclaredField("qualified"), false);
    DependencyDescriptor nonqualifiedDescriptor = new DependencyDescriptor(QualifiedTestBean.class.getDeclaredField("nonqualified"), false);
    assertThat(lbf.isAutowireCandidate(JUERGEN, null)).isTrue();
    assertThat(lbf.isAutowireCandidate(JUERGEN, nonqualifiedDescriptor)).isTrue();
    assertThat(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor)).isTrue();
}
Also used : DependencyDescriptor(cn.taketoday.beans.factory.config.DependencyDescriptor) ConstructorArgumentValues(cn.taketoday.beans.factory.config.ConstructorArgumentValues) Test(org.junit.jupiter.api.Test)

Example 4 with DependencyDescriptor

use of cn.taketoday.beans.factory.config.DependencyDescriptor in project today-infrastructure by TAKETODAY.

the class QualifierAnnotationAutowireBeanFactoryTests method testAutowireCandidateWithMultipleCandidatesDescriptor.

@Test
public void testAutowireCandidateWithMultipleCandidatesDescriptor() throws Exception {
    StandardBeanFactory lbf = new StandardBeanFactory();
    ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
    cavs1.addGenericArgumentValue(JUERGEN);
    RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
    person1.addQualifier(new AutowireCandidateQualifier(TestQualifier.class));
    lbf.registerBeanDefinition(JUERGEN, person1);
    ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
    cavs2.addGenericArgumentValue(MARK);
    RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
    person2.addQualifier(new AutowireCandidateQualifier(TestQualifier.class));
    lbf.registerBeanDefinition(MARK, person2);
    DependencyDescriptor qualifiedDescriptor = new DependencyDescriptor(new MethodParameter(QualifiedTestBean.class.getDeclaredConstructor(Person.class), 0), false);
    assertThat(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor)).isTrue();
    assertThat(lbf.isAutowireCandidate(MARK, qualifiedDescriptor)).isTrue();
}
Also used : DependencyDescriptor(cn.taketoday.beans.factory.config.DependencyDescriptor) MethodParameter(cn.taketoday.core.MethodParameter) ConstructorArgumentValues(cn.taketoday.beans.factory.config.ConstructorArgumentValues) Test(org.junit.jupiter.api.Test)

Example 5 with DependencyDescriptor

use of cn.taketoday.beans.factory.config.DependencyDescriptor in project today-infrastructure by TAKETODAY.

the class QualifierAnnotationAutowireBeanFactoryTests method testAutowireCandidateExplicitlyFalseWithIrrelevantDescriptor.

@Test
public void testAutowireCandidateExplicitlyFalseWithIrrelevantDescriptor() throws Exception {
    StandardBeanFactory lbf = new StandardBeanFactory();
    ConstructorArgumentValues cavs = new ConstructorArgumentValues();
    cavs.addGenericArgumentValue(JUERGEN);
    RootBeanDefinition rbd = new RootBeanDefinition(Person.class, cavs, null);
    rbd.setAutowireCandidate(false);
    lbf.registerBeanDefinition(JUERGEN, rbd);
    assertThat(lbf.isAutowireCandidate(JUERGEN, null)).isFalse();
    assertThat(lbf.isAutowireCandidate(JUERGEN, new DependencyDescriptor(Person.class.getDeclaredField("name"), false))).isFalse();
    assertThat(lbf.isAutowireCandidate(JUERGEN, new DependencyDescriptor(Person.class.getDeclaredField("name"), true))).isFalse();
}
Also used : DependencyDescriptor(cn.taketoday.beans.factory.config.DependencyDescriptor) ConstructorArgumentValues(cn.taketoday.beans.factory.config.ConstructorArgumentValues) Test(org.junit.jupiter.api.Test)

Aggregations

DependencyDescriptor (cn.taketoday.beans.factory.config.DependencyDescriptor)22 Test (org.junit.jupiter.api.Test)18 ConstructorArgumentValues (cn.taketoday.beans.factory.config.ConstructorArgumentValues)16 MethodParameter (cn.taketoday.core.MethodParameter)8 Disabled (org.junit.jupiter.api.Disabled)6 LocalVariableTableParameterNameDiscoverer (cn.taketoday.core.LocalVariableTableParameterNameDiscoverer)4 Nullable (cn.taketoday.lang.Nullable)4 InjectionPoint (cn.taketoday.beans.factory.InjectionPoint)2 NoSuchBeanDefinitionException (cn.taketoday.beans.factory.NoSuchBeanDefinitionException)2 NoUniqueBeanDefinitionException (cn.taketoday.beans.factory.NoUniqueBeanDefinitionException)2 AutowireCapableBeanFactory (cn.taketoday.beans.factory.config.AutowireCapableBeanFactory)2 SynthesizingMethodParameter (cn.taketoday.core.annotation.SynthesizingMethodParameter)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)2 Parameter (java.lang.reflect.Parameter)2