Search in sources :

Example 1 with DependencyDescriptor

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

the class QualifierAnnotationAutowireBeanFactoryTests method testAutowireCandidateExplicitlyFalseWithFieldDescriptor.

@Test
public void testAutowireCandidateExplicitlyFalseWithFieldDescriptor() throws Exception {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    ConstructorArgumentValues cavs = new ConstructorArgumentValues();
    cavs.addGenericArgumentValue(JUERGEN);
    RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null);
    person.setAutowireCandidate(false);
    person.addQualifier(new AutowireCandidateQualifier(TestQualifier.class));
    lbf.registerBeanDefinition(JUERGEN, person);
    DependencyDescriptor qualifiedDescriptor = new DependencyDescriptor(QualifiedTestBean.class.getDeclaredField("qualified"), false);
    DependencyDescriptor nonqualifiedDescriptor = new DependencyDescriptor(QualifiedTestBean.class.getDeclaredField("nonqualified"), false);
    assertFalse(lbf.isAutowireCandidate(JUERGEN, null));
    assertFalse(lbf.isAutowireCandidate(JUERGEN, nonqualifiedDescriptor));
    assertFalse(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor));
}
Also used : DependencyDescriptor(org.springframework.beans.factory.config.DependencyDescriptor) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) Test(org.junit.Test)

Example 2 with DependencyDescriptor

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

the class QualifierAnnotationAutowireBeanFactoryTests method testAutowireCandidateWithMultipleCandidatesDescriptor.

@Test
public void testAutowireCandidateWithMultipleCandidatesDescriptor() throws Exception {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    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);
    assertTrue(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor));
    assertTrue(lbf.isAutowireCandidate(MARK, qualifiedDescriptor));
}
Also used : DependencyDescriptor(org.springframework.beans.factory.config.DependencyDescriptor) MethodParameter(org.springframework.core.MethodParameter) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) Test(org.junit.Test)

Example 3 with DependencyDescriptor

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

the class ParameterAutowireUtils method resolveDependency.

/**
	 * Resolve the dependency for the supplied {@link Parameter} from the
	 * supplied {@link ApplicationContext}.
	 * <p>Provides comprehensive autowiring support for individual method parameters
	 * on par with Spring'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 SpEL expressions in {@code @Value} declarations.
	 * <p>The dependency is required unless the parameter is 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
	 * @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
	 * @param applicationContext the application context from which to resolve the
	 * dependency
	 * @return the resolved object, or {@code null} if none found
	 * @throws BeansException if dependency resolution failed
	 * @see #isAutowirable(Parameter)
	 * @see Autowired#required
	 * @see SynthesizingMethodParameter#forParameter(Parameter)
	 * @see AutowireCapableBeanFactory#resolveDependency(DependencyDescriptor, String)
	 */
static Object resolveDependency(Parameter parameter, Class<?> containingClass, ApplicationContext applicationContext) {
    boolean required = findMergedAnnotation(parameter, Autowired.class).map(Autowired::required).orElse(true);
    MethodParameter methodParameter = SynthesizingMethodParameter.forParameter(parameter);
    DependencyDescriptor descriptor = new DependencyDescriptor(methodParameter, required);
    descriptor.setContainingClass(containingClass);
    return applicationContext.getAutowireCapableBeanFactory().resolveDependency(descriptor, null);
}
Also used : DependencyDescriptor(org.springframework.beans.factory.config.DependencyDescriptor) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) MethodParameter(org.springframework.core.MethodParameter)

Example 4 with DependencyDescriptor

use of org.springframework.beans.factory.config.DependencyDescriptor in project jersey by jersey.

the class AutowiredInjectResolver method getBeanFromSpringContext.

private Object getBeanFromSpringContext(String beanName, Injectee injectee, final boolean required) {
    try {
        DependencyDescriptor dependencyDescriptor = createSpringDependencyDescriptor(injectee);
        Set<String> autowiredBeanNames = new HashSet<>(1);
        autowiredBeanNames.add(beanName);
        return ctx.getAutowireCapableBeanFactory().resolveDependency(dependencyDescriptor, null, autowiredBeanNames, null);
    } catch (NoSuchBeanDefinitionException e) {
        if (required) {
            LOGGER.warning(e.getMessage());
            throw e;
        }
        return null;
    }
}
Also used : DependencyDescriptor(org.springframework.beans.factory.config.DependencyDescriptor) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) HashSet(java.util.HashSet)

Example 5 with DependencyDescriptor

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

the class QualifierAnnotationAutowireBeanFactoryTests method testAutowireCandidateWithFieldDescriptor.

@Ignore
@Test
public void testAutowireCandidateWithFieldDescriptor() throws Exception {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    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);
    DependencyDescriptor qualifiedDescriptor = new DependencyDescriptor(QualifiedTestBean.class.getDeclaredField("qualified"), false);
    DependencyDescriptor nonqualifiedDescriptor = new DependencyDescriptor(QualifiedTestBean.class.getDeclaredField("nonqualified"), false);
    assertTrue(lbf.isAutowireCandidate(JUERGEN, null));
    assertTrue(lbf.isAutowireCandidate(JUERGEN, nonqualifiedDescriptor));
    assertTrue(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor));
    assertTrue(lbf.isAutowireCandidate(MARK, null));
    assertTrue(lbf.isAutowireCandidate(MARK, nonqualifiedDescriptor));
    assertFalse(lbf.isAutowireCandidate(MARK, qualifiedDescriptor));
}
Also used : DependencyDescriptor(org.springframework.beans.factory.config.DependencyDescriptor) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

DependencyDescriptor (org.springframework.beans.factory.config.DependencyDescriptor)12 Test (org.junit.Test)8 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)8 MethodParameter (org.springframework.core.MethodParameter)5 Ignore (org.junit.Ignore)3 LocalVariableTableParameterNameDiscoverer (org.springframework.core.LocalVariableTableParameterNameDiscoverer)2 PropertyDescriptor (java.beans.PropertyDescriptor)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 BeansException (org.springframework.beans.BeansException)1 TypeConverter (org.springframework.beans.TypeConverter)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 UnsatisfiedDependencyException (org.springframework.beans.factory.UnsatisfiedDependencyException)1 PriorityOrdered (org.springframework.core.PriorityOrdered)1 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)1