Skip to content

io microsphere spring beans BeanUtils

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

BeanUtils

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

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

Overview

Bean Utilities Class

Declaration

public abstract class BeanUtils 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

generateBeanName

String beanName = BeanUtils.generateBeanName(MyService.class);
// Returns "myService"

String anotherBeanName = BeanUtils.generateBeanName(XMLParser.class);
// Returns "xmlParser"

generateBeanName

String beanName = BeanUtils.generateBeanName("com.example.MyService");
// Returns "myService"

String anotherBeanName = BeanUtils.generateBeanName("com.example.XMLParser");
// Returns "xmlParser"

isBeanPresent

boolean present = isBeanPresent(beanFactory, MyService.class, true);

isBeanPresent

boolean present = isBeanPresent(beanFactory, "com.example.MyService");

isBeanPresent

boolean present = isBeanPresent(beanFactory, "com.example.MyService", true);

isBeanPresent

boolean present = isBeanPresent(beanFactory, "myService", MyService.class);

getBeanNames

String[] beanNames = getBeanNames(beanFactory, MyService.class);

getBeanNames

String[] beanNames = getBeanNames(beanFactory, MyService.class, true);

getBeanNames

String[] beanNames = getBeanNames(beanFactory, MyService.class);

getBeanNames

String[] beanNames = getBeanNames(beanFactory, MyService.class, true);

resolveBeanType

Class<?> beanType = resolveBeanType("com.example.MyService", classLoader);

getOptionalBean

MyService myService = getOptionalBean(beanFactory, MyService.class);

getOptionalBean

MyService myService = BeanUtils.getOptionalBean(beanFactory, MyService.class);

getBeanIfAvailable

MyService myService = getBeanIfAvailable(beanFactory, "myService", MyService.class);

getSortedBeans

List<MyService> myServices = getSortedBeans(beanFactory, MyService.class);

getSortedBeans

List<MyService> myServices = getSortedBeans(beanFactory, MyService.class);

initializeBean

MyBean myBean = new MyBean();
ApplicationContext context = ...; // Obtain or create an ApplicationContext
BeanUtils.initializeBean(myBean, context);

invokeBeanInterfaces

MyBean myBean = new MyBean();
ApplicationContext context = ...; // Obtain or create an ApplicationContext
invokeBeanInterfaces(myBean, context);

invokeBeanInterfaces

MyBean myBean = new MyBean();
ConfigurableApplicationContext context = ...; // Obtain or create a ConfigurableApplicationContext
BeanUtils.invokeBeanInterfaces(myBean, context);

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.BeanUtils;

API Reference

Public Methods

Method Description
generateBeanName Generate a default bean name for the given bean class.
generateBeanName Generate a default bean name for the given class name.
isBeanPresent Is Bean Present or not?
isBeanPresent Check if a bean of the specified class is present in the given ListableBeanFactory.
isBeanPresent Check if a bean with the specified class name is present in the given ListableBeanFactory.
isBeanPresent Check if a bean with the specified class name is present in the given ListableBeanFactory.
isBeanPresent Check if a bean with the specified name and class is present in the given BeanFactory.
getBeanNames Get bean names of the specified type from the given ListableBeanFactory.
getBeanNames Get Bean Names from ListableBeanFactory by type.
getBeanNames Get bean names of the specified type from the given ConfigurableListableBeanFactory.
getBeanNames Get bean names of the specified type from the given ConfigurableListableBeanFactory.
resolveBeanType Resolve the bean type from the given class name using the specified ClassLoader.
getOptionalBean Retrieve an optional bean of the specified type from the given ListableBeanFactory.
getOptionalBean Retrieve an optional bean of the specified type from the given ListableBeanFactory.
getBeanIfAvailable Retrieve the bean with the specified name and type from the given BeanFactory if it exists.
getSortedBeans Retrieve a list of beans of the specified type from the given BeanFactory, sorted based on their order.
getSortedBeans Retrieve all beans of the specified type from the given ListableBeanFactory, sorted based on their order.
initializeBean Initialize the given bean instance by invoking its standard Spring lifecycle interfaces.
invokeBeanInterfaces Invokes the standard Spring bean lifecycle interfaces in a specific order for the given bean and application context.
invokeBeanInterfaces Invokes the standard Spring bean lifecycle interfaces in a specific order for the given bean and application context.

Method Details

generateBeanName

public static String generateBeanName(@Nonnull Class<?> beanClass)

Generate a default bean name for the given bean class.

