Skip to content

io microsphere spring beans factory config BeanDefinitionUtils

github-actions[bot] edited this page Apr 11, 2026 · 26 revisions

BeanDefinitionUtils

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

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

Overview

BeanDefinition Utilities class

Declaration

public abstract class BeanDefinitionUtils implements Utils

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.2.10-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

genericBeanDefinition

Class<?> beanType = MyService.class;
AbstractBeanDefinition beanDefinition = genericBeanDefinition(beanType);

genericBeanDefinition

Class<?> beanType = MyService.class;
Object[] constructorArgs = new Object[]{"arg1", 123};
AbstractBeanDefinition beanDefinition = genericBeanDefinition(beanType, constructorArgs);

genericBeanDefinition

Class<?> beanType = MyService.class;
int role = BeanDefinition.ROLE_APPLICATION;
AbstractBeanDefinition beanDefinition = genericBeanDefinition(beanType, role);

genericBeanDefinition

Class<?> beanType = MyService.class;
int role = BeanDefinition.ROLE_APPLICATION;
Object[] constructorArgs = new Object[]{"arg1", 123};
AbstractBeanDefinition beanDefinition = genericBeanDefinition(beanType, role, constructorArgs);
Class<?> beanType = MyRepository.class;
int role = BeanDefinition.ROLE_INFRASTRUCTURE;
AbstractBeanDefinition beanDefinition = genericBeanDefinition(beanType, role);

resolveBeanType

RootBeanDefinition beanDefinition = new RootBeanDefinition();
Class<?> beanType = resolveBeanType(beanDefinition);

resolveBeanType

RootBeanDefinition beanDefinition = new RootBeanDefinition();
ClassLoader classLoader = getClass().getClassLoader();
Class<?> beanType = resolveBeanType(beanDefinition, classLoader);
RootBeanDefinition beanDefinition = new RootBeanDefinition();
Class<?> beanType = resolveBeanType(beanDefinition, null); // Uses default class loader internally

findInfrastructureBeanNames

ConfigurableListableBeanFactory beanFactory = ...; // Obtain from ApplicationContext
Set<String> infrastructureBeanNames = findInfrastructureBeanNames(beanFactory);
for (String beanName : infrastructureBeanNames) {
    System.out.println("Infrastructure Bean: " + beanName);
}
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class);
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
Set<String> infrastructureBeans = findInfrastructureBeanNames(beanFactory);

findBeanNames

ConfigurableListableBeanFactory beanFactory = ...; // Obtain from ApplicationContext
Set<String> beanNames = findBeanNames(beanFactory, bd -> bd.getRole() == BeanDefinition.ROLE_APPLICATION);
for (String name : beanNames) {
    System.out.println("Application Bean: " + name);
}
Predicate<BeanDefinition> isInfrastructure = bd -> bd.getRole() == BeanDefinition.ROLE_INFRASTRUCTURE;
Predicate<BeanDefinition> hasAInName = bd -> bd.getBeanClassName() != null && bd.getBeanClassName().contains("A");

Set<String> beanNames = findBeanNames(beanFactory, isInfrastructure, hasAInName);

isInfrastructureBean

BeanDefinition beanDefinition = ...; // Obtain from bean factory
if (BeanDefinitionUtils.isInfrastructureBean(beanDefinition)) {
    System.out.println("This is an infrastructure bean.");
} else {
    System.out.println("This is not an infrastructure bean.");
}
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
Set<String> infrastructureBeans = BeanDefinitionUtils.findBeanNames(beanFactory,
    BeanDefinitionUtils::isInfrastructureBean);

Usage

Maven Dependency

Add the following dependency to your pom.xml:

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

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

Import

import io.microsphere.spring.beans.factory.config.BeanDefinitionUtils;

API Reference

Public Methods

