-
Notifications
You must be signed in to change notification settings - Fork 39
io microsphere spring beans factory 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
BeanFactory Utilities class
public abstract class BeanFactoryUtils implements UtilsAuthor: Mercy
-
Introduced in:
1.0.0 -
Current Project Version:
0.2.32-SNAPSHOT
This component is tested and compatible with the following Java versions:
| Java Version | Status |
|---|---|
| Java 17 | ✅ Compatible |
| Java 21 | ✅ Compatible |
| Java 25 | ✅ Compatible |
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
}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
}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
}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
}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
}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
}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
}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
}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
}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
}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
}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.");
}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.");
}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.");
}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.");
}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");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.");
}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.");
}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);
}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);
}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 io.microsphere.spring.beans.factory.BeanFactoryUtils;| 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. |
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.
`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
}
}
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.
`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
}
}
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.
`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
}
}
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.
`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
}
}
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.
`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
}
}
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.
`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
}
}
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.
`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
}
}
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.
`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
}
}
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.
`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
}
}
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.
`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
}
}
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.
`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
}
}
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.
`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.");
}
}
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.
`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.");
}
}
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.
`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.");
}
}
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.
`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.");
}
}
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).
`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");
`
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.
`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.");
}
}
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.
`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.");
}
}
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.
`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);
}
}
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.
`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.
spring-context
- AbstractInjectionPointDependencyResolver
- AbstractSmartLifecycle
- AbstractSpringResourceURLConnection
- AnnotatedBeanCapableImportBeanDefinitionRegistrar
- AnnotatedBeanCapableImportCandidate
- AnnotatedBeanCapableImportSelector
- AnnotatedBeanDefinitionRegistryUtils
- AnnotatedInjectionBeanPostProcessor
- AnnotatedInjectionPointDependencyResolver
- AnnotatedPropertySourceLoader
- AnnotationBeanDefinitionRegistryPostProcessor
- AnnotationUtils
- ApplicationContextUtils
- ApplicationEventInterceptor
- ApplicationEventInterceptorChain
- ApplicationListenerInterceptor
- ApplicationListenerInterceptorChain
- AutoRegistrationBean
- AutoRegistrationBeanInitializer
- AutoRegistrationBeanRegistrar
- AutowireCandidateResolvingListener
- AutowiredInjectionPointDependencyResolver
- BeanCapableImportCandidate
- BeanDefinitionUtils
- BeanDependencyResolver
- BeanFactoryListener
- BeanFactoryListenerAdapter
- BeanFactoryListeners
- BeanFactoryUtils
- BeanListener
- BeanListenerAdapter
- BeanListeners
- BeanMethodInjectionPointDependencyResolver
- BeanPropertyChangedEvent
- BeanRegistrar
- BeanSource
- BeanTimeStatistics
- BeanUtils
- CollectingConfigurationPropertyListener
- CompositeAutowireCandidateResolvingListener
- ConfigurableApplicationContextInitializer
- ConfigurationBeanAliasGenerator
- ConfigurationBeanBinder
- ConfigurationBeanBindingPostProcessor
- ConfigurationBeanBindingRegistrar
- ConfigurationBeanBindingsRegister
- ConfigurationBeanCustomizer
- ConfigurationPropertyOverrideAnnotationAttributesStrategy
- ConfigurationPropertyRepository
- ConstructionInjectionPointDependencyResolver
- ConversionServiceResolver
- ConversionServiceUtils
- DefaultApplicationEventInterceptorChain
- DefaultApplicationListenerInterceptorChain
- DefaultBeanDependencyResolver
- DefaultConfigurationBeanAliasGenerator
- DefaultConfigurationBeanBinder
- DefaultPropertiesPropertySource
- DefaultPropertiesPropertySourceLoader
- DefaultPropertiesPropertySources
- DefaultPropertiesPropertySourcesLoader
- DefaultResourceComparator
- DelegatingFactoryBean
- Dependency
- DependencyAnalysisBeanFactoryListener
- DependencyTreeWalker
- EnableAutoRegistrationBean
- EnableConfigurationBeanBinding
- EnableConfigurationBeanBindings
- EnableEventExtension
- EnableSpringConverterAdapter
- EnableSpringConverterAdapterRegistrar
- EnableTTLCaching
- EnvironmentEnabled
- EnvironmentListener
- EnvironmentUtils
- EventExtensionAttributes
- EventExtensionRegistrar
- EventPublishingBeanAfterProcessor
- EventPublishingBeanBeforeProcessor
- EventPublishingBeanInitializer
- ExposingClassPathBeanDefinitionScanner
- FilterMode
- GenericAnnotationAttributes
- GenericApplicationListenerAdapter
- GenericBeanNameGenerator
- GenericBeanPostProcessorAdapter
- HyphenAliasGenerator
- ImmutableMapPropertySource
- ImportOptional
- ImportOptionalSelector
- InjectionPointDependencyResolver
- InjectionPointDependencyResolvers
- InterceptingApplicationEventMulticaster
- InterceptingApplicationEventMulticasterProxy
- InterceptingApplicationListener
- JavaBeansPropertyChangeListenerAdapter
- JoinAliasGenerator
- JsonPropertySource
- JsonPropertySourceFactory
- ListenableAutowireCandidateResolver
- ListenableAutowireCandidateResolverInitializer
- ListenableConfigurableEnvironment
- ListenableConfigurableEnvironmentInitializer
- LoggingAutowireCandidateResolvingListener
- LoggingBeanFactoryListener
- LoggingBeanListener
- LoggingEnvironmentListener
- LoggingSmartLifecycle
- MethodParameterUtils
- MimeTypeUtils
- NamedBeanHolderComparator
- OnceApplicationContextEventListener
- OverrideAnnotationAttributes
- OverrideAnnotationAttributesStrategy
- ParallelPreInstantiationSingletonsBeanFactoryListener
- ProfileListener
- PropertiesUtils
- PropertyConstants
- PropertyResolverListener
- PropertyResolverUtils
- PropertySourceChangedEvent
- PropertySourceExtension
- PropertySourceExtensionAttributes
- PropertySourceExtensionLoader
- PropertySourcesChangedEvent
- PropertySourcesUtils
- PropertyValuesUtils
- ResolvableDependencyTypeFilter
- ResolvablePlaceholderAnnotationAttributes
- ResourceInjectionPointDependencyResolver
- ResourceLoaderUtils
- ResourcePropertySource
- ResourcePropertySourceLoader
- ResourcePropertySources
- ResourcePropertySourcesLoader
- ResourceUtils
- ResourceYamlProcessor
- SpringConverterAdapter
- SpringDelegatingBeanProtocolURLConnectionFactory
- SpringEnvironmentURLConnectionFactory
- SpringFactoriesLoaderUtils
- SpringProfilesURLConnectionAdapter
- SpringPropertySourcesURLConnectionAdapter
- SpringProtocolURLStreamHandler
- SpringResourceURLConnection
- SpringResourceURLConnectionAdapter
- SpringResourceURLConnectionFactory
- SpringSubProtocolURLConnectionFactory
- SpringVersion
- SpringVersionUtils
- TTLCachePut
- TTLCacheResolver
- TTLCacheable
- TTLCachingConfiguration
- TTLContext
- UnderScoreJoinAliasGenerator
- YamlPropertySource
- YamlPropertySourceFactory
spring-guice
spring-jdbc
- CompoundJdbcEventListenerFactory
- EnableP6DataSource
- NoOpP6LoadableOptions
- P6DataSourceBeanDefinitionRegistrar
- P6DataSourceBeanPostProcessor
- PropertySourcesP6LoadableOptionsAdapter
- SpringP6SpyURLConnectionFactory
spring-test
- AbstractWebFluxTest
- AbstractWebMvcTest
- AnnotatedTypeMetadataTestFactory
- EmbeddedDataBaseBeanDefinitionRegistrar
- EmbeddedDataBaseBeanDefinitionsRegistrar
- EmbeddedDatabaseType
- EmbeddedTomcatConfiguration
- EmbeddedTomcatContextLoader
- EmbeddedTomcatMergedContextConfiguration
- EmbeddedTomcatTestContextBootstrapper
- EmbeddedZookeeperServer
- EmbeddedZookeeperServerTestExecutionListener
- EnableEmbeddedDatabase
- EnableEmbeddedDatabases
- MockServletWebRequest
- PersonHandler
- PersonHandler
- RouterFunctionTestConfig
- RouterFunctionTestConfig
- ServletTestUtils
- SimpleUrlHandlerMappingTestConfig
- SimpleUrlHandlerMappingTestConfig
- SpringLoggingTest
- SpringTestUtils
- SpringTestWebUtils
- TestConditionContext
- TestController
- TestFilter
- TestFilterRegistration
- TestServlet
- TestServletContext
- TestServletContextListener
- TestServletRegistration
- User
- WebTestUtils
spring-web
- AbstractNameValueExpression
- AbstractWebEndpointMappingFactory
- AbstractWebRequestRule
- CompositeWebEndpointMappingRegistry
- CompositeWebRequestRule
- ConsumeMediaTypeExpression
- DelegatingHandlerMethodAdvice
- EnableWebExtension
- FilterRegistrationWebEndpointMappingFactory
- FilteringWebEndpointMappingRegistry
- GenericMediaTypeExpression
- HandlerMetadata
- HandlerMethodAdvice
- HandlerMethodArgumentInterceptor
- HandlerMethodArgumentsResolvedEvent
- HandlerMethodInterceptor
- HandlerMethodMetadata
- HttpUtils
- Jackson2WebEndpointMappingFactory
- MediaTypeExpression
- MediaTypeUtils
- NameValueExpression
- ProduceMediaTypeExpression
- PropertyConstants
- RegistrationWebEndpointMappingFactory
- RequestAttributesUtils
- RequestContextStrategy
- ServletRegistrationWebEndpointMappingFactory
- ServletWebEndpointMappingResolver
- SimpleWebEndpointMappingRegistry
- SmartWebEndpointMappingFactory
- SpringWebHelper
- SpringWebType
- UnknownSpringWebHelper
- WebEndpointMapping
- WebEndpointMappingFactory
- WebEndpointMappingFilter
- WebEndpointMappingRegistrar
- WebEndpointMappingRegistry
- WebEndpointMappingResolver
- WebEndpointMappingsReadyEvent
- WebEventPublisher
- WebExtensionBeanDefinitionRegistrar
- WebRequestConsumesRule
- WebRequestHeaderExpression
- WebRequestHeadersRule
- WebRequestMethodsRule
- WebRequestParamExpression
- WebRequestParamsRule
- WebRequestPattensRule
- WebRequestProducesRule
- WebRequestRule
- WebRequestUtils
- WebScope
- WebSource
- WebTarget
- WebType
- WebUtils
spring-webflux
- CompositeWebFilter
- ConsumingWebEndpointMappingAdapter
- DelegatingWebFilter
- EnableWebFluxExtension
- HandlerMappingWebEndpointMappingFactory
- HandlerMappingWebEndpointMappingResolver
- HandlerMetadataWebEndpointMappingFactory
- InterceptingHandlerMethodProcessor
- MonoUtils
- RequestContextWebFilter
- RequestHandledEventPublishingWebFilter
- RequestMappingMetadataWebEndpointMappingFactory
- RequestPredicateKind
- RequestPredicateVisitorAdapter
- ReversedProxyHandlerMapping
- RouterFunctionVisitorAdapter
- ServerRequestHandledEvent
- ServerWebRequest
- SpringWebFluxHelper
- StoringRequestBodyArgumentInterceptor
- StoringResponseBodyReturnValueInterceptor
- WebFluxExtensionBeanDefinitionRegistrar
- WebServerScope
- WebServerUtils
spring-webmvc
- AbstractPageRenderContextHandlerInterceptor
- AnnotatedMethodHandlerInterceptor
- ConfigurableContentNegotiationManagerWebMvcConfigurer
- ConsumingWebEndpointMappingAdapter
- ContentCachingFilter
- EnableWebMvcExtension
- EnableWebMvcExtensionListener
- ExclusiveViewResolverApplicationListener
- HandlerMappingWebEndpointMappingFactory
- HandlerMappingWebEndpointMappingResolver
- HandlerMetadataWebEndpointMappingFactory
- HandlerMethodArgumentResolverAdvice
- InterceptingHandlerMethodProcessor
- LazyCompositeHandlerInterceptor
- LoggingHandlerMethodArgumentResolverAdvice
- LoggingMethodHandlerInterceptor
- LoggingPageRenderContextHandlerInterceptor
- MethodHandlerInterceptor
- PropertyConstants
- RequestBodyAdviceAdapter
- RequestMappingMetadata
- RequestMappingMetadataWebEndpointMappingFactory
- RequestPredicateVisitorAdapter
- ResponseBodyAdviceAdapter
- ReversedProxyHandlerMapping
- RouterFunctionVisitorAdapter
- SpringWebMvcHelper
- StoringRequestBodyArgumentAdvice
- StoringResponseBodyReturnValueAdvice
- ViewResolverUtils
- ViewUtils
- WebMvcExtensionBeanDefinitionRegistrar
- WebMvcExtensionConfiguration
- WebMvcUtils
- WebUtils