Java Code Examples for org.springframework.beans.factory.config.DependencyDescriptor#setContainingClass()

The following examples show how to use org.springframework.beans.factory.config.DependencyDescriptor#setContainingClass() . 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: 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 2
Source File: AutowiredAnnotationBeanPostProcessor.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected void inject(Object bean, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable {
	Field field = (Field) this.member;
	Object value;
	if (this.cached) {
		value = resolvedCachedArgument(beanName, this.cachedFieldValue);
	}
	else {
		DependencyDescriptor desc = new DependencyDescriptor(field, this.required);
		desc.setContainingClass(bean.getClass());
		Set<String> autowiredBeanNames = new LinkedHashSet<>(1);
		Assert.state(beanFactory != null, "No BeanFactory available");
		TypeConverter typeConverter = beanFactory.getTypeConverter();
		try {
			value = beanFactory.resolveDependency(desc, beanName, autowiredBeanNames, typeConverter);
		}
		catch (BeansException ex) {
			throw new UnsatisfiedDependencyException(null, beanName, new InjectionPoint(field), ex);
		}
		synchronized (this) {
			if (!this.cached) {
				if (value != null || this.required) {
					this.cachedFieldValue = desc;
					registerDependentBeans(beanName, autowiredBeanNames);
					if (autowiredBeanNames.size() == 1) {
						String autowiredBeanName = autowiredBeanNames.iterator().next();
						if (beanFactory.containsBean(autowiredBeanName) &&
								beanFactory.isTypeMatch(autowiredBeanName, field.getType())) {
							this.cachedFieldValue = new ShortcutDependencyDescriptor(
									desc, autowiredBeanName, field.getType());
						}
					}
				}
				else {
					this.cachedFieldValue = null;
				}
				this.cached = true;
			}
		}
	}
	if (value != null) {
		ReflectionUtils.makeAccessible(field);
		field.set(bean, value);
	}
}
 
Example 3
Source File: ParameterResolutionDelegate.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * 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 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 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);
}
 
