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));
}
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));
}
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);
}
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;
}
}
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));
}
Aggregations