org.springframework.beans.factory.config.DependencyDescriptor Java Examples

The following examples show how to use org.springframework.beans.factory.config.DependencyDescriptor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ParameterResolutionTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void resolveDependencyForAnnotatedParametersInTopLevelClassConstructor() throws Exception {
	Constructor<?> constructor = AutowirableClass.class.getConstructor(String.class, String.class, String.class, String.class);

	AutowireCapableBeanFactory beanFactory = mock(AutowireCapableBeanFactory.class);
	// Configure the mocked BeanFactory to return the DependencyDescriptor for convenience and
	// to avoid using an ArgumentCaptor.
	given(beanFactory.resolveDependency(any(), isNull())).willAnswer(invocation -> invocation.getArgument(0));

	Parameter[] parameters = constructor.getParameters();
	for (int parameterIndex = 0; parameterIndex < parameters.length; parameterIndex++) {
		Parameter parameter = parameters[parameterIndex];
		DependencyDescriptor intermediateDependencyDescriptor = (DependencyDescriptor) ParameterResolutionDelegate.resolveDependency(
				parameter, parameterIndex, AutowirableClass.class, beanFactory);
		assertEquals(constructor, intermediateDependencyDescriptor.getAnnotatedElement());
		assertEquals(parameter, intermediateDependencyDescriptor.getMethodParameter().getParameter());
	}
}
 