Example 4
Source File: AutowiredAnnotationBeanPostProcessor.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
protected void inject(Object bean, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable {
	Field field = (Field) this.member;
	Object value;
	if (this.cached) {
		value = resolvedCachedArgument(beanName, this.cachedFieldValue);
	}
	else {
		DependencyDescriptor desc = new DependencyDescriptor(field, this.required);
		desc.setContainingClass(bean.getClass());
		Set<String> autowiredBeanNames = new LinkedHashSet<>(1);
		Assert.state(beanFactory != null, "No BeanFactory available");
		TypeConverter typeConverter = beanFactory.getTypeConverter();
		try {
			value = beanFactory.resolveDependency(desc, beanName, autowiredBeanNames, typeConverter);
		}
		catch (BeansException ex) {
			throw new UnsatisfiedDependencyException(null, beanName, new InjectionPoint(field), ex);
		}
		synchronized (this) {
			if (!this.cached) {
				if (value != null || this.required) {
					this.cachedFieldValue = desc;
					registerDependentBeans(beanName, autowiredBeanNames);
					if (autowiredBeanNames.size() == 1) {
						String autowiredBeanName = autowiredBeanNames.iterator().next();
						if (beanFactory.containsBean(autowiredBeanName) &&
								beanFactory.isTypeMatch(autowiredBeanName, field.getType())) {
							this.cachedFieldValue = new ShortcutDependencyDescriptor(
									desc, autowiredBeanName, field.getType());
						}
					}
				}
				else {
					this.cachedFieldValue = null;
				}
				this.cached = true;
			}
		}
	}
	if (value != null) {
		ReflectionUtils.makeAccessible(field);
		field.set(bean, value);
	}
}
 
Example 5
Source File: AutowiredAnnotationBeanPostProcessor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable {
	Field field = (Field) this.member;
	Object value;
	if (this.cached) {
		value = resolvedCachedArgument(beanName, this.cachedFieldValue);
	}
	else {
		DependencyDescriptor desc = new DependencyDescriptor(field, this.required);
		desc.setContainingClass(bean.getClass());
		Set<String> autowiredBeanNames = new LinkedHashSet<String>(1);
		TypeConverter typeConverter = beanFactory.getTypeConverter();
		try {
			value = beanFactory.resolveDependency(desc, beanName, autowiredBeanNames, typeConverter);
		}
		catch (BeansException ex) {
			throw new UnsatisfiedDependencyException(null, beanName, new InjectionPoint(field), ex);
		}
		synchronized (this) {
			if (!this.cached) {
				if (value != null || this.required) {
					this.cachedFieldValue = desc;
					registerDependentBeans(beanName, autowiredBeanNames);
					if (autowiredBeanNames.size() == 1) {
						String autowiredBeanName = autowiredBeanNames.iterator().next();
						if (beanFactory.containsBean(autowiredBeanName)) {
							if (beanFactory.isTypeMatch(autowiredBeanName, field.getType())) {
								this.cachedFieldValue = new ShortcutDependencyDescriptor(
										desc, autowiredBeanName, field.getType());
							}
						}
					}
				}
				else {
					this.cachedFieldValue = null;
				}
				this.cached = true;
			}
		}
	}
	if (value != null) {
		ReflectionUtils.makeAccessible(field);
		field.set(bean, value);
	}
}
 
Example 6
Source File: InjectBeanPostProcessor.java    From spring-boot-starter-dubbo with Apache License 2.0 4 votes vote down vote up
@Override
protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable {
	Field field = (Field) this.member;
	Object value;
	if (this.cached) {
		value = resolvedCachedArgument(beanName, this.cachedFieldValue);
	}
	else {
		DependencyDescriptor desc = new DependencyDescriptor(field, this.required);
		desc.setContainingClass(bean.getClass());
		Set<String> autowiredBeanNames = new LinkedHashSet<String>(1);
		TypeConverter typeConverter = beanFactory.getTypeConverter();
		try {
			value = beanFactory.resolveDependency(desc, beanName, autowiredBeanNames, typeConverter);
		}
		catch (BeansException ex) {
			throw new UnsatisfiedDependencyException(null, beanName, new InjectionPoint(field), ex);
		}
		synchronized (this) {
			if (!this.cached) {
				if (value != null || this.required) {
					this.cachedFieldValue = desc;
					registerDependentBeans(beanName, autowiredBeanNames);
					if (autowiredBeanNames.size() == 1) {
						String autowiredBeanName = autowiredBeanNames.iterator().next();
						if (beanFactory.containsBean(autowiredBeanName)) {
							if (beanFactory.isTypeMatch(autowiredBeanName, field.getType())) {
								this.cachedFieldValue = new ShortcutDependencyDescriptor(
										desc, autowiredBeanName, field.getType());
							}
						}
					}
				}
				else {
					this.cachedFieldValue = null;
				}
				this.cached = true;
			}
		}
	}
	if (value != null) {
		ReflectionUtils.makeAccessible(field);
		field.set(bean, value);
	}
}
 
Example 7
Source File: AutowiredAnnotationBeanPostProcessor.java    From blog_demos with Apache License 2.0 4 votes vote down vote up
@Override
protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable {
	Field field = (Field) this.member;
	try {
		Object value;
		if (this.cached) {
			value = resolvedCachedArgument(beanName, this.cachedFieldValue);
		}
		else {
			DependencyDescriptor desc = new DependencyDescriptor(field, this.required);
			desc.setContainingClass(bean.getClass());
			Set<String> autowiredBeanNames = new LinkedHashSet<String>(1);
			TypeConverter typeConverter = beanFactory.getTypeConverter();
			value = beanFactory.resolveDependency(desc, beanName, autowiredBeanNames, typeConverter);
			synchronized (this) {
				if (!this.cached) {
					if (value != null || this.required) {
						this.cachedFieldValue = desc;
						registerDependentBeans(beanName, autowiredBeanNames);
						if (autowiredBeanNames.size() == 1) {
							String autowiredBeanName = autowiredBeanNames.iterator().next();
							if (beanFactory.containsBean(autowiredBeanName)) {
								if (beanFactory.isTypeMatch(autowiredBeanName, field.getType())) {
									this.cachedFieldValue = new RuntimeBeanReference(autowiredBeanName);
								}
							}
						}
					}
					else {
						this.cachedFieldValue = null;
					}
					this.cached = true;
				}
			}
		}
		if (value != null) {
			ReflectionUtils.makeAccessible(field);
			field.set(bean, value);
		}
	}
	catch (Throwable ex) {
		throw new BeanCreationException("Could not autowire field: " + field, ex);
	}
}
 
Example 8
Source File: AutowiredAnnotationBeanPostProcessor.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable {
	Field field = (Field) this.member;
	try {
		Object value;
		if (this.cached) {
			value = resolvedCachedArgument(beanName, this.cachedFieldValue);
		}
		else {
			DependencyDescriptor desc = new DependencyDescriptor(field, this.required);
			desc.setContainingClass(bean.getClass());
			Set<String> autowiredBeanNames = new LinkedHashSet<String>(1);
			TypeConverter typeConverter = beanFactory.getTypeConverter();
			value = beanFactory.resolveDependency(desc, beanName, autowiredBeanNames, typeConverter);
			synchronized (this) {
				if (!this.cached) {
					if (value != null || this.required) {
						this.cachedFieldValue = desc;
						registerDependentBeans(beanName, autowiredBeanNames);
						if (autowiredBeanNames.size() == 1) {
							String autowiredBeanName = autowiredBeanNames.iterator().next();
							if (beanFactory.containsBean(autowiredBeanName)) {
								if (beanFactory.isTypeMatch(autowiredBeanName, field.getType())) {
									this.cachedFieldValue = new RuntimeBeanReference(autowiredBeanName);
								}
							}
						}
					}
					else {
						this.cachedFieldValue = null;
					}
					this.cached = true;
				}
			}
		}
		if (value != null) {
			ReflectionUtils.makeAccessible(field);
			field.set(bean, value);
		}
	}
	catch (Throwable ex) {
		throw new BeanCreationException("Could not autowire field: " + field, ex);
	}
}
 
Example 9
Source File: ParameterAutowireUtils.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * 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 parameterIndex the index of 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
 * @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
 * @see Autowired#required
 * @see SynthesizingMethodParameter#forParameter(Parameter)
 * @see AutowireCapableBeanFactory#resolveDependency(DependencyDescriptor, String)
 */
@Nullable
static Object resolveDependency(
		Parameter parameter, int parameterIndex, Class<?> containingClass, ApplicationContext applicationContext) {

	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 applicationContext.getAutowireCapableBeanFactory().resolveDependency(descriptor, null);
}
 
Example 10
Source File: ParameterAutowireUtils.java    From spring-test-junit5 with Apache License 2.0 3 votes vote down vote up
/**
 * 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 MethodParameterFactory#createSynthesizingMethodParameter(Parameter)
 * @see AutowireCapableBeanFactory#resolveDependency(DependencyDescriptor, String)
 */
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);
}