Class AbstractInjectionPointDependencyResolver

java.lang.Object
io.microsphere.spring.beans.factory.AbstractInjectionPointDependencyResolver
All Implemented Interfaces:
InjectionPointDependencyResolver, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanFactoryAware
Direct Known Subclasses:
AnnotatedInjectionPointDependencyResolver, BeanMethodInjectionPointDependencyResolver, ConstructionInjectionPointDependencyResolver

public abstract class AbstractInjectionPointDependencyResolver extends Object implements InjectionPointDependencyResolver, org.springframework.beans.factory.BeanFactoryAware
Abstract base class for implementing 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 Details

    • logger

      protected final io.microsphere.logging.Logger logger
    • resolvableDependencyTypeFilter

      @Nonnull protected ResolvableDependencyTypeFilter 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 given Field injection 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:
      resolve in interface InjectionPointDependencyResolver
      Parameters:
      field - the field injection point to resolve
      beanFactory - the ConfigurableListableBeanFactory to look up beans
      dependentBeanNames - 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 given Method injection 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:
      resolve in interface InjectionPointDependencyResolver
      Parameters:
      method - the method injection point to resolve
      beanFactory - the ConfigurableListableBeanFactory to look up beans
      dependentBeanNames - 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 given Constructor injection 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:
      resolve in interface InjectionPointDependencyResolver
      Parameters:
      constructor - the constructor injection point to resolve
      beanFactory - the ConfigurableListableBeanFactory to look up beans
      dependentBeanNames - 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 given Parameter injection 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:
      resolve in interface InjectionPointDependencyResolver
      Parameters:
      parameter - the parameter injection point to resolve
      beanFactory - the ConfigurableListableBeanFactory to look up beans
      dependentBeanNames - 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 the ResolvableDependencyTypeFilter and AutowireCandidateResolver from the given BeanFactory.

      Example Usage

      
         AbstractInjectionPointDependencyResolver resolver = new MyDependencyResolver();
         resolver.setBeanFactory(beanFactory);
         // resolver is now ready to resolve injection point dependencies
       
      Specified by:
      setBeanFactory in interface org.springframework.beans.factory.BeanFactoryAware
      Parameters:
      beanFactory - the owning BeanFactory
      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 given Field using the AutowireCandidateResolver'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 from
      beanFactory - the ConfigurableListableBeanFactory (unused but available for subclass overrides)
      Returns:
      the suggested bean name, or null if 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 given Parameter using the AutowireCandidateResolver'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 from
      beanFactory - the ConfigurableListableBeanFactory (unused but available for subclass overrides)
      Returns:
      the suggested bean name, or null if no name was suggested
    • resolveDependentBeanNameByName

      protected String resolveDependentBeanNameByName(org.springframework.beans.factory.config.DependencyDescriptor dependencyDescriptor)
      Resolve the dependent bean name by name from the given DependencyDescriptor using the AutowireCandidateResolver'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 - the DependencyDescriptor to resolve the bean name from
      Returns:
      the suggested bean name as a String, or null if the suggested value is not a String or is null
    • 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 the Type to resolve bean names for
      beanFactory - the ConfigurableListableBeanFactory to look up beans
      dependentBeanNames - the set to collect resolved dependent bean names into
    • resolveDependentType

      protected Class<?> resolveDependentType(Type type)
      Resolve the dependent Class from the given Type.

      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
       
      Parameters:
      type - the Type to resolve
      Returns:
      the resolved Class, or null if the type cannot be resolved