The generated bean name is derived from the short name of the class (without package qualification), with the first character decapitalized according to JavaBeans convention.

Example Usage

`String beanName = BeanUtils.generateBeanName(MyService.class);
// Returns "myService"

String anotherBeanName = BeanUtils.generateBeanName(XMLParser.class);
// Returns "xmlParser"
`

Behavior

- If the class name starts with multiple uppercase letters (e.g., `XMLParser`), only the first character is decapitalized if followed by a lowercase letter, otherwise it remains as is per `java.beans.Introspector#decapitalize(String)` rules.
- The method uses `org.springframework.util.ClassUtils#getShortName(Class)` to extract the simple class name.

generateBeanName

public static String generateBeanName(@Nonnull String className)

Generate a default bean name for the given class name.

The generated bean name is derived from the short name of the class (without package qualification), with the first character decapitalized according to JavaBeans convention.

Example Usage

`String beanName = BeanUtils.generateBeanName("com.example.MyService");
// Returns "myService"

String anotherBeanName = BeanUtils.generateBeanName("com.example.XMLParser");
// Returns "xmlParser"
`

Behavior

- If the class name starts with multiple uppercase letters (e.g., `XMLParser`), only the first character is decapitalized if followed by a lowercase letter, otherwise it remains as is per `java.beans.Introspector#decapitalize(String)` rules.
- The method uses `org.springframework.util.ClassUtils#getShortName(String)` to extract the simple class name.

isBeanPresent

public static boolean isBeanPresent(@Nonnull ListableBeanFactory beanFactory, @Nonnull Class<?> beanClass,
                                        boolean includingAncestors)

Check if a bean of the specified class is present in the given ListableBeanFactory.

This method checks whether there is at least one bean of the specified type in the bean factory. If the parameter includingAncestors is set to true, it will also check ancestor bean factories.

Example Usage

`boolean present = isBeanPresent(beanFactory, MyService.class, true);
`

Behavior

- If the bean class is null or not resolvable, an empty array will be returned.
- If no beans are found and logging is enabled at trace level, a log message will be recorded.

isBeanPresent

public static boolean isBeanPresent(@Nonnull ListableBeanFactory beanFactory, @Nonnull String beanClassName)

Check if a bean with the specified class name is present in the given ListableBeanFactory.

This method checks whether there is at least one bean with the specified class name in the bean factory. The check includes only the current bean factory and does not search in ancestor factories.

Example Usage

`boolean present = isBeanPresent(beanFactory, "com.example.MyService");
`

Behavior

- The method resolves the class using the bean factory's class loader (if available).
- If the class cannot be resolved or no bean is found, it returns false.

isBeanPresent

public static boolean isBeanPresent(@Nonnull ListableBeanFactory beanFactory, @Nonnull String beanClassName,
                                        boolean includingAncestors)

Check if a bean with the specified class name is present in the given ListableBeanFactory.

This method checks whether there is at least one bean with the specified class name in the bean factory. If the parameter includingAncestors is set to true, it will also check ancestor bean factories.

Example Usage

`boolean present = isBeanPresent(beanFactory, "com.example.MyService", true);
`

Behavior

- The method resolves the class using the bean factory's class loader (if available).
- If the class cannot be resolved or no bean is found, it returns false.
- If logging is enabled at trace level and no bean is found, a log message will be recorded.

isBeanPresent

public static boolean isBeanPresent(@Nonnull BeanFactory beanFactory, @Nonnull String beanName,
                                        @Nonnull Class<?> beanClass)

Check if a bean with the specified name and class is present in the given BeanFactory.

This method verifies whether a bean exists in the bean factory by both its name and type. It ensures that the bean is present and matches the expected class type.

Example Usage

`boolean present = isBeanPresent(beanFactory, "myService", MyService.class);
`

Behavior

- Returns false if either the bean factory, bean name, or bean class is null.
- If logging is enabled at trace level, a message will be logged when the bean is not found.

getBeanNames

public static String[] getBeanNames(@Nonnull ListableBeanFactory beanFactory, @Nonnull Class<?> beanClass)

Get bean names of the specified type from the given ListableBeanFactory.

This method retrieves the names of all beans that match the specified class type. The search is limited to the current bean factory and does not include ancestor factories.

Example Usage

`String[] beanNames = getBeanNames(beanFactory, MyService.class);
`

Behavior

- Returns an empty array if no beans of the specified type are found.
- If logging is enabled at trace level, a message will be logged when no beans are found.

