Class BeanFactoryUtils
- java.lang.Object
-
- io.microsphere.spring.beans.factory.BeanFactoryUtils
-
- All Implemented Interfaces:
io.microsphere.util.Utils
public abstract class BeanFactoryUtils extends java.lang.Object implements io.microsphere.util.Utils
BeanFactory
Utilities class- Since:
- 1.0.0
- Author:
- Mercy
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static org.springframework.beans.factory.config.AutowireCapableBeanFactory
asAutowireCapableBeanFactory(java.lang.Object beanFactory)
Converts the givenObject
into an instance ofAutowireCapableBeanFactory
.static org.springframework.beans.factory.support.BeanDefinitionRegistry
asBeanDefinitionRegistry(java.lang.Object beanFactory)
Converts the givenObject
into an instance ofBeanDefinitionRegistry
.static org.springframework.beans.factory.config.ConfigurableBeanFactory
asConfigurableBeanFactory(java.lang.Object beanFactory)
Converts the givenObject
into an instance ofConfigurableBeanFactory
.static org.springframework.beans.factory.config.ConfigurableListableBeanFactory
asConfigurableListableBeanFactory(java.lang.Object beanFactory)
Converts the givenObject
into an instance ofConfigurableListableBeanFactory
.static org.springframework.beans.factory.support.DefaultListableBeanFactory
asDefaultListableBeanFactory(java.lang.Object beanFactory)
Converts the givenObject
into an instance ofDefaultListableBeanFactory
.static org.springframework.beans.factory.HierarchicalBeanFactory
asHierarchicalBeanFactory(java.lang.Object beanFactory)
Converts the givenObject
into an instance ofHierarchicalBeanFactory
.static org.springframework.beans.factory.ListableBeanFactory
asListableBeanFactory(java.lang.Object beanFactory)
Converts the givenObject
into an instance ofListableBeanFactory
.static java.util.List<org.springframework.beans.factory.config.BeanPostProcessor>
getBeanPostProcessors(org.springframework.beans.factory.BeanFactory beanFactory)
Retrieves all instances ofBeanPostProcessor
from the givenBeanFactory
.static <T> java.util.List<T>
getBeans(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.String[] beanNames, java.lang.Class<T> beanType)
Retrieves beans of the specified type that match the given bean names from theListableBeanFactory
.static <T> T
getOptionalBean(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.String beanName, java.lang.Class<T> beanType)
Retrieves a bean of the specified type and name from the givenListableBeanFactory
if it exists.static java.util.Set<java.lang.Class<?>>
getResolvableDependencyTypes(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
Retrieves the set of resolvable dependency types that have been registered with the givenConfigurableListableBeanFactory
.static java.util.Set<java.lang.Class<?>>
getResolvableDependencyTypes(org.springframework.beans.factory.support.DefaultListableBeanFactory beanFactory)
Retrieves the set of resolvable dependency types that have been registered with the givenDefaultListableBeanFactory
.static boolean
isBeanDefinitionRegistry(java.lang.Object beanFactory)
Checks whether the provided object is an instance ofBeanDefinitionRegistry
.static boolean
isDefaultListableBeanFactory(java.lang.Object beanFactory)
Checks whether the provided object is an instance ofDefaultListableBeanFactory
.
-
-
-
Method Detail
-
getOptionalBean
@Nullable public static <T> T getOptionalBean(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.String beanName, java.lang.Class<T> beanType)
Retrieves a bean of the specified type and name from the givenListableBeanFactory
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 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> java.util.List<T> getBeans(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.String[] beanNames, java.lang.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
public static boolean isDefaultListableBeanFactory(java.lang.Object beanFactory)
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 benull
or any type.- Returns:
true
if the object is an instance ofDefaultListableBeanFactory
;false
otherwise.
-
isBeanDefinitionRegistry
public static boolean isBeanDefinitionRegistry(java.lang.Object beanFactory)
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 benull
or any type.- Returns:
true
if the object is an instance ofBeanDefinitionRegistry
;false
otherwise.
-
asBeanDefinitionRegistry
@Nullable public static org.springframework.beans.factory.support.BeanDefinitionRegistry asBeanDefinitionRegistry(java.lang.Object beanFactory)
Converts the givenObject
into 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 benull
or any type.- Returns:
- An instance of
BeanDefinitionRegistry
, ornull
if the input isnull
. - Throws:
java.lang.IllegalArgumentException
- if the provided object is not an instance ofBeanDefinitionRegistry
and cannot be converted.
-
asListableBeanFactory
@Nullable public static org.springframework.beans.factory.ListableBeanFactory asListableBeanFactory(java.lang.Object beanFactory)
Converts the givenObject
into 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 benull
or any type.- Returns:
- An instance of
ListableBeanFactory
, ornull
if the input isnull
. - Throws:
java.lang.IllegalArgumentException
- if the provided object is not an instance ofListableBeanFactory
and cannot be converted.
-
asHierarchicalBeanFactory
@Nullable public static org.springframework.beans.factory.HierarchicalBeanFactory asHierarchicalBeanFactory(java.lang.Object beanFactory)
Converts the givenObject
into 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 benull
or any type.- Returns:
- An instance of
HierarchicalBeanFactory
, ornull
if the input isnull
. - Throws:
java.lang.IllegalArgumentException
- if the provided object is not an instance ofHierarchicalBeanFactory
and cannot be converted.
-
asConfigurableBeanFactory
@Nullable public static org.springframework.beans.factory.config.ConfigurableBeanFactory asConfigurableBeanFactory(java.lang.Object beanFactory)
Converts the givenObject
into 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 benull
or any type.- Returns:
- An instance of
ConfigurableBeanFactory
, ornull
if the input isnull
. - Throws:
java.lang.IllegalArgumentException
- if the provided object is not an instance ofConfigurableBeanFactory
and cannot be converted.
-
asAutowireCapableBeanFactory
@Nullable public static org.springframework.beans.factory.config.AutowireCapableBeanFactory asAutowireCapableBeanFactory(java.lang.Object beanFactory)
Converts the givenObject
into 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 benull
or any type.- Returns:
- An instance of
AutowireCapableBeanFactory
, ornull
if the input isnull
. - Throws:
java.lang.IllegalArgumentException
- if the provided object is not an instance ofAutowireCapableBeanFactory
and cannot be converted.
-
asConfigurableListableBeanFactory
@Nullable public static org.springframework.beans.factory.config.ConfigurableListableBeanFactory asConfigurableListableBeanFactory(java.lang.Object beanFactory)
Converts the givenObject
into 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 benull
or any type.- Returns:
- An instance of
ConfigurableListableBeanFactory
, ornull
if the input isnull
. - Throws:
java.lang.IllegalArgumentException
- if the provided object is not an instance ofConfigurableListableBeanFactory
and cannot be converted.
-
asDefaultListableBeanFactory
@Nullable public static org.springframework.beans.factory.support.DefaultListableBeanFactory asDefaultListableBeanFactory(java.lang.Object beanFactory)
Converts the givenObject
into 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 benull
or any type.- Returns:
- An instance of
DefaultListableBeanFactory
, ornull
if the input isnull
. - Throws:
java.lang.IllegalArgumentException
- if the provided object is not an instance ofDefaultListableBeanFactory
and cannot be converted.
-
getResolvableDependencyTypes
@Nonnull public static java.util.Set<java.lang.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 java.util.Set<java.lang.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 java.util.List<org.springframework.beans.factory.config.BeanPostProcessor> getBeanPostProcessors(@Nullable org.springframework.beans.factory.BeanFactory beanFactory)
Retrieves all instances ofBeanPostProcessor
from 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
BeanPostProcessor
instances if available; otherwise, an empty list.
-
-