Example #2
Source File: DefaultListableBeanFactory.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public Object resolveDependency(DependencyDescriptor descriptor, String beanName,
		Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException {

	descriptor.initParameterNameDiscovery(getParameterNameDiscoverer());
	if (descriptor.getDependencyType().equals(javaUtilOptionalClass)) {
		return new OptionalDependencyFactory().createOptionalDependency(descriptor, beanName);
	}
	else if (ObjectFactory.class == descriptor.getDependencyType()) {
		return new DependencyObjectFactory(descriptor, beanName);
	}
	else if (javaxInjectProviderClass == descriptor.getDependencyType()) {
		return new DependencyProviderFactory().createDependencyProvider(descriptor, beanName);
	}
	else {
		Object result = getAutowireCandidateResolver().getLazyResolutionProxyIfNecessary(descriptor, beanName);
		if (result == null) {
			result = doResolveDependency(descriptor, beanName, autowiredBeanNames, typeConverter);
		}
		return result;
	}
}
 
Example #3
Source File: DefaultListableBeanFactory.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Determine whether the specified bean definition qualifies as an autowire candidate,
 * to be injected into other beans which declare a dependency of matching type.
 * @param beanName the name of the bean definition to check
 * @param descriptor the descriptor of the dependency to resolve
 * @param resolver the AutowireCandidateResolver to use for the actual resolution algorithm
 * @return whether the bean should be considered as autowire candidate
 */
protected boolean isAutowireCandidate(String beanName, DependencyDescriptor descriptor, AutowireCandidateResolver resolver)
		throws NoSuchBeanDefinitionException {

	String beanDefinitionName = BeanFactoryUtils.transformedBeanName(beanName);
	if (containsBeanDefinition(beanDefinitionName)) {
		return isAutowireCandidate(beanName, getMergedLocalBeanDefinition(beanDefinitionName), descriptor, resolver);
	}
	else if (containsSingleton(beanName)) {
		return isAutowireCandidate(beanName, new RootBeanDefinition(getType(beanName)), descriptor, resolver);
	}

	BeanFactory parent = getParentBeanFactory();
	if (parent instanceof DefaultListableBeanFactory) {
		// No bean definition found in this factory -> delegate to parent.
		return ((DefaultListableBeanFactory) parent).isAutowireCandidate(beanName, descriptor, resolver);
	}
	else if (parent instanceof ConfigurableListableBeanFactory) {
		// If no DefaultListableBeanFactory, can't pass the resolver along.
		return ((ConfigurableListableBeanFactory) parent).isAutowireCandidate(beanName, descriptor);
	}
	else {
		return true;
	}
}
 
Example #4
Source File: CubaDefaultListableBeanFactory.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Override
public Object resolveDependency(DependencyDescriptor descriptor, String beanName, Set<String> autowiredBeanNames,
                                TypeConverter typeConverter) throws BeansException {
    Field field = descriptor.getField();

    if (field != null && Logger.class == descriptor.getDependencyType()) {
        return LoggerFactory.getLogger(getDeclaringClass(descriptor));
    }

    if (field != null && Config.class.isAssignableFrom(field.getType())) {
        return getConfig(field.getType());
    }
    MethodParameter methodParam = descriptor.getMethodParameter();
    if (methodParam != null && Config.class.isAssignableFrom(methodParam.getParameterType())) {
        return getConfig(methodParam.getParameterType());
    }
    return super.resolveDependency(descriptor, beanName, autowiredBeanNames, typeConverter);
}
 
Example #5
Source File: DefaultListableBeanFactory.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Determine the autowire candidate in the given set of beans.
 * <p>Looks for {@code @Primary} and {@code @Priority} (in that order).
 * @param candidateBeans a Map of candidate names and candidate instances
 * that match the required type, as returned by {@link #findAutowireCandidates}
 * @param descriptor the target dependency to match against
 * @return the name of the autowire candidate, or {@code null} if none found
 */
protected String determineAutowireCandidate(Map<String, Object> candidateBeans, DependencyDescriptor descriptor) {
	Class<?> requiredType = descriptor.getDependencyType();
	String primaryCandidate = determinePrimaryCandidate(candidateBeans, requiredType);
	if (primaryCandidate != null) {
		return primaryCandidate;
	}
	String priorityCandidate = determineHighestPriorityCandidate(candidateBeans, requiredType);
	if (priorityCandidate != null) {
		return priorityCandidate;
	}
	// Fallback
	for (Map.Entry<String, Object> entry : candidateBeans.entrySet()) {
		String candidateBeanName = entry.getKey();
		Object beanInstance = entry.getValue();
		if ((beanInstance != null && this.resolvableDependencies.containsValue(beanInstance)) ||
				matchesBeanName(candidateBeanName, descriptor.getDependencyName())) {
			return candidateBeanName;
		}
	}
	return null;
}
 
Example #6
Source File: DefaultListableBeanFactory.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Determine the autowire candidate in the given set of beans.
 * <p>Looks for {@code @Primary} and {@code @Priority} (in that order).
 * @param candidates a Map of candidate names and candidate instances
 * that match the required type, as returned by {@link #findAutowireCandidates}
 * @param descriptor the target dependency to match against
 * @return the name of the autowire candidate, or {@code null} if none found
 */
@Nullable
protected String determineAutowireCandidate(Map<String, Object> candidates, DependencyDescriptor descriptor) {
	Class<?> requiredType = descriptor.getDependencyType();
	String primaryCandidate = determinePrimaryCandidate(candidates, requiredType);
	if (primaryCandidate != null) {
		return primaryCandidate;
	}
	String priorityCandidate = determineHighestPriorityCandidate(candidates, requiredType);
	if (priorityCandidate != null) {
		return priorityCandidate;
	}
	// Fallback
	for (Map.Entry<String, Object> entry : candidates.entrySet()) {
		String candidateName = entry.getKey();
		Object beanInstance = entry.getValue();
		if ((beanInstance != null && this.resolvableDependencies.containsValue(beanInstance)) ||
				matchesBeanName(candidateName, descriptor.getDependencyName())) {
			return candidateName;
		}
	}
	return null;
}
 
Example #7
Source File: DefaultListableBeanFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object getIfUnique() throws BeansException {
	DependencyDescriptor descriptorToUse = new DependencyDescriptor(descriptor) {
		@Override
		public boolean isRequired() {
			return false;
		}
		@Override
		public Object resolveNotUnique(Class<?> type, Map<String, Object> matchingBeans) {
			return null;
		}
	};
	if (this.optional) {
		return new OptionalDependencyFactory().createOptionalDependency(descriptorToUse, this.beanName);
	}
	else {
		return doResolveDependency(descriptorToUse, this.beanName, null, null);
	}
}
 
Example #8
Source File: DefaultListableBeanFactory.java    From kfs with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
   public boolean isAutowireCandidate(String beanName, DependencyDescriptor descriptor)
		throws NoSuchBeanDefinitionException {

	// Consider FactoryBeans as autowiring candidates.
	boolean isFactoryBean = (descriptor != null && descriptor.getDependencyType() != null &&
			FactoryBean.class.isAssignableFrom(descriptor.getDependencyType()));
	if (isFactoryBean) {
		beanName = BeanFactoryUtils.transformedBeanName(beanName);
	}

	if (containsBeanDefinition(beanName)) {
		return isAutowireCandidate(beanName, getMergedLocalBeanDefinition(beanName), descriptor);
	}
	else if (containsSingleton(beanName)) {
		return isAutowireCandidate(beanName, new RootBeanDefinition(getType(beanName)), descriptor);
	}
	else if (getParentBeanFactory() instanceof ConfigurableListableBeanFactory) {
		// No bean definition found in this factory -> delegate to parent.
		return ((ConfigurableListableBeanFactory) getParentBeanFactory()).isAutowireCandidate(beanName, descriptor);
	}
	else {
		return true;
	}
}
 
Example #9
Source File: DefaultListableBeanFactory.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
@Nullable
public Object getIfUnique() throws BeansException {
	DependencyDescriptor descriptorToUse = new DependencyDescriptor(this.descriptor) {
		@Override
		public boolean isRequired() {
			return false;
		}
		@Override
		@Nullable
		public Object resolveNotUnique(ResolvableType type, Map<String, Object> matchingBeans) {
			return null;
		}
	};
	if (this.optional) {
		return createOptionalDependency(descriptorToUse, this.beanName);
	}
	else {
		return doResolveDependency(descriptorToUse, this.beanName, null, null);
	}
}
 
Example #10
Source File: DefaultListableBeanFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Determine the autowire candidate in the given set of beans.
 * <p>Looks for {@code @Primary} and {@code @Priority} (in that order).
 * @param candidates a Map of candidate names and candidate instances
 * that match the required type, as returned by {@link #findAutowireCandidates}
 * @param descriptor the target dependency to match against
 * @return the name of the autowire candidate, or {@code null} if none found
 */
protected String determineAutowireCandidate(Map<String, Object> candidates, DependencyDescriptor descriptor) {
	Class<?> requiredType = descriptor.getDependencyType();
	String primaryCandidate = determinePrimaryCandidate(candidates, requiredType);
	if (primaryCandidate != null) {
		return primaryCandidate;
	}
	String priorityCandidate = determineHighestPriorityCandidate(candidates, requiredType);
	if (priorityCandidate != null) {
		return priorityCandidate;
	}
	// Fallback
	for (Map.Entry<String, Object> entry : candidates.entrySet()) {
		String candidateName = entry.getKey();
		Object beanInstance = entry.getValue();
		if ((beanInstance != null && this.resolvableDependencies.containsValue(beanInstance)) ||
				matchesBeanName(candidateName, descriptor.getDependencyName())) {
			return candidateName;
		}
	}
	return null;
}
 
Example #11
Source File: DefaultListableBeanFactory.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
@Nullable
public Object getIfUnique() throws BeansException {
	DependencyDescriptor descriptorToUse = new DependencyDescriptor(this.descriptor) {
		@Override
		public boolean isRequired() {
			return false;
		}
		@Override
		@Nullable
		public Object resolveNotUnique(ResolvableType type, Map<String, Object> matchingBeans) {
			return null;
		}
	};
	if (this.optional) {
		return createOptionalDependency(descriptorToUse, this.beanName);
	}
	else {
		return doResolveDependency(descriptorToUse, this.beanName, null, null);
	}
}
 
Example #12
Source File: DefaultListableBeanFactory.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
@Nullable
public Object resolveDependency(DependencyDescriptor descriptor, @Nullable String requestingBeanName,
		@Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter) throws BeansException {

	descriptor.initParameterNameDiscovery(getParameterNameDiscoverer());
	if (Optional.class == descriptor.getDependencyType()) {
		return createOptionalDependency(descriptor, requestingBeanName);
	}
	else if (ObjectFactory.class == descriptor.getDependencyType() ||
			ObjectProvider.class == descriptor.getDependencyType()) {
		return new DependencyObjectProvider(descriptor, requestingBeanName);
	}
	else if (javaxInjectProviderClass == descriptor.getDependencyType()) {
		return new Jsr330Factory().createDependencyProvider(descriptor, requestingBeanName);
	}
	else {
		Object result = getAutowireCandidateResolver().getLazyResolutionProxyIfNecessary(
				descriptor, requestingBeanName);
		if (result == null) {
			result = doResolveDependency(descriptor, requestingBeanName, autowiredBeanNames, typeConverter);
		}
		return result;
	}
}
 
Example #13
Source File: QualifierAnnotationAutowireBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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));
}
 
Example #14
Source File: DefaultListableBeanFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Determine whether the specified bean definition qualifies as an autowire candidate,
 * to be injected into other beans which declare a dependency of matching type.
 * @param beanName the name of the bean definition to check
 * @param mbd the merged bean definition to check
 * @param descriptor the descriptor of the dependency to resolve
 * @param resolver the AutowireCandidateResolver to use for the actual resolution algorithm
 * @return whether the bean should be considered as autowire candidate
 */
protected boolean isAutowireCandidate(String beanName, RootBeanDefinition mbd,
		DependencyDescriptor descriptor, AutowireCandidateResolver resolver) {

	String beanDefinitionName = BeanFactoryUtils.transformedBeanName(beanName);
	resolveBeanClass(mbd, beanDefinitionName);
	if (mbd.isFactoryMethodUnique) {
		boolean resolve;
		synchronized (mbd.constructorArgumentLock) {
			resolve = (mbd.resolvedConstructorOrFactoryMethod == null);
		}
		if (resolve) {
			new ConstructorResolver(this).resolveFactoryMethodIfPossible(mbd);
		}
	}
	return resolver.isAutowireCandidate(
			new BeanDefinitionHolder(mbd, beanName, getAliases(beanDefinitionName)), descriptor);
}
 
Example #15
Source File: ProviderAwareListableBeanFactory.java    From arctic-sea with Apache License 2.0 6 votes vote down vote up
@Override
protected Map<String, Object> findAutowireCandidates(String beanName, Class<?> requiredType,
                                                     DependencyDescriptor descriptor) {

    if (!requiredType.equals(Provider.class)) {
        return super.findAutowireCandidates(beanName, requiredType, descriptor);
    }

    DependencyDescriptor providedDescriptor = new DependencyDescriptor(descriptor);
    providedDescriptor.increaseNestingLevel();
    Class<?> type = providedDescriptor.getDependencyType();
    Set<String> candidates = findAutowireCandidates(beanName, type, providedDescriptor).keySet();
    return candidates.stream()
            .collect(toMap(Function.identity(), name -> new DependencyProvider(this, descriptor, name)));

}
 
Example #16
Source File: DefaultListableBeanFactory.java    From blog_demos with Apache License 2.0 6 votes vote down vote up
@Override
public Object resolveDependency(DependencyDescriptor descriptor, String beanName,
		Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException {

	descriptor.initParameterNameDiscovery(getParameterNameDiscoverer());
	if (descriptor.getDependencyType().equals(ObjectFactory.class)) {
		return new DependencyObjectFactory(descriptor, beanName);
	}
	else if (descriptor.getDependencyType().equals(javaxInjectProviderClass)) {
		return new DependencyProviderFactory().createDependencyProvider(descriptor, beanName);
	}
	else {
		Object result = getAutowireCandidateResolver().getLazyResolutionProxyIfNecessary(descriptor, beanName);
		if (result == null) {
			result = doResolveDependency(descriptor, beanName, autowiredBeanNames, typeConverter);
		}
		return result;
	}
}
 
Example #17
Source File: QualifierAnnotationAutowireCandidateResolver.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Determine whether the provided bean definition is an autowire candidate.
 * <p>To be considered a candidate the bean's <em>autowire-candidate</em>
 * attribute must not have been set to 'false'. Also, if an annotation on
 * the field or parameter to be autowired is recognized by this bean factory
 * as a <em>qualifier</em>, the bean must 'match' against the annotation as
 * well as any attributes it may contain. The bean definition must contain
 * the same qualifier or match by meta attributes. A "value" attribute will
 * fallback to match against the bean name or an alias if a qualifier or
 * attribute does not match.
 * @see Qualifier
 */
@Override
public boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) {
	boolean match = super.isAutowireCandidate(bdHolder, descriptor);
	if (match) {
		match = checkQualifiers(bdHolder, descriptor.getAnnotations());
		if (match) {
			MethodParameter methodParam = descriptor.getMethodParameter();
			if (methodParam != null) {
				Method method = methodParam.getMethod();
				if (method == null || void.class == method.getReturnType()) {
					match = checkQualifiers(bdHolder, methodParam.getMethodAnnotations());
				}
			}
		}
	}
	return match;
}
 
Example #18
Source File: QualifierAnnotationAutowireBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #19
Source File: CubaDefaultListableBeanFactory.java    From cuba with Apache License 2.0 5 votes vote down vote up
protected Class<?> getDeclaringClass(DependencyDescriptor descriptor) {
    MethodParameter methodParameter = descriptor.getMethodParameter();
    if (methodParameter != null) {
        return methodParameter.getDeclaringClass();
    }
    Field field = descriptor.getField();
    if (field != null) {
        return field.getDeclaringClass();
    }
    throw new AssertionError("Injection must be into a method parameter or field.");
}
 
Example #20
Source File: CustomAutowireConfigurerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) {
	if (!bdHolder.getBeanDefinition().isAutowireCandidate()) {
		return false;
	}
	if (!bdHolder.getBeanName().matches("[a-z-]+")) {
		return false;
	}
	if (bdHolder.getBeanDefinition().getAttribute("priority").equals("1")) {
		return true;
	}
	return false;
}
 