Method Description
genericBeanDefinition Build a generic instance of AbstractBeanDefinition with the given bean type.
genericBeanDefinition Build a generic instance of AbstractBeanDefinition
genericBeanDefinition Build a generic instance of AbstractBeanDefinition with the specified bean type and role.
genericBeanDefinition Build a generic instance of AbstractBeanDefinition with the specified bean type, role, and constructor arguments.
genericBeanDefinition Build a generic instance of AbstractBeanDefinition with the given bean type and builder consumer.
resolveBeanType Resolves the bean type from the given RootBeanDefinition using the default class loader.
resolveBeanType Resolves the bean type from the given RootBeanDefinition using the specified class loader.
findInfrastructureBeanNames Find the names of all infrastructure beans in the given bean factory.
findBeanNames Find bean names that match the given predicate(s) in the provided ConfigurableListableBeanFactory.
isInfrastructureBean Determine whether the given bean definition represents an infrastructure bean.

Method Details

genericBeanDefinition

public static AbstractBeanDefinition genericBeanDefinition(Class<?> beanType)

Build a generic instance of AbstractBeanDefinition with the given bean type.

This method is a convenience wrapper that calls #genericBeanDefinition(Class, int, Object[]) with default role (BeanDefinition#ROLE_APPLICATION) and no constructor arguments.

Example Usage

`Class beanType = MyService.class;
AbstractBeanDefinition beanDefinition = genericBeanDefinition(beanType);
`

genericBeanDefinition

public static AbstractBeanDefinition genericBeanDefinition(Class<?> beanType, Object... constructorArguments)

Build a generic instance of AbstractBeanDefinition

This method creates a bean definition for the specified bean type with optional constructor arguments. It internally uses BeanDefinitionBuilder to construct the bean definition and sets the provided constructor arguments via constructor injection.

Example Usage

`Class beanType = MyService.class;
Object[] constructorArgs = new Object[]{"arg1", 123`;
AbstractBeanDefinition beanDefinition = genericBeanDefinition(beanType, constructorArgs);
}

The above example will create a bean definition for the class MyService, passing in two constructor arguments.

genericBeanDefinition

public static AbstractBeanDefinition genericBeanDefinition(Class<?> beanType, int role)

Build a generic instance of AbstractBeanDefinition with the specified bean type and role.

This method is a convenience wrapper that calls #genericBeanDefinition(Class, int, Object[]) with no constructor arguments.

Example Usage

`Class beanType = MyService.class;
int role = BeanDefinition.ROLE_APPLICATION;
AbstractBeanDefinition beanDefinition = genericBeanDefinition(beanType, role);
`

genericBeanDefinition

public static AbstractBeanDefinition genericBeanDefinition(Class<?> beanType, int role, Object[] constructorArguments)

Build a generic instance of AbstractBeanDefinition with the specified bean type, role, and constructor arguments.

This method uses BeanDefinitionBuilder to construct a bean definition for the given bean type, sets its role, and injects any provided constructor arguments via constructor injection.

Example Usage

Basic Bean Definition

`Class beanType = MyService.class;
int role = BeanDefinition.ROLE_APPLICATION;
Object[] constructorArgs = new Object[]{"arg1", 123`;
AbstractBeanDefinition beanDefinition = genericBeanDefinition(beanType, role, constructorArgs);
}

The above example creates a bean definition for the class MyService, assigning it the role of an application bean, and passing in two constructor arguments.

No Constructor Arguments

`Class beanType = MyRepository.class;
int role = BeanDefinition.ROLE_INFRASTRUCTURE;
AbstractBeanDefinition beanDefinition = genericBeanDefinition(beanType, role);
`

In this case, no constructor arguments are provided, so the default constructor will be used.

genericBeanDefinition

public static AbstractBeanDefinition genericBeanDefinition(Class<?> beanType, Consumer<BeanDefinitionBuilder> builderConsumer)

Build a generic instance of AbstractBeanDefinition with the given bean type and builder consumer.

resolveBeanType

public static Class<?> resolveBeanType(RootBeanDefinition beanDefinition)

Resolves the bean type from the given RootBeanDefinition using the default class loader.

