Class AbstractInjectionPointDependencyResolver
- All Implemented Interfaces:
InjectionPointDependencyResolver,org.springframework.beans.factory.Aware,org.springframework.beans.factory.BeanFactoryAware
- Direct Known Subclasses:
AnnotatedInjectionPointDependencyResolver,BeanMethodInjectionPointDependencyResolver,ConstructionInjectionPointDependencyResolver
InjectionPointDependencyResolver.
This class provides a foundation for resolving dependencies at injection points within Spring-managed beans.
It handles common tasks such as bean factory awareness, dependency type resolution, and logging using the
Logger interface.
Key Responsibilities
- Tracking and resolving dependencies based on injection points (fields, methods, constructors).
- Filtering resolvable dependency types via the configured
ResolvableDependencyTypeFilter. - Providing consistent logging capabilities through the injected logger.
- Supporting custom resolution logic by allowing subclasses to implement specific strategies.
Example Usage
Basic Implementation
public class MyDependencyResolver extends AbstractInjectionPointDependencyResolver {
// Custom implementation details here
}
Resolving Dependencies from a Field
public void resolve(Field field, ConfigurableListableBeanFactory beanFactory, Set<String> dependentBeanNames) {
String dependentBeanName = resolveDependentBeanNameByName(field, beanFactory);
if (dependentBeanName == null) {
resolveDependentBeanNamesByType(field::getGenericType, beanFactory, dependentBeanNames);
} else {
dependentBeanNames.add(dependentBeanName);
}
}
Resolving Dependencies from a Method Parameter
public void resolve(Parameter parameter, ConfigurableListableBeanFactory beanFactory, Set<String> dependentBeanNames) {
String dependentBeanName = resolveDependentBeanNameByName(parameter, beanFactory);
if (dependentBeanName == null) {
resolveDependentBeanNamesByType(parameter::getParameterizedType, beanFactory, dependentBeanNames);
} else {
dependentBeanNames.add(dependentBeanName);
}
}
- Since:
- 1.0.0
- Author:
- Mercy
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected org.springframework.beans.factory.support.AutowireCandidateResolverprotected final io.microsphere.logging.Loggerprotected ResolvableDependencyTypeFilter -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidresolve(Constructor constructor, org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, Set<String> dependentBeanNames) Resolve the dependent bean names from the givenConstructorinjection point.voidresolve(Field field, org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, Set<String> dependentBeanNames) Resolve the dependent bean names from the givenFieldinjection point.voidresolve(Method method, org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, Set<String> dependentBeanNames) Resolve the dependent bean names from the givenMethodinjection point.voidresolve(Parameter parameter, org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, Set<String> dependentBeanNames) Resolve the dependent bean names from the givenParameterinjection point.protected StringresolveDependentBeanNameByName(Field field, org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory) Resolve the dependent bean name by name for the givenFieldusing theAutowireCandidateResolver's suggested value.protected StringresolveDependentBeanNameByName(Parameter parameter, org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory) Resolve the dependent bean name by name for the givenParameterusing theAutowireCandidateResolver's suggested value.protected StringresolveDependentBeanNameByName(org.springframework.beans.factory.config.DependencyDescriptor dependencyDescriptor) Resolve the dependent bean name by name from the givenDependencyDescriptorusing theAutowireCandidateResolver's suggested value.protected voidresolveDependentBeanNamesByType(Supplier<Type> typeSupplier, org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, Set<String> dependentBeanNames) Resolve dependent bean names by type from the given type supplier.protected Class<?>resolveDependentType(Type type) voidsetBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory) Initialize this resolver by extracting theResolvableDependencyTypeFilterandAutowireCandidateResolverfrom the givenBeanFactory.
-
Field Details
-
logger
protected final io.microsphere.logging.Logger logger -
resolvableDependencyTypeFilter
-
autowireCandidateResolver
@Nonnull protected org.springframework.beans.factory.support.AutowireCandidateResolver autowireCandidateResolver
-
-
Constructor Details
-
AbstractInjectionPointDependencyResolver
public AbstractInjectionPointDependencyResolver()
-
-
Method Details
-
resolve
public void resolve(Field field, org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, Set<String> dependentBeanNames) Resolve the dependent bean names from the givenFieldinjection point.First attempts to resolve by name using the
AutowireCandidateResolver. If no name is suggested, falls back to resolving by the field's generic type.Example Usage
AbstractInjectionPointDependencyResolver resolver = ...; resolver.setBeanFactory(beanFactory); Field field = ReflectionUtils.findField(MyConfig.class, "myDependency"); Set<String> dependentBeanNames = new LinkedHashSet<>(); resolver.resolve(field, beanFactory, dependentBeanNames); // dependentBeanNames now contains the bean names matching the field- Specified by:
resolvein interfaceInjectionPointDependencyResolver- Parameters:
field- the field injection point to resolvebeanFactory- theConfigurableListableBeanFactoryto look up beansdependentBeanNames- the set to collect resolved dependent bean names into
-
resolve
public void resolve(Method method, org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, Set<String> dependentBeanNames) Resolve the dependent bean names from the givenMethodinjection point.Iterates over each parameter of the method and delegates resolution to
resolve(Parameter, ConfigurableListableBeanFactory, Set). Methods with no parameters are ignored.Example Usage
AbstractInjectionPointDependencyResolver resolver = ...; resolver.setBeanFactory(beanFactory); Method method = MethodUtils.findMethod(MyConfig.class, "user", MyDependency[].class); Set<String> dependentBeanNames = new LinkedHashSet<>(); resolver.resolve(method, beanFactory, dependentBeanNames); // dependentBeanNames now contains bean names matching the method parameters- Specified by:
resolvein interfaceInjectionPointDependencyResolver- Parameters:
method- the method injection point to resolvebeanFactory- theConfigurableListableBeanFactoryto look up beansdependentBeanNames- the set to collect resolved dependent bean names into
-
resolve
public void resolve(Constructor constructor, org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, Set<String> dependentBeanNames) Resolve the dependent bean names from the givenConstructorinjection point.Iterates over each parameter of the constructor and delegates resolution to
resolve(Parameter, ConfigurableListableBeanFactory, Set). Constructors with no parameters are ignored.Example Usage
AbstractInjectionPointDependencyResolver resolver = ...; resolver.setBeanFactory(beanFactory); Constructor<?> constructor = ConstructorUtils.findConstructor(MyConfig.class, Map.class); Set<String> dependentBeanNames = new LinkedHashSet<>(); resolver.resolve(constructor, beanFactory, dependentBeanNames); // dependentBeanNames now contains bean names matching the constructor parameters- Specified by:
resolvein interfaceInjectionPointDependencyResolver- Parameters:
constructor- the constructor injection point to resolvebeanFactory- theConfigurableListableBeanFactoryto look up beansdependentBeanNames- the set to collect resolved dependent bean names into
-
resolve
public void resolve(Parameter parameter, org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, Set<String> dependentBeanNames) Resolve the dependent bean names from the givenParameterinjection point.First attempts to resolve by name using the
AutowireCandidateResolver. If no name is suggested, falls back to resolving by the parameter's parameterized type.Example Usage
AbstractInjectionPointDependencyResolver resolver = ...; resolver.setBeanFactory(beanFactory); Method method = MethodUtils.findMethod(MyConfig.class, "user", MyDependency[].class); Parameter parameter = method.getParameters()[0]; Set<String> dependentBeanNames = new LinkedHashSet<>(); resolver.resolve(parameter, beanFactory, dependentBeanNames); // dependentBeanNames now contains bean names matching the parameter type- Specified by:
resolvein interfaceInjectionPointDependencyResolver- Parameters:
parameter- the parameter injection point to resolvebeanFactory- theConfigurableListableBeanFactoryto look up beansdependentBeanNames- the set to collect resolved dependent bean names into
-
setBeanFactory
public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory) throws org.springframework.beans.BeansException Initialize this resolver by extracting theResolvableDependencyTypeFilterandAutowireCandidateResolverfrom the givenBeanFactory.Example Usage
AbstractInjectionPointDependencyResolver resolver = new MyDependencyResolver(); resolver.setBeanFactory(beanFactory); // resolver is now ready to resolve injection point dependencies- Specified by:
setBeanFactoryin interfaceorg.springframework.beans.factory.BeanFactoryAware- Parameters:
beanFactory- the owningBeanFactory- Throws:
org.springframework.beans.BeansException- if initialization fails
-
resolveDependentBeanNameByName
protected String resolveDependentBeanNameByName(Field field, org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory) Resolve the dependent bean name by name for the givenFieldusing theAutowireCandidateResolver's suggested value.Example Usage
Field field = ReflectionUtils.findField(MyConfig.class, "myService"); String beanName = resolveDependentBeanNameByName(field, beanFactory); // beanName is the suggested name, or null if none was suggested- Parameters:
field- the field to resolve the dependent bean name frombeanFactory- theConfigurableListableBeanFactory(unused but available for subclass overrides)- Returns:
- the suggested bean name, or
nullif no name was suggested
-
resolveDependentBeanNameByName
protected String resolveDependentBeanNameByName(Parameter parameter, org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory) Resolve the dependent bean name by name for the givenParameterusing theAutowireCandidateResolver's suggested value.Example Usage
Parameter parameter = method.getParameters()[0]; String beanName = resolveDependentBeanNameByName(parameter, beanFactory); // beanName is the suggested name, or null if none was suggested- Parameters:
parameter- the parameter to resolve the dependent bean name frombeanFactory- theConfigurableListableBeanFactory(unused but available for subclass overrides)- Returns:
- the suggested bean name, or
nullif no name was suggested
-
resolveDependentBeanNameByName
protected String resolveDependentBeanNameByName(org.springframework.beans.factory.config.DependencyDescriptor dependencyDescriptor) Resolve the dependent bean name by name from the givenDependencyDescriptorusing theAutowireCandidateResolver's suggested value.Example Usage
DependencyDescriptor descriptor = new DependencyDescriptor(field, true, false); String beanName = resolveDependentBeanNameByName(descriptor); // beanName is non-null only if the resolver suggests a String value- Parameters:
dependencyDescriptor- theDependencyDescriptorto resolve the bean name from- Returns:
- the suggested bean name as a
String, ornullif the suggested value is not aStringor isnull
-
resolveDependentBeanNamesByType
protected void resolveDependentBeanNamesByType(Supplier<Type> typeSupplier, org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, Set<String> dependentBeanNames) Resolve dependent bean names by type from the given type supplier.The resolved type is checked against the
ResolvableDependencyTypeFilter; if it is a resolvable dependency type (e.g.,BeanFactory,ApplicationContext), it is skipped. Otherwise, all bean names matching the type are added to the result set.Example Usage
Set<String> dependentBeanNames = new LinkedHashSet<>(); resolveDependentBeanNamesByType(field::getGenericType, beanFactory, dependentBeanNames); // dependentBeanNames now contains all bean names matching the field's type- Parameters:
typeSupplier- a supplier providing theTypeto resolve bean names forbeanFactory- theConfigurableListableBeanFactoryto look up beansdependentBeanNames- the set to collect resolved dependent bean names into
-
resolveDependentType
Resolve the dependentClassfrom the givenType.Handles plain classes (including array component types), and parameterized types such as
ObjectProvider<SomeBean>,List<SomeBean>,Map<String, SomeBean>, etc. For parameterized types, the last type argument is recursively resolved.Example Usage
// For a field declared as Optional<List<MyBean>> Type type = field.getGenericType(); Class<?> dependentType = resolveDependentType(type); // dependentType == MyBean.class
-