Class BeanFactoryUtils

java.lang.Object
io.microsphere.spring.beans.factory.BeanFactoryUtils
All Implemented Interfaces:
io.microsphere.util.Utils

public abstract class BeanFactoryUtils extends Object implements io.microsphere.util.Utils
BeanFactory Utilities class
Since:
1.0.0
Author:
Mercy
  • Method Summary

    Modifier and Type
    Method
    Description
    static org.springframework.beans.factory.config.AutowireCapableBeanFactory
    Converts the given Object into an instance of AutowireCapableBeanFactory.
    static org.springframework.beans.factory.support.BeanDefinitionRegistry
    Converts the given Object into an instance of BeanDefinitionRegistry.
    static org.springframework.beans.factory.config.ConfigurableBeanFactory
    Converts the given Object into an instance of ConfigurableBeanFactory.
    static org.springframework.beans.factory.config.ConfigurableListableBeanFactory
    Converts the given Object into an instance of ConfigurableListableBeanFactory.
    static org.springframework.beans.factory.support.DefaultListableBeanFactory
    Converts the given Object into an instance of DefaultListableBeanFactory.
    static org.springframework.beans.factory.HierarchicalBeanFactory
    Converts the given Object into an instance of HierarchicalBeanFactory.
    static org.springframework.beans.factory.ListableBeanFactory
    Converts the given Object into an instance of ListableBeanFactory.
    static List<org.springframework.beans.factory.config.BeanPostProcessor>
    getBeanPostProcessors(org.springframework.beans.factory.BeanFactory beanFactory)
    Retrieves all instances of BeanPostProcessor from the given BeanFactory.
    static <T> List<T>
    getBeans(org.springframework.beans.factory.ListableBeanFactory beanFactory, String[] beanNames, Class<T> beanType)
    Retrieves beans of the specified type that match the given bean names from the ListableBeanFactory.
    static <T> T
    getOptionalBean(org.springframework.beans.factory.ListableBeanFactory beanFactory, String beanName, Class<T> beanType)
    Retrieves a bean of the specified type and name from the given ListableBeanFactory if it exists.
    static Set<Class<?>>
    getResolvableDependencyTypes(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
    Retrieves the set of resolvable dependency types that have been registered with the given ConfigurableListableBeanFactory.
    static Set<Class<?>>
    getResolvableDependencyTypes(org.springframework.beans.factory.support.DefaultListableBeanFactory beanFactory)
    Retrieves the set of resolvable dependency types that have been registered with the given DefaultListableBeanFactory.
    static boolean
    Checks whether the provided object is an instance of BeanDefinitionRegistry.
    static boolean
    Checks whether the provided object is an instance of DefaultListableBeanFactory.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getOptionalBean

      @Nullable public static <T> T getOptionalBean(org.springframework.beans.factory.ListableBeanFactory beanFactory, String beanName, Class<T> beanType)
      Retrieves a bean of the specified type and name from the given ListableBeanFactory if it exists.

      This method checks whether the provided bean name is valid and then attempts to retrieve the corresponding bean. If no such bean exists or the name is invalid, this method returns null.

      Example Usage

      
       ListableBeanFactory beanFactory = ...; // Obtain the bean factory
       String beanName = "myBean";
       Class<MyBeanType> beanType = MyBeanType.class;
      
       MyBeanType myBean = BeanFactoryUtils.getOptionalBean(beanFactory, beanName, beanType);
       if (myBean != null) {
           // Use the bean
       } else {
           // Handle absence of the bean
       }
       
      Type Parameters:
      T - The type of the bean to retrieve.
      Parameters:
      beanFactory - The target bean factory to retrieve the bean from. Must not be null.
      beanName - The name of the bean to retrieve. If empty or blank, this method will return null.
      beanType - The type of the bean to retrieve. Must not be null.
      Returns:
      The bean instance if present in the bean factory; otherwise, null.
    • getBeans

      @Nonnull public static <T> List<T> getBeans(org.springframework.beans.factory.ListableBeanFactory beanFactory, String[] beanNames, Class<T> beanType)
      Retrieves beans of the specified type that match the given bean names from the ListableBeanFactory.

      This method filters and returns only those beans whose names are present in the provided array of bean names. It ensures that all beans returned are of the specified type. If no matching beans are found, an empty list is returned.

      Example Usage

      
       ListableBeanFactory beanFactory = ...; // Obtain or inject the bean factory
       String[] beanNames = {"bean1", "bean2"};
       Class<MyBeanType> beanType = MyBeanType.class;
      
       List<MyBeanType> beans = BeanFactoryUtils.getBeans(beanFactory, beanNames, beanType);
      
       if (!beans.isEmpty()) {
           for (MyBeanType bean : beans) {
               // Use each bean instance
           }
       } else {
           // Handle case where no beans were found
       }
       
      Type Parameters:
      T - The type of the beans to retrieve.
      Parameters:
      beanFactory - The target bean factory to retrieve beans from. Must not be null.
      beanNames - An array of bean names to filter by. If empty or null, this method will return an empty list.
      beanType - The type of the beans to retrieve. Must not be null.
      Returns:
      A read-only, non-null list of bean instances that match the specified names and type. Returns an empty list if no matches are found.
    • isDefaultListableBeanFactory

      public static boolean isDefaultListableBeanFactory(Object beanFactory)
      Checks whether the provided object is an instance of DefaultListableBeanFactory.

      This method is useful when you need to verify if a given bean factory supports bean definition registration and listing capabilities, typically available in a standard Spring container setup.

      Example Usage

      
       Object beanFactory = ...; // Obtain or inject a bean factory
      
       if (BeanFactoryUtils.isDefaultListableBeanFactory(beanFactory)) {
           DefaultListableBeanFactory dlbf = BeanFactoryUtils.asDefaultListableBeanFactory(beanFactory);
           // Use dlbf for advanced operations like registering new bean definitions
       } else {
           // Handle case where bean factory is not a DefaultListableBeanFactory
       }
       
      Parameters:
      beanFactory - The object to check. May be null or any type.
      Returns:
      true if the object is an instance of DefaultListableBeanFactory; false otherwise.
    • isBeanDefinitionRegistry

      public static boolean isBeanDefinitionRegistry(Object beanFactory)
      Checks whether the provided object is an instance of BeanDefinitionRegistry.

      This method is useful when you need to verify if a given bean factory supports dynamic registration of bean definitions, which is typically required during advanced configuration or post-processing phases.

      Example Usage

      
       Object beanFactory = ...; // Obtain or inject a bean factory
      
       if (BeanFactoryUtils.isBeanDefinitionRegistry(beanFactory)) {
           BeanDefinitionRegistry registry = BeanFactoryUtils.asBeanDefinitionRegistry(beanFactory);
           // Register or manipulate bean definitions as needed
       } else {
           // Handle case where bean factory does not support BeanDefinitionRegistry
       }
       
      Parameters:
      beanFactory - The object to check. May be null or any type.
      Returns:
      true if the object is an instance of BeanDefinitionRegistry; false otherwise.
    • asBeanDefinitionRegistry

      @Nullable public static org.springframework.beans.factory.support.BeanDefinitionRegistry asBeanDefinitionRegistry(Object beanFactory)
      Converts the given Object into an instance of BeanDefinitionRegistry.

      This method is typically used when you need to perform operations that require a bean definition registry, such as registering or modifying bean definitions in advanced configuration scenarios.

      Example Usage

      
       Object beanFactory = ...; // Obtain or inject a bean factory
      
       if (BeanFactoryUtils.isBeanDefinitionRegistry(beanFactory)) {
           BeanDefinitionRegistry registry = BeanFactoryUtils.asBeanDefinitionRegistry(beanFactory);
           // Register or manipulate bean definitions as needed
           registry.registerBeanDefinition("myBean", myBeanDefinition);
       } else {
           // Handle case where bean factory does not support BeanDefinitionRegistry
       }
       
      Parameters:
      beanFactory - The object to convert. May be null or any type.
      Returns:
      An instance of BeanDefinitionRegistry, or null if the input is null.
      Throws:
      IllegalArgumentException - if the provided object is not an instance of BeanDefinitionRegistry and cannot be converted.
    • asListableBeanFactory

      @Nullable public static org.springframework.beans.factory.ListableBeanFactory asListableBeanFactory(Object beanFactory)
      Converts the given Object into an instance of ListableBeanFactory.

      This method is typically used when you need to perform operations specific to a listable bean factory, such as retrieving beans by type or getting bean names. If the provided object is a Spring ApplicationContext, it will be adapted to its underlying autowire-capable bean factory.

      Example Usage

      
       Object beanFactory = ...; // Obtain or inject a bean factory
      
       if (BeanFactoryUtils.isListableBeanFactory(beanFactory)) {
           ListableBeanFactory lbf = BeanFactoryUtils.asListableBeanFactory(beanFactory);
           String[] beanNames = lbf.getBeanDefinitionNames();
           for (String beanName : beanNames) {
               // Process each bean name
           }
       } else {
           // Handle case where bean factory does not support ListableBeanFactory
       }
       
      Parameters:
      beanFactory - The object to convert. May be null or any type.
      Returns:
      An instance of ListableBeanFactory, or null if the input is null.
      Throws:
      IllegalArgumentException - if the provided object is not an instance of ListableBeanFactory and cannot be converted.
    • asHierarchicalBeanFactory

      @Nullable public static org.springframework.beans.factory.HierarchicalBeanFactory asHierarchicalBeanFactory(Object beanFactory)
      Converts the given Object into an instance of HierarchicalBeanFactory.

      This method is typically used when you need to perform operations specific to a hierarchical bean factory, such as accessing parent bean factories or managing bean definitions in a hierarchical structure. If the provided object is a Spring ApplicationContext, it will be adapted to its underlying autowire-capable bean factory before conversion.

      Example Usage

      
       Object beanFactory = ...; // Obtain or inject a bean factory
      
       if (BeanFactoryUtils.isHierarchicalBeanFactory(beanFactory)) {
           HierarchicalBeanFactory hbf = BeanFactoryUtils.asHierarchicalBeanFactory(beanFactory);
           BeanFactory parentFactory = hbf.getParentBeanFactory();
           // Use the hierarchical bean factory and its parent if available
       } else {
           // Handle case where bean factory does not support HierarchicalBeanFactory
       }
       
      Parameters:
      beanFactory - The object to convert. May be null or any type.
      Returns:
      An instance of HierarchicalBeanFactory, or null if the input is null.
      Throws:
      IllegalArgumentException - if the provided object is not an instance of HierarchicalBeanFactory and cannot be converted.
    • asConfigurableBeanFactory

      @Nullable public static org.springframework.beans.factory.config.ConfigurableBeanFactory asConfigurableBeanFactory(Object beanFactory)
      Converts the given Object into an instance of ConfigurableBeanFactory.

      This method is typically used when you need to perform operations specific to a configurable bean factory, such as modifying bean definitions or customizing the configuration process. If the provided object is a Spring ApplicationContext, it will be adapted to its underlying autowire-capable bean factory before conversion.

      Example Usage

      
       Object beanFactory = ...; // Obtain or inject a bean factory
      
       if (BeanFactoryUtils.isConfigurableBeanFactory(beanFactory)) {
           ConfigurableBeanFactory cbf = BeanFactoryUtils.asConfigurableBeanFactory(beanFactory);
           // Customize the bean factory configuration as needed
           cbf.setAllowBeanDefinitionOverriding(true);
       } else {
           // Handle case where bean factory does not support ConfigurableBeanFactory
       }
       
      Parameters:
      beanFactory - The object to convert. May be null or any type.
      Returns:
      An instance of ConfigurableBeanFactory, or null if the input is null.
      Throws:
      IllegalArgumentException - if the provided object is not an instance of ConfigurableBeanFactory and cannot be converted.
    • asAutowireCapableBeanFactory

      @Nullable public static org.springframework.beans.factory.config.AutowireCapableBeanFactory asAutowireCapableBeanFactory(Object beanFactory)
      Converts the given Object into an instance of AutowireCapableBeanFactory.

      This method is typically used when you need to perform operations specific to an autowire-capable bean factory, such as autowiring beans or managing bean definitions in advanced configuration scenarios. If the provided object is a Spring ApplicationContext, it will be adapted to its underlying autowire-capable bean factory before conversion.

      Example Usage

      
       Object beanFactory = ...; // Obtain or inject a bean factory
      
       if (BeanFactoryUtils.isAutowireCapableBeanFactory(beanFactory)) {
           AutowireCapableBeanFactory acbf = BeanFactoryUtils.asAutowireCapableBeanFactory(beanFactory);
           // Use the autowire-capable bean factory for autowiring or bean creation
           MyBean myBean = acbf.createBean(MyBean.class);
       } else {
           // Handle case where bean factory does not support AutowireCapableBeanFactory
       }
       
      Parameters:
      beanFactory - The object to convert. May be null or any type.
      Returns:
      An instance of AutowireCapableBeanFactory, or null if the input is null.
      Throws:
      IllegalArgumentException - if the provided object is not an instance of AutowireCapableBeanFactory and cannot be converted.
    • asConfigurableListableBeanFactory

      @Nullable public static org.springframework.beans.factory.config.ConfigurableListableBeanFactory asConfigurableListableBeanFactory(Object beanFactory)
      Converts the given Object into an instance of ConfigurableListableBeanFactory.

      This method is typically used when you need to perform operations specific to a configurable listable bean factory, such as retrieving beans by type or managing bean definitions with configurability and listability. If the provided object is a Spring ApplicationContext, it will be adapted to its underlying autowire-capable bean factory before conversion.

      Example Usage

      
       Object beanFactory = ...; // Obtain or inject a bean factory
      
       if (BeanFactoryUtils.isConfigurableListableBeanFactory(beanFactory)) {
           ConfigurableListableBeanFactory clbf = BeanFactoryUtils.asConfigurableListableBeanFactory(beanFactory);
           // Retrieve beans by type or configure bean definitions
           MyBean myBean = clbf.getBean(MyBean.class);
       } else {
           // Handle case where bean factory does not support ConfigurableListableBeanFactory
       }
       
      Parameters:
      beanFactory - The object to convert. May be null or any type.
      Returns:
      An instance of ConfigurableListableBeanFactory, or null if the input is null.
      Throws:
      IllegalArgumentException - if the provided object is not an instance of ConfigurableListableBeanFactory and cannot be converted.
    • asDefaultListableBeanFactory

      @Nullable public static org.springframework.beans.factory.support.DefaultListableBeanFactory asDefaultListableBeanFactory(Object beanFactory)
      Converts the given Object into an instance of DefaultListableBeanFactory.

      This method is typically used when you need to perform operations specific to a DefaultListableBeanFactory, such as registering or retrieving bean definitions. If the provided object is a Spring ApplicationContext, it will be adapted to its underlying autowire-capable bean factory before conversion.

      Example Usage

      
       Object beanFactory = ...; // Obtain or inject a bean factory
      
       if (BeanFactoryUtils.isDefaultListableBeanFactory(beanFactory)) {
           DefaultListableBeanFactory dlbf = BeanFactoryUtils.asDefaultListableBeanFactory(beanFactory);
           // Register or manipulate bean definitions as needed
           dlbf.registerBeanDefinition("myBean", myBeanDefinition);
       } else {
           // Handle case where bean factory is not a DefaultListableBeanFactory
       }
       
      Parameters:
      beanFactory - The object to convert. May be null or any type.
      Returns:
      An instance of DefaultListableBeanFactory, or null if the input is null.
      Throws:
      IllegalArgumentException - if the provided object is not an instance of DefaultListableBeanFactory and cannot be converted.
    • getResolvableDependencyTypes

      @Nonnull public static Set<Class<?>> getResolvableDependencyTypes(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
      Retrieves the set of resolvable dependency types that have been registered with the given ConfigurableListableBeanFactory.

      This method provides access to the types that the bean factory has been configured to resolve automatically when needed during bean creation. It is useful for inspecting or debugging which dependencies are available for autowiring resolution.

      Example Usage

      
       ConfigurableListableBeanFactory beanFactory = ...; // Obtain or inject the bean factory
      
       Set<Class<?>> resolvableDependencies = BeanFactoryUtils.getResolvableDependencyTypes(beanFactory);
      
       if (!resolvableDependencies.isEmpty()) {
           for (Class<?> dependencyType : resolvableDependencies) {
               System.out.println("Registered resolvable dependency type: " + dependencyType.getName());
           }
       } else {
           System.out.println("No resolvable dependency types found.");
       }
       
      Parameters:
      beanFactory - The target bean factory from which to retrieve the resolvable dependency types. Must not be null.
      Returns:
      A non-null, read-only set of resolvable dependency types. Returns an empty set if no resolvable dependencies are registered.
    • getResolvableDependencyTypes

      @Nonnull public static Set<Class<?>> getResolvableDependencyTypes(org.springframework.beans.factory.support.DefaultListableBeanFactory beanFactory)
      Retrieves the set of resolvable dependency types that have been registered with the given DefaultListableBeanFactory.

      This method accesses the internal registry of resolvable dependencies maintained by the bean factory. These are typically used during autowiring to resolve and inject dependencies that are not directly defined as beans. If no resolvable dependencies are registered, an empty set is returned.

      Example Usage

      
       DefaultListableBeanFactory beanFactory = ...; // Obtain or create a bean factory instance
      
       Set<Class<?>> resolvableDependencies = BeanFactoryUtils.getResolvableDependencyTypes(beanFactory);
      
       if (!resolvableDependencies.isEmpty()) {
           for (Class<?> dependencyType : resolvableDependencies) {
               System.out.println("Registered resolvable dependency: " + dependencyType.getName());
           }
       } else {
           System.out.println("No resolvable dependencies found.");
       }
       
      Parameters:
      beanFactory - The target bean factory from which to retrieve the resolvable dependency types. Must not be null.
      Returns:
      A non-null, read-only set of resolvable dependency types. Returns an empty set if no resolvable dependencies are registered.
    • getBeanPostProcessors

      @Nonnull public static List<org.springframework.beans.factory.config.BeanPostProcessor> getBeanPostProcessors(@Nullable org.springframework.beans.factory.BeanFactory beanFactory)
      Retrieves all instances of BeanPostProcessor from the given BeanFactory.

      This method checks if the provided bean factory is an instance of AbstractBeanFactory, which maintains a list of registered bean post processors. If it is, this method returns an unmodifiable list of those post processors. Otherwise, it returns an empty list.

      Example Usage

      
       BeanFactory beanFactory = ...; // Obtain or inject the bean factory
      
       List<BeanPostProcessor> postProcessors = BeanFactoryUtils.getBeanPostProcessors(beanFactory);
      
       if (!postProcessors.isEmpty()) {
           for (BeanPostProcessor processor : postProcessors) {
               // Use or inspect each bean post processor
               System.out.println("Found BeanPostProcessor: " + processor.getClass().getName());
           }
       } else {
           // Handle case where no bean post processors are available
           System.out.println("No BeanPostProcessors found.");
       }
       
      Parameters:
      beanFactory - The target bean factory to retrieve bean post processors from. May be null.
      Returns:
      A non-null, unmodifiable list of BeanPostProcessor instances if available; otherwise, an empty list.