Example #21
Source File: DefaultListableBeanFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Determine whether the specified bean definition qualifies as an autowire candidate,
 * to be injected into other beans which declare a dependency of matching type.
 * @param beanName the name of the bean definition to check
 * @param mbd the merged bean definition to check
 * @param descriptor the descriptor of the dependency to resolve
 * @param resolver the AutowireCandidateResolver to use for the actual resolution algorithm
 * @return whether the bean should be considered as autowire candidate
 */
protected boolean isAutowireCandidate(String beanName, RootBeanDefinition mbd,
		DependencyDescriptor descriptor, AutowireCandidateResolver resolver) {

	String beanDefinitionName = BeanFactoryUtils.transformedBeanName(beanName);
	resolveBeanClass(mbd, beanDefinitionName);
	if (mbd.isFactoryMethodUnique && mbd.factoryMethodToIntrospect == null) {
		new ConstructorResolver(this).resolveFactoryMethodIfPossible(mbd);
	}
	return resolver.isAutowireCandidate(
			new BeanDefinitionHolder(mbd, beanName, getAliases(beanDefinitionName)), descriptor);
}
 
Example #22
Source File: ResourceConfiguration.java    From mcspring-boot with MIT License 5 votes vote down vote up
@Scope(SCOPE_PROTOTYPE)
@Bean
public Instance configInstance(InjectionPoint injectionPoint, ConversionService conversionService, Environment environment) {
    val dynamicValue = injectionPoint.getAnnotation(DynamicValue.class);
    val type = ((DependencyDescriptor) injectionPoint).getResolvableType().getGeneric(0);

    return new Instance(environment, dynamicValue.value(), conversionService, type.getRawClass());
}
 
