Skip to content

io microsphere spring context annotation AnnotatedBeanDefinitionRegistryUtils

github-actions[bot] edited this page Jul 11, 2026 · 25 revisions

AnnotatedBeanDefinitionRegistryUtils

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

Source: microsphere-spring-context/src/main/java/io/microsphere/spring/context/annotation/AnnotatedBeanDefinitionRegistryUtils.java

Overview

Annotated BeanDefinition Utilities

Declaration

public abstract class AnnotatedBeanDefinitionRegistryUtils implements Utils

Author: Mercy

Version Information

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

isPresentBean

boolean isBeanPresent = AnnotatedBeanDefinitionRegistryUtils.isPresentBean(registry, MyService.class);
if (isBeanPresent) {
    System.out.println("MyService is already registered.");
} else {
    System.out.println("MyService is not registered yet.");
}

registerBeans

// Register MyService and MyRepository if not already registered
AnnotatedBeanDefinitionRegistryUtils.registerBeans(registry, MyService.class, MyRepository.class);
// Register MyService and MyRepository if not already registered
AnnotatedBeanDefinitionRegistryUtils.registerBeans(registry, MyService.class, MyRepository.class);

scanBasePackages

int componentCount = AnnotatedBeanDefinitionRegistryUtils.scanBasePackages(registry, "com.example.app");
System.out.println("Registered " + componentCount + " components.");

resolveAnnotatedBeanNameGenerator

BeanNameGenerator beanNameGenerator = AnnotatedBeanDefinitionRegistryUtils.resolveAnnotatedBeanNameGenerator(registry);

findBeanDefinitionHolders

ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry);
Set<BeanDefinitionHolder> holders = AnnotatedBeanDefinitionRegistryUtils.findBeanDefinitionHolders(scanner, "com.example.app");

findBeanDefinitionHolders

ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry);
BeanNameGenerator beanNameGenerator = AnnotatedBeanDefinitionRegistryUtils.resolveAnnotatedBeanNameGenerator(registry);
Set<BeanDefinitionHolder> holders = AnnotatedBeanDefinitionRegistryUtils.findBeanDefinitionHolders(scanner, "com.example.app", beanNameGenerator);

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.context.annotation.AnnotatedBeanDefinitionRegistryUtils;

API Reference

Public Methods

Method Description
isPresentBean Checks whether a bean defined by the specified annotated class is already present in the registry.
registerBeans Registers the specified annotated classes as beans in the given BeanDefinitionRegistry,
scanBasePackages Scans the specified base packages for Spring components annotated with stereotypes such as
resolveAnnotatedBeanNameGenerator Resolves the appropriate BeanNameGenerator instance for generating bean names during annotation-based configuration.
findBeanDefinitionHolders Scans the specified package for candidate components (beans) using the provided scanner,
findBeanDefinitionHolders Scans the specified package for candidate components (beans) using the provided scanner,

Method Details

isPresentBean

public static boolean isPresentBean(BeanDefinitionRegistry registry, Class<?> annotatedClass)

Checks whether a bean defined by the specified annotated class is already present in the registry.

This method iterates over all bean definitions in the registry and compares the class of the annotation metadata with the provided class. If a match is found, it returns true, indicating that the bean is already registered.

Example Usage

`boolean isBeanPresent = AnnotatedBeanDefinitionRegistryUtils.isPresentBean(registry, MyService.class);
if (isBeanPresent) {
    System.out.println("MyService is already registered.");
` else {
    System.out.println("MyService is not registered yet.");
}
}

registerBeans

public static void registerBeans(BeanDefinitionRegistry registry, Class<?>... annotatedClasses)

Registers the specified annotated classes as beans in the given BeanDefinitionRegistry, if they are not already present.

This method ensures idempotent registration by first checking whether each class is already registered using the #isPresentBean(BeanDefinitionRegistry, Class) method. Only those classes that are not yet registered will be processed for bean registration.

Example Usage

`// Register MyService and MyRepository if not already registered
AnnotatedBeanDefinitionRegistryUtils.registerBeans(registry, MyService.class, MyRepository.class);
`

If the provided array of classes is empty or null, this method will return immediately without performing any operations.

scanBasePackages

public static int scanBasePackages(BeanDefinitionRegistry registry, String... basePackages)

Scans the specified base packages for Spring components annotated with stereotypes such as Component @Component, and registers them as beans in the provided registry.

This method returns the number of beans that were registered during the scan. It ensures idempotent behavior by logging the scanned components at TRACE level if enabled.

Example Usage

`int componentCount = AnnotatedBeanDefinitionRegistryUtils.scanBasePackages(registry, "com.example.app");
System.out.println("Registered " + componentCount + " components.");
`

If the provided array of package names is empty or null, this method will return 0 without performing any operations.

resolveAnnotatedBeanNameGenerator

public static BeanNameGenerator resolveAnnotatedBeanNameGenerator(BeanDefinitionRegistry registry)

Resolves the appropriate BeanNameGenerator instance for generating bean names during annotation-based configuration.

It'd better to use BeanNameGenerator instance that should reference ConfigurationClassPostProcessor#componentScanBeanNameGenerator, thus it maybe a potential problem on bean name generation.

This method attempts to retrieve an existing BeanNameGenerator from the registry if it implements the SingletonBeanRegistry interface. The bean name generator is typically named AnnotationConfigUtils#CONFIGURATION_BEAN_NAME_GENERATOR. If it cannot be found, a new instance of AnnotationBeanNameGenerator is created and returned as a fallback.

Note: It is preferable to use the shared instance from the registry (if available), such as the one used by Spring's ConfigurationClassPostProcessor, to ensure consistent bean naming. Failing to do so may lead to discrepancies in bean name generation.

Example Usage

`BeanNameGenerator beanNameGenerator = AnnotatedBeanDefinitionRegistryUtils.resolveAnnotatedBeanNameGenerator(registry);
`

findBeanDefinitionHolders

public static Set<BeanDefinitionHolder> findBeanDefinitionHolders(ClassPathBeanDefinitionScanner scanner,
                                                                      String packageToScan)

Scans the specified package for candidate components (beans) using the provided scanner, generates bean names, and returns a set of BeanDefinitionHolder objects encapsulating the found bean definitions.

This method is typically used during component scanning to locate beans annotated with Spring stereotypes such as Component @Component.

Example Usage

`ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry);
Set holders = AnnotatedBeanDefinitionRegistryUtils.findBeanDefinitionHolders(scanner, "com.example.app");
`

findBeanDefinitionHolders

public static Set<BeanDefinitionHolder> findBeanDefinitionHolders(ClassPathBeanDefinitionScanner scanner,
                                                                      String packageToScan,
                                                                      BeanNameGenerator beanNameGenerator)

Scans the specified package for candidate components (beans) using the provided scanner, generates bean names using the given bean name generator, and returns a set of BeanDefinitionHolder objects encapsulating the found bean definitions.

This method is typically used during component scanning to locate beans annotated with Spring stereotypes such as Component @Component.

Example Usage

`ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry);
BeanNameGenerator beanNameGenerator = AnnotatedBeanDefinitionRegistryUtils.resolveAnnotatedBeanNameGenerator(registry);
Set holders = AnnotatedBeanDefinitionRegistryUtils.findBeanDefinitionHolders(scanner, "com.example.app", beanNameGenerator);
`

See Also

  • BeanDefinition

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