Skip to content

io microsphere spring beans factory BeanFactoryUtils

github-actions[bot] edited this page Jun 23, 2026 · 26 revisions

BeanFactoryUtils

Type: Class | Module: microsphere-spring-context | Package: io.microsphere.spring.beans.factory | Since: 1.0.0

Source: microsphere-spring-context/src/main/java/io/microsphere/spring/beans/factory/BeanFactoryUtils.java

Overview

BeanFactory Utilities class

Declaration

public abstract class BeanFactoryUtils implements Utils

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.2.32-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

Method Examples

getOptionalBean

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
}

getBeans

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
}

isDefaultListableBeanFactory

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
}

isBeanDefinitionRegistry

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
}

asBeanDefinitionRegistry

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
}

asListableBeanFactory

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
}

asHierarchicalBeanFactory

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
}

asConfigurableBeanFactory

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
}

asAutowireCapableBeanFactory

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
}

asConfigurableListableBeanFactory

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
}

asDefaultListableBeanFactory

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
}

getResolvableDependencyTypes

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.");
}

getResolvableDependencyTypes

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.");
}

getBeanPostProcessors

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.");
}

getBeanClassLoader

BeanFactory beanFactory = ...; // Obtain or inject the bean factory

ClassLoader classLoader = BeanFactoryUtils.getBeanClassLoader(beanFactory);

if (classLoader != null) {
    // Use the class loader for dynamic class loading or resource access
    Class<?> clazz = classLoader.loadClass("com.example.MyClass");
} else {
    // Handle case where class loader is not available
    System.out.println("Bean class loader is not available.");
}

nullSafeBeanClassLoader

BeanFactory beanFactory = ...; // Obtain or inject the bean factory

ClassLoader classLoader = BeanFactoryUtils.nullSafeBeanClassLoader(beanFactory);

// Use the class loader safely without null checks
Class<?> clazz = classLoader.loadClass("com.example.MyClass");

getBeanTypes

ListableBeanFactory beanFactory = ...; // Obtain or inject the bean factory
Class<MyService> serviceType = MyService.class;

Set<Class<MyService>> serviceTypes = BeanFactoryUtils.getBeanTypes(beanFactory, serviceType);

if (!serviceTypes.isEmpty()) {
    for (Class<MyService> type : serviceTypes) {
        System.out.println("Found bean type: " + type.getName());
    }
} else {
    System.out.println("No beans of type MyService found.");
}

getBeanTypes

ListableBeanFactory beanFactory = ...; // Obtain or inject the bean factory
Class<MyService> serviceType = MyService.class;

Set<Class<MyService>> serviceTypes = BeanFactoryUtils.getBeanTypes(beanFactory, serviceType, true, false);

if (!serviceTypes.isEmpty()) {
    for (Class<MyService> type : serviceTypes) {
        System.out.println("Found bean type: " + type.getName());
    }
} else {
    System.out.println("No beans of type MyService found.");
}

getBeanClass

ConfigurableListableBeanFactory beanFactory = ...; // Obtain or inject the bean factory
String beanName = "myBean";

Class<?> beanClass = BeanFactoryUtils.getBeanClass(beanFactory, beanName);

if (beanClass != null) {
    System.out.println("Bean class: " + beanClass.getName());
} else {
    System.out.println("Could not determine bean class for: " + beanName);
}

getBeanDefinition

ConfigurableListableBeanFactory beanFactory = ...; // Obtain or inject the bean factory
String beanName = "myBean";

BeanDefinition beanDefinition = BeanFactoryUtils.getBeanDefinition(beanFactory, beanName);

if (beanDefinition != null) {
    // Inspect or modify the bean definition
    System.out.println("Bean class: " + beanDefinition.getBeanClassName());
} else {
    // Handle case where bean definition is not found
    System.out.println("No bean definition found for name: " + beanName);
}

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-spring-context</artifactId>
    <version>${microsphere-spring.version}</version>
</dependency>