Example #23
Source File: DefaultListableBeanFactory.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Determine whether the specified bean definition qualifies as an autowire candidate,
 * to be injected into other beans which declare a dependency of matching type.
 * @param beanName the name of the bean definition to check
 * @param mbd the merged bean definition to check
 * @param descriptor the descriptor of the dependency to resolve
 * @return whether the bean should be considered as autowire candidate
 */
protected boolean isAutowireCandidate(String beanName, RootBeanDefinition mbd, DependencyDescriptor descriptor) {
	resolveBeanClass(mbd, beanName);
	if (mbd.isFactoryMethodUnique) {
		boolean resolve;
		synchronized (mbd.constructorArgumentLock) {
			resolve = (mbd.resolvedConstructorOrFactoryMethod == null);
		}
		if (resolve) {
			new ConstructorResolver(this).resolveFactoryMethodIfPossible(mbd);
		}
	}
	return getAutowireCandidateResolver().isAutowireCandidate(
			new BeanDefinitionHolder(mbd, beanName, getAliases(beanName)), descriptor);
}
 
Example #24
Source File: DefaultListableBeanFactory.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
   public Object resolveDependency(DependencyDescriptor descriptor, String beanName,
		Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException  {

	descriptor.initParameterNameDiscovery(getParameterNameDiscoverer());
	if (descriptor.getDependencyType().equals(ObjectFactory.class)) {
		return new DependencyObjectFactory(descriptor, beanName);
	}
	else if (descriptor.getDependencyType().equals(javaxInjectProviderClass)) {
		return new DependencyProviderFactory().createDependencyProvider(descriptor, beanName);
	}
	else {
		return doResolveDependency(descriptor, descriptor.getDependencyType(), beanName, autowiredBeanNames, typeConverter);
	}
}
 
Example #25
Source File: CommonAnnotationBeanPostProcessor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Build a DependencyDescriptor for the underlying field/method.
 */
public final DependencyDescriptor getDependencyDescriptor() {
	if (this.isField) {
		return new LookupDependencyDescriptor((Field) this.member, this.lookupType);
	}
	else {
		return new LookupDependencyDescriptor((Method) this.member, this.lookupType);
	}
}
 
Example #26
Source File: DefaultListableBeanFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Raise a NoSuchBeanDefinitionException or BeanNotOfRequiredTypeException
 * for an unresolvable dependency.
 */
private void raiseNoMatchingBeanFound(
		Class<?> type, ResolvableType resolvableType, DependencyDescriptor descriptor) throws BeansException {

	checkBeanNotOfRequiredType(type, descriptor);

	throw new NoSuchBeanDefinitionException(resolvableType,
			"expected at least 1 bean which qualifies as autowire candidate. " +
			"Dependency annotations: " + ObjectUtils.nullSafeToString(descriptor.getAnnotations()));
}
 
Example #27
Source File: ParameterAutowireUtils.java    From tutorials with MIT License 5 votes vote down vote up
public static Object resolveDependency(Parameter parameter, Class<?> containingClass, ApplicationContext applicationContext) {

        boolean required = findMergedAnnotation(parameter, Autowired.class).map(Autowired::required)
            .orElse(true);
        MethodParameter methodParameter = (parameter.getDeclaringExecutable() instanceof Method ? MethodParameterFactory.createSynthesizingMethodParameter(parameter) : MethodParameterFactory.createMethodParameter(parameter));
        DependencyDescriptor descriptor = new DependencyDescriptor(methodParameter, required);
        descriptor.setContainingClass(containingClass);

        return applicationContext.getAutowireCapableBeanFactory()
            .resolveDependency(descriptor, null);
    }
 
Example #28
Source File: RemoteAutowireCandidateResolver.java    From Ratel with Apache License 2.0 5 votes vote down vote up
@Override
protected Object buildLazyResolutionProxy(DependencyDescriptor descriptor, String beanName) {

    Collection<String> annotationsType = getAnnotationsTypes(descriptor);
    if (annotationsType.contains(Discover.class.getName())) {
        if (descriptor.getDependencyType().equals(EventCannon.class)) {
            return produceEventCannonProxy();
        } else {
            RetryPolicyConfig retryPolicy = null;
            Optional<Annotation> retryPolicyAnn = getAnnotationWithType(descriptor, RetryPolicy.class);
            if (retryPolicyAnn.isPresent()) {
                retryPolicy = RetryPolicyConfig.fromRetryPolicy((RetryPolicy) retryPolicyAnn.get());
            }
            TimeoutConfig timeout = null;
            Optional<Annotation> timeoutAnn = getAnnotationWithType(descriptor, Timeout.class);
            if (timeoutAnn.isPresent()) {
                timeout = TimeoutConfig.fromTimeout((Timeout) timeoutAnn.get());
            }

            boolean useCache = annotationsType.contains(Cachable.class.getName());

            return ratelClientProducer.produceServiceProxy(descriptor.getDependencyType(), useCache, retryPolicy,
                    timeout);
        }
    }

    return super.buildLazyResolutionProxy(descriptor, beanName);
}
 
Example #29
Source File: QualifierAnnotationAutowireBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testAutowireCandidateExplicitlyFalseWithIrrelevantDescriptor() throws Exception {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	ConstructorArgumentValues cavs = new ConstructorArgumentValues();
	cavs.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition rbd = new RootBeanDefinition(Person.class, cavs, null);
	rbd.setAutowireCandidate(false);
	lbf.registerBeanDefinition(JUERGEN, rbd);
	assertFalse(lbf.isAutowireCandidate(JUERGEN, null));
	assertFalse(lbf.isAutowireCandidate(JUERGEN,
			new DependencyDescriptor(Person.class.getDeclaredField("name"), false)));
	assertFalse(lbf.isAutowireCandidate(JUERGEN,
			new DependencyDescriptor(Person.class.getDeclaredField("name"), true)));
}
 
Example #30
Source File: InjectBeanPostProcessor.java    From spring-boot-starter-dubbo with Apache License 2.0 5 votes vote down vote up
/**
 * Resolve the specified cached method argument or field value.
 */
private Object resolvedCachedArgument(String beanName, Object cachedArgument) {
	if (cachedArgument instanceof DependencyDescriptor) {
		DependencyDescriptor descriptor = (DependencyDescriptor) cachedArgument;
		return this.beanFactory.resolveDependency(descriptor, beanName, null, null);
	}
	else {
		return cachedArgument;
	}
}