getBeanNames

public static String[] getBeanNames(@Nonnull ListableBeanFactory beanFactory, @Nonnull Class<?> beanClass,
                                        @Nonnull boolean includingAncestors)

Get Bean Names from ListableBeanFactory by type.

This method retrieves the names of all beans that match the specified class type. If the parameter includingAncestors is set to true, it will also check ancestor bean factories.

Example Usage

`String[] beanNames = getBeanNames(beanFactory, MyService.class, true);
`

Behavior

- If no beans are found and logging is enabled at trace level, a log message will be recorded.
- Returns an empty array if no beans of the specified type are found.

getBeanNames

public static String[] getBeanNames(@Nonnull ConfigurableListableBeanFactory beanFactory, @Nonnull Class<?> beanClass)

Get bean names of the specified type from the given ConfigurableListableBeanFactory.

This method retrieves the names of all beans that match the specified class type. The search is limited to the current bean factory and does not include ancestor factories.

Example Usage

`String[] beanNames = getBeanNames(beanFactory, MyService.class);
`

Behavior

- Returns an empty array if no beans of the specified type are found.
- If logging is enabled at trace level, a message will be logged when no beans are found.

getBeanNames

public static String[] getBeanNames(@Nonnull ConfigurableListableBeanFactory beanFactory, @Nonnull Class<?> beanClass,
                                        boolean includingAncestors)

Get bean names of the specified type from the given ConfigurableListableBeanFactory.

This method retrieves the names of all beans that match the specified class type. If the parameter includingAncestors is set to true, it will also check ancestor bean factories.

Example Usage

`String[] beanNames = getBeanNames(beanFactory, MyService.class, true);
`

Behavior

- If no beans are found and logging is enabled at trace level, a log message will be recorded.
- Returns an empty array if no beans of the specified type are found.

resolveBeanType

public static Class<?> resolveBeanType(@Nullable String beanClassName, @Nullable ClassLoader classLoader)

Resolve the bean type from the given class name using the specified ClassLoader.

This method attempts to load the class with the provided name using the given ClassLoader. If the class cannot be found, it returns null. Otherwise, it returns the resolved class. Additionally, if the provided class name is null or empty, the method will return null.

Example Usage

`Class beanType = resolveBeanType("com.example.MyService", classLoader);
`

Behavior

- If the class name is null or empty, it returns null.
- If the class cannot be loaded, it logs a warning and returns null.
- Otherwise, it returns the resolved class.

getOptionalBean

public static <T> T getOptionalBean(@Nonnull ListableBeanFactory beanFactory, @Nonnull Class<T> beanClass,
                                        boolean includingAncestors)

Retrieve an optional bean of the specified type from the given ListableBeanFactory.

This method attempts to find and return a single bean of the specified class. If no beans are found, it returns null. If multiple beans are found, it throws a NoUniqueBeanDefinitionException.

Example Usage

`MyService myService = getOptionalBean(beanFactory, MyService.class);
`

Behavior

- If no beans of the specified type are found, returns `null`.
- If multiple beans are found, throws a `NoUniqueBeanDefinitionException`.
- If logging is enabled at trace level, logs a message when no bean is found.
- If logging is enabled at error level, logs any exception thrown during bean retrieval.

getOptionalBean

public static <T> T getOptionalBean(@Nonnull ListableBeanFactory beanFactory, @Nonnull Class<T> beanClass)

Retrieve an optional bean of the specified type from the given ListableBeanFactory.

This method attempts to find and return a single bean of the specified class. If no beans are found, it returns null. If multiple beans are found, it throws a NoUniqueBeanDefinitionException.

Example Usage

`MyService myService = BeanUtils.getOptionalBean(beanFactory, MyService.class);
`

Behavior

- If no beans of the specified type are found, returns `null`.
- If multiple beans are found, throws a `NoUniqueBeanDefinitionException`.
- If logging is enabled at trace level, logs a message when no bean is found.
- If logging is enabled at error level, logs any exception thrown during bean retrieval.

getBeanIfAvailable

public static <T> T getBeanIfAvailable(@Nonnull BeanFactory beanFactory, @Nonnull String beanName,
                                           @Nonnull Class<T> beanType)

Retrieve the bean with the specified name and type from the given BeanFactory if it exists.

This method checks whether a bean with the specified name and type exists in the bean factory. If it does, the bean is returned; otherwise, null is returned.

Example Usage

`MyService myService = getBeanIfAvailable(beanFactory, "myService", MyService.class);
`

