Class BeanFactoryUtils
- All Implemented Interfaces:
io.microsphere.util.Utils
BeanFactory Utilities class- Since:
- 1.0.0
- Author:
- Mercy
-
Method Summary
Modifier and TypeMethodDescriptionstatic org.springframework.beans.factory.config.AutowireCapableBeanFactoryasAutowireCapableBeanFactory(Object beanFactory) Converts the givenObjectinto an instance ofAutowireCapableBeanFactory.static org.springframework.beans.factory.support.BeanDefinitionRegistryasBeanDefinitionRegistry(Object beanFactory) Converts the givenObjectinto an instance ofBeanDefinitionRegistry.static org.springframework.beans.factory.config.ConfigurableBeanFactoryasConfigurableBeanFactory(Object beanFactory) Converts the givenObjectinto an instance ofConfigurableBeanFactory.static org.springframework.beans.factory.config.ConfigurableListableBeanFactoryasConfigurableListableBeanFactory(Object beanFactory) Converts the givenObjectinto an instance ofConfigurableListableBeanFactory.static org.springframework.beans.factory.support.DefaultListableBeanFactoryasDefaultListableBeanFactory(Object beanFactory) Converts the givenObjectinto an instance ofDefaultListableBeanFactory.static org.springframework.beans.factory.HierarchicalBeanFactoryasHierarchicalBeanFactory(Object beanFactory) Converts the givenObjectinto an instance ofHierarchicalBeanFactory.static org.springframework.beans.factory.ListableBeanFactoryasListableBeanFactory(Object beanFactory) Converts the givenObjectinto an instance ofListableBeanFactory.static List<org.springframework.beans.factory.config.BeanPostProcessor>getBeanPostProcessors(org.springframework.beans.factory.BeanFactory beanFactory) Retrieves all instances ofBeanPostProcessorfrom the givenBeanFactory.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 theListableBeanFactory.static <T> TgetOptionalBean(org.springframework.beans.factory.ListableBeanFactory beanFactory, String beanName, Class<T> beanType) Retrieves a bean of the specified type and name from the givenListableBeanFactoryif it exists.getResolvableDependencyTypes(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory) Retrieves the set of resolvable dependency types that have been registered with the givenConfigurableListableBeanFactory.getResolvableDependencyTypes(org.springframework.beans.factory.support.DefaultListableBeanFactory beanFactory) Retrieves the set of resolvable dependency types that have been registered with the givenDefaultListableBeanFactory.static booleanisBeanDefinitionRegistry(Object beanFactory) Checks whether the provided object is an instance ofBeanDefinitionRegistry.static booleanisDefaultListableBeanFactory(Object beanFactory) Checks whether the provided object is an instance ofDefaultListableBeanFactory.
-
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 givenListableBeanFactoryif 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 benull.beanName- The name of the bean to retrieve. If empty or blank, this method will returnnull.beanType- The type of the bean to retrieve. Must not benull.- 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 theListableBeanFactory.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 benull.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 benull.- 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
Checks whether the provided object is an instance ofDefaultListableBeanFactory.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 benullor any type.- Returns:
trueif the object is an instance ofDefaultListableBeanFactory;falseotherwise.
-
isBeanDefinitionRegistry
Checks whether the provided object is an instance ofBeanDefinitionRegistry.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 benullor any type.- Returns:
trueif the object is an instance ofBeanDefinitionRegistry;falseotherwise.
-
asBeanDefinitionRegistry
@Nullable public static org.springframework.beans.factory.support.BeanDefinitionRegistry asBeanDefinitionRegistry(Object beanFactory) Converts the givenObjectinto an instance ofBeanDefinitionRegistry.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 benullor any type.- Returns:
- An instance of
BeanDefinitionRegistry, ornullif the input isnull. - Throws:
IllegalArgumentException- if the provided object is not an instance ofBeanDefinitionRegistryand cannot be converted.
-
asListableBeanFactory
@Nullable public static org.springframework.beans.factory.ListableBeanFactory asListableBeanFactory(Object beanFactory) Converts the givenObjectinto an instance ofListableBeanFactory.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 benullor any type.- Returns:
- An instance of
ListableBeanFactory, ornullif the input isnull. - Throws:
IllegalArgumentException- if the provided object is not an instance ofListableBeanFactoryand cannot be converted.
-
asHierarchicalBeanFactory
@Nullable public static org.springframework.beans.factory.HierarchicalBeanFactory asHierarchicalBeanFactory(Object beanFactory) Converts the givenObjectinto an instance ofHierarchicalBeanFactory.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 benullor any type.- Returns:
- An instance of
HierarchicalBeanFactory, ornullif the input isnull. - Throws:
IllegalArgumentException- if the provided object is not an instance ofHierarchicalBeanFactoryand cannot be converted.
-
asConfigurableBeanFactory
@Nullable public static org.springframework.beans.factory.config.ConfigurableBeanFactory asConfigurableBeanFactory(Object beanFactory) Converts the givenObjectinto an instance ofConfigurableBeanFactory.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 benullor any type.- Returns:
- An instance of
ConfigurableBeanFactory, ornullif the input isnull. - Throws:
IllegalArgumentException- if the provided object is not an instance ofConfigurableBeanFactoryand cannot be converted.
-
asAutowireCapableBeanFactory
@Nullable public static org.springframework.beans.factory.config.AutowireCapableBeanFactory asAutowireCapableBeanFactory(Object beanFactory) Converts the givenObjectinto an instance ofAutowireCapableBeanFactory.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 benullor any type.- Returns:
- An instance of
AutowireCapableBeanFactory, ornullif the input isnull. - Throws:
IllegalArgumentException- if the provided object is not an instance ofAutowireCapableBeanFactoryand cannot be converted.
-
asConfigurableListableBeanFactory
@Nullable public static org.springframework.beans.factory.config.ConfigurableListableBeanFactory asConfigurableListableBeanFactory(Object beanFactory) Converts the givenObjectinto an instance ofConfigurableListableBeanFactory.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 benullor any type.- Returns:
- An instance of
ConfigurableListableBeanFactory, ornullif the input isnull. - Throws:
IllegalArgumentException- if the provided object is not an instance ofConfigurableListableBeanFactoryand cannot be converted.
-
asDefaultListableBeanFactory
@Nullable public static org.springframework.beans.factory.support.DefaultListableBeanFactory asDefaultListableBeanFactory(Object beanFactory) Converts the givenObjectinto an instance ofDefaultListableBeanFactory.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 SpringApplicationContext, 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 benullor any type.- Returns:
- An instance of
DefaultListableBeanFactory, ornullif the input isnull. - Throws:
IllegalArgumentException- if the provided object is not an instance ofDefaultListableBeanFactoryand 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 givenConfigurableListableBeanFactory.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 benull.- 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 givenDefaultListableBeanFactory.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 benull.- 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 ofBeanPostProcessorfrom the givenBeanFactory.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 benull.- Returns:
- A non-null, unmodifiable list of
BeanPostProcessorinstances if available; otherwise, an empty list.
-