This method attempts to resolve the bean type via its ResolvableType. If that fails, it falls back to resolving the bean class using the bean's class name and the default class loader.

Example Usage

`RootBeanDefinition beanDefinition = new RootBeanDefinition();
Class beanType = resolveBeanType(beanDefinition);
`

resolveBeanType

public static Class<?> resolveBeanType(RootBeanDefinition beanDefinition, @Nullable ClassLoader classLoader)

Resolves the bean type from the given RootBeanDefinition using the specified class loader.

This method attempts to resolve the bean type via its ResolvableType. If that fails, it falls back to resolving the bean class using the bean's class name and the provided class loader.

Example Usage

Basic Resolution

`RootBeanDefinition beanDefinition = new RootBeanDefinition();
ClassLoader classLoader = getClass().getClassLoader();
Class beanType = resolveBeanType(beanDefinition, classLoader);
`

Resolution with Null Class Loader

`RootBeanDefinition beanDefinition = new RootBeanDefinition();
Class beanType = resolveBeanType(beanDefinition, null); // Uses default class loader internally
`

findInfrastructureBeanNames

public static Set<String> findInfrastructureBeanNames(ConfigurableListableBeanFactory beanFactory)

Find the names of all infrastructure beans in the given bean factory.

An infrastructure bean is typically a bean with the role BeanDefinition#ROLE_INFRASTRUCTURE. These beans are usually not intended for direct use by application code.

Example Usage

Basic Usage

`ConfigurableListableBeanFactory beanFactory = ...; // Obtain from ApplicationContext
Set infrastructureBeanNames = findInfrastructureBeanNames(beanFactory);
for (String beanName : infrastructureBeanNames) {
    System.out.println("Infrastructure Bean: " + beanName);
`
}

Integration with Spring Context

`ApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class);
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
Set infrastructureBeans = findInfrastructureBeanNames(beanFactory);
`

findBeanNames

public static Set<String> findBeanNames(@Nullable ConfigurableListableBeanFactory beanFactory, Predicate<? super BeanDefinition>... predicates)

Find bean names that match the given predicate(s) in the provided ConfigurableListableBeanFactory.

This method combines multiple predicates using logical AND and tests each bean definition against the combined predicate. If a bean definition matches, its name is added to the result set.

Example Usage

Basic Filtering

`ConfigurableListableBeanFactory beanFactory = ...; // Obtain from ApplicationContext
Set beanNames = findBeanNames(beanFactory, bd -> bd.getRole() == BeanDefinition.ROLE_APPLICATION);
for (String name : beanNames) {
    System.out.println("Application Bean: " + name);
`
}

Combining Predicates

`Predicate isInfrastructure = bd -> bd.getRole() == BeanDefinition.ROLE_INFRASTRUCTURE;
Predicate hasAInName = bd -> bd.getBeanClassName() != null && bd.getBeanClassName().contains("A");

Set beanNames = findBeanNames(beanFactory, isInfrastructure, hasAInName);
`

isInfrastructureBean

public static boolean isInfrastructureBean(@Nullable BeanDefinition beanDefinition)

Determine whether the given bean definition represents an infrastructure bean.

An infrastructure bean is defined by having the role BeanDefinition#ROLE_INFRASTRUCTURE. These beans are typically internal to the framework or libraries and are not intended for direct use by application code.

Example Usage

Basic Check

`BeanDefinition beanDefinition = ...; // Obtain from bean factory
if (BeanDefinitionUtils.isInfrastructureBean(beanDefinition)) {
    System.out.println("This is an infrastructure bean.");
` else {
    System.out.println("This is not an infrastructure bean.");
}
}

Filtering Infrastructure Beans

`ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
Set infrastructureBeans = BeanDefinitionUtils.findBeanNames(beanFactory,
    BeanDefinitionUtils::isInfrastructureBean);
`

See Also

  • AbstractBeanDefinition
  • BeanDefinition#ROLE_APPLICATION
  • BeanDefinition#ROLE_INFRASTRUCTURE

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