Behavior

- If the bean is not present, returns `null`.
- If logging is enabled at trace level, logs a message when the bean is not found.

getSortedBeans

public static <T> List<T> getSortedBeans(@Nonnull BeanFactory beanFactory, @Nonnull Class<T> type)

Retrieve a list of beans of the specified type from the given BeanFactory, sorted based on their order.

If the provided BeanFactory is an instance of ListableBeanFactory, it delegates to #getSortedBeans(ListableBeanFactory, Class) for retrieving and sorting the beans. Otherwise, it returns an empty list.

Example Usage

`List myServices = getSortedBeans(beanFactory, MyService.class);
`

Behavior

- Returns an unmodifiable list of beans sorted by their order.
- If no beans are found, returns an empty list.

getSortedBeans

public static <T> List<T> getSortedBeans(@Nonnull ListableBeanFactory beanFactory, @Nonnull Class<T> type)

Retrieve all beans of the specified type from the given ListableBeanFactory, sorted based on their order.

This method retrieves a map of bean names to bean instances matching the specified type and converts it into a list. The list is then sorted using AnnotationAwareOrderComparator based on the beans' order.

Example Usage

`List myServices = getSortedBeans(beanFactory, MyService.class);
`

Behavior

- Returns an unmodifiable list of beans sorted by their order.
- If no beans are found, returns an empty list.

initializeBean

public static void initializeBean(@Nullable Object bean, @Nullable ApplicationContext context)

Initialize the given bean instance by invoking its standard Spring lifecycle interfaces.

This method triggers the invocation of various Aware interfaces and the InitializingBean#afterPropertiesSet() method if the bean implements them. It effectively simulates the initialization phase of a Spring bean within the provided ApplicationContext.

Example Usage

`MyBean myBean = new MyBean();
ApplicationContext context = ...; // Obtain or create an ApplicationContext
BeanUtils.initializeBean(myBean, context);
`

Behavior

- If the bean is `null`, no action is taken.
- If the context is `null`, only basic factory-aware interfaces might be invoked depending on implementation details, but typically context-aware interfaces are skipped.
- The method delegates to `#invokeBeanInterfaces(Object, ApplicationContext)` to perform the actual interface invocations.

invokeBeanInterfaces

public static void invokeBeanInterfaces(@Nullable Object bean, @Nullable ApplicationContext context)

Invokes the standard Spring bean lifecycle interfaces in a specific order for the given bean and application context.

This method delegates to #invokeBeanInterfaces(Object, ConfigurableApplicationContext) after converting the provided ApplicationContext into a ConfigurableApplicationContext, allowing further processing if needed.

Invoke Spring Bean interfaces in order:

- `BeanNameAware`
- `BeanClassLoaderAware`
- `BeanFactoryAware`
- `EnvironmentAware`
- `EmbeddedValueResolverAware`
- `ResourceLoaderAware`
- `ApplicationEventPublisherAware`
- `MessageSourceAware`
- `ApplicationStartupAware` (Spring Framework 5.3+)
- `ApplicationContextAware`
- `InitializingBean`

Example Usage

`MyBean myBean = new MyBean();
ApplicationContext context = ...; // Obtain or create an ApplicationContext
invokeBeanInterfaces(myBean, context);
`

Behavior

- If the provided bean implements any of the supported lifecycle interfaces, their respective methods will be invoked.
- The order of invocation follows the standard Spring lifecycle contract.

invokeBeanInterfaces

public static void invokeBeanInterfaces(@Nullable Object bean, @Nullable ConfigurableApplicationContext context)

Invokes the standard Spring bean lifecycle interfaces in a specific order for the given bean and application context.

This method ensures that all relevant lifecycle callbacks are executed in the correct sequence, starting with the Aware interface methods followed by the InitializingBean#afterPropertiesSet() method.

Example Usage

`MyBean myBean = new MyBean();
ConfigurableApplicationContext context = ...; // Obtain or create a ConfigurableApplicationContext
BeanUtils.invokeBeanInterfaces(myBean, context);
`

Lifecycle Invocation Order

- `BeanNameAware`
- `BeanClassLoaderAware`
- `BeanFactoryAware`
- `EnvironmentAware`
- `EmbeddedValueResolverAware`
- `ResourceLoaderAware`
- `ApplicationEventPublisherAware`
- `MessageSourceAware`
- `ApplicationStartupAware` (Spring Framework 5.3+)
- `ApplicationContextAware`
- `InitializingBean`

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