Tip: Use the BOM (microsphere-spring-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.spring.beans.factory.BeanFactoryUtils;

API Reference

Public Methods

Method Description
getOptionalBean Retrieves a bean of the specified type and name from the given ListableBeanFactory if it exists.
getBeans Retrieves beans of the specified type that match the given bean names from the ListableBeanFactory.
isDefaultListableBeanFactory Checks whether the provided object is an instance of DefaultListableBeanFactory.
isBeanDefinitionRegistry Checks whether the provided object is an instance of BeanDefinitionRegistry.
asBeanDefinitionRegistry Converts the given Object into an instance of BeanDefinitionRegistry.
asListableBeanFactory Converts the given Object into an instance of ListableBeanFactory.
asHierarchicalBeanFactory Converts the given Object into an instance of HierarchicalBeanFactory.
asConfigurableBeanFactory Converts the given Object into an instance of ConfigurableBeanFactory.
asAutowireCapableBeanFactory Converts the given Object into an instance of AutowireCapableBeanFactory.
asConfigurableListableBeanFactory Converts the given Object into an instance of ConfigurableListableBeanFactory.
asDefaultListableBeanFactory Converts the given Object into an instance of DefaultListableBeanFactory.
getResolvableDependencyTypes Retrieves the set of resolvable dependency types that have been registered with the given `ConfigurableListableBeanFa...
getResolvableDependencyTypes Retrieves the set of resolvable dependency types that have been registered with the given DefaultListableBeanFactory.
getBeanPostProcessors Retrieves all instances of BeanPostProcessor from the given BeanFactory.
getBeanClassLoader Retrieves the bean class loader from the given BeanFactory.
nullSafeBeanClassLoader Retrieves the bean class loader from the given BeanFactory in a null-safe manner.
getBeanTypes Retrieves the set of bean types that match the specified type from the given ListableBeanFactory.
getBeanTypes Retrieves the set of bean types that match the specified type from the given ListableBeanFactory.
getBeanClass Retrieves the actual Class of the bean with the specified name from the given ConfigurableListableBeanFactory.
getBeanDefinition Retrieves the BeanDefinition for the specified bean name from the given ConfigurableListableBeanFactory.

Method Details

getOptionalBean

public static <T> T getOptionalBean(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 beanType = MyBeanType.class;

MyBeanType myBean = BeanFactoryUtils.getOptionalBean(beanFactory, beanName, beanType);
if (myBean != null) {
    // Use the bean
` else {
    // Handle absence of the bean
}
}

getBeans

public static <T> List<T> getBeans(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 beanType = MyBeanType.class;

List 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
}
}

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
}
}

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
}
}

asBeanDefinitionRegistry

public static 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
}
}

asListableBeanFactory

public static 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
}
}

asHierarchicalBeanFactory

public static 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
}
}

asConfigurableBeanFactory

public static 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
}
}

asAutowireCapableBeanFactory

public static 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
}
}

asConfigurableListableBeanFactory

public static 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
}
}

asDefaultListableBeanFactory

public static 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
}
}

getResolvableDependencyTypes

public static Set<Class<?>> getResolvableDependencyTypes(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> 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.");
}
}

getResolvableDependencyTypes

public static Set<Class<?>> getResolvableDependencyTypes(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> 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.");
}
}

getBeanPostProcessors

public static List<BeanPostProcessor> getBeanPostProcessors(@Nullable 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 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.");
}
}

getBeanClassLoader

public static ClassLoader getBeanClassLoader(@Nullable BeanFactory beanFactory)

Retrieves the bean class loader from the given BeanFactory.

This method attempts to extract the ClassLoader used for loading bean classes. If the provided bean factory implements ConfigurableBeanFactory, its configured bean class loader is returned. Otherwise, this method returns null.

Example Usage

`BeanFactory beanFactory = ...; // Obtain or inject the bean factory

ClassLoader classLoader = BeanFactoryUtils.getBeanClassLoader(beanFactory);

if (classLoader != null) {
    // Use the class loader for dynamic class loading or resource access
    Class clazz = classLoader.loadClass("com.example.MyClass");
` else {
    // Handle case where class loader is not available
    System.out.println("Bean class loader is not available.");
}
}

nullSafeBeanClassLoader

public static ClassLoader nullSafeBeanClassLoader(@Nullable BeanFactory beanFactory)

Retrieves the bean class loader from the given BeanFactory in a null-safe manner.

This method attempts to extract the ClassLoader used for loading bean classes. If the provided bean factory implements ConfigurableBeanFactory, its configured bean class loader is returned. If the bean factory is null or does not provide a class loader, this method returns a safe default class loader (typically the context class loader of the current thread).

Example Usage

`BeanFactory beanFactory = ...; // Obtain or inject the bean factory

ClassLoader classLoader = BeanFactoryUtils.nullSafeBeanClassLoader(beanFactory);

// Use the class loader safely without null checks
Class clazz = classLoader.loadClass("com.example.MyClass");
`

getBeanTypes

public static <T> Set<Class<T>> getBeanTypes(ListableBeanFactory beanFactory, Class<T> beanType)

Retrieves the set of bean types that match the specified type from the given ListableBeanFactory.

This method scans the bean factory for beans of the specified type and returns their actual runtime classes. By default, it includes non-singleton beans and allows eager initialization of FactoryBeans during the search. The returned set is unmodifiable and immutable.

Example Usage

`ListableBeanFactory beanFactory = ...; // Obtain or inject the bean factory
Class serviceType = MyService.class;

Set> serviceTypes = BeanFactoryUtils.getBeanTypes(beanFactory, serviceType);

if (!serviceTypes.isEmpty()) {
    for (Class type : serviceTypes) {
        System.out.println("Found bean type: " + type.getName());
    `
} else {
    System.out.println("No beans of type MyService found.");
}
}

getBeanTypes

public static <T> Set<Class<T>> getBeanTypes(ListableBeanFactory beanFactory, Class<T> beanType,
                                                 boolean includeNonSingletons, boolean allowEagerInit)

Retrieves the set of bean types that match the specified type from the given ListableBeanFactory.

This method scans the bean factory for beans of the specified type and returns their actual runtime classes. It allows control over whether to include non-singleton beans and whether to allow eager initialization of FactoryBeans during the search. The returned set is unmodifiable and immutable.

Example Usage

`ListableBeanFactory beanFactory = ...; // Obtain or inject the bean factory
Class serviceType = MyService.class;

Set> serviceTypes = BeanFactoryUtils.getBeanTypes(beanFactory, serviceType, true, false);

if (!serviceTypes.isEmpty()) {
    for (Class type : serviceTypes) {
        System.out.println("Found bean type: " + type.getName());
    `
} else {
    System.out.println("No beans of type MyService found.");
}
}

getBeanClass

public static Class<?> getBeanClass(ConfigurableListableBeanFactory beanFactory, String beanName)

Retrieves the actual Class of the bean with the specified name from the given ConfigurableListableBeanFactory.

This method first attempts to retrieve the BeanDefinition for the specified bean name. If a bean definition is found, it resolves the bean type from the definition. If no bean definition is available (e.g., for manually registered singletons), it retrieves the singleton instance and determines its class. If the singleton instance is also not found, this method returns null.

Example Usage

`ConfigurableListableBeanFactory beanFactory = ...; // Obtain or inject the bean factory
String beanName = "myBean";

Class beanClass = BeanFactoryUtils.getBeanClass(beanFactory, beanName);

if (beanClass != null) {
    System.out.println("Bean class: " + beanClass.getName());
` else {
    System.out.println("Could not determine bean class for: " + beanName);
}
}

getBeanDefinition

public static BeanDefinition getBeanDefinition(ConfigurableListableBeanFactory beanFactory, String beanName)

Retrieves the BeanDefinition for the specified bean name from the given ConfigurableListableBeanFactory.

This method checks if the bean factory contains a bean definition for the provided name. If it does, the corresponding BeanDefinition is returned. Otherwise, this method returns null.

Example Usage

`ConfigurableListableBeanFactory beanFactory = ...; // Obtain or inject the bean factory
String beanName = "myBean";

BeanDefinition beanDefinition = BeanFactoryUtils.getBeanDefinition(beanFactory, beanName);

if (beanDefinition != null) {
    // Inspect or modify the bean definition
    System.out.println("Bean class: " + beanDefinition.getBeanClassName());
` else {
    // Handle case where bean definition is not found
    System.out.println("No bean definition found for name: " + beanName);
}
}

This documentation was auto-generated from the source code of microsphere-spring.

Home

spring-context

spring-guice

spring-jdbc

spring-test

spring-web

spring-webflux

spring-webmvc

Clone this wiki locally