Class AnnotatedBeanDefinitionRegistryUtils

java.lang.Object
io.microsphere.spring.context.annotation.AnnotatedBeanDefinitionRegistryUtils
All Implemented Interfaces:
io.microsphere.util.Utils

public abstract class AnnotatedBeanDefinitionRegistryUtils extends Object implements io.microsphere.util.Utils
Annotated BeanDefinition Utilities
Since:
1.0.0
Author:
Mercy
See Also:
  • BeanDefinition
  • Method Summary

    Modifier and Type
    Method
    Description
    static Set<org.springframework.beans.factory.config.BeanDefinitionHolder>
    findBeanDefinitionHolders(org.springframework.context.annotation.ClassPathBeanDefinitionScanner scanner, String packageToScan, org.springframework.beans.factory.support.BeanDefinitionRegistry registry, org.springframework.beans.factory.support.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.
    static boolean
    isPresentBean(org.springframework.beans.factory.support.BeanDefinitionRegistry registry, Class<?> annotatedClass)
    Checks whether a bean defined by the specified annotated class is already present in the registry.
    static void
    registerBeans(org.springframework.beans.factory.support.BeanDefinitionRegistry registry, Class<?>... annotatedClasses)
    Registers the specified annotated classes as beans in the given BeanDefinitionRegistry, if they are not already present.
    static org.springframework.beans.factory.support.BeanNameGenerator
    resolveAnnotatedBeanNameGenerator(org.springframework.beans.factory.support.BeanDefinitionRegistry registry)
    Resolves the appropriate BeanNameGenerator instance for generating bean names during annotation-based configuration.
    static int
    scanBasePackages(org.springframework.beans.factory.support.BeanDefinitionRegistry registry, String... basePackages)
    Scans the specified base packages for Spring components annotated with stereotypes such as @Component, and registers them as beans in the provided registry.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isPresentBean

      public static boolean isPresentBean(org.springframework.beans.factory.support.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.");
       }
       
      Parameters:
      registry - the BeanDefinitionRegistry to check for the presence of the bean
      annotatedClass - the annotated class to check in the registry
      Returns:
      true if the bean defined by the annotated class is present, false otherwise
    • registerBeans

      public static void registerBeans(org.springframework.beans.factory.support.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.

      Parameters:
      registry - the BeanDefinitionRegistry where beans will be registered
      annotatedClasses - one or more annotated classes to register as beans if not already present
    • scanBasePackages

      public static int scanBasePackages(org.springframework.beans.factory.support.BeanDefinitionRegistry registry, String... basePackages)
      Scans the specified base packages for Spring components annotated with stereotypes such as @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.

      Parameters:
      registry - the BeanDefinitionRegistry where beans will be registered
      basePackages - one or more package names to scan for components
      Returns:
      the number of beans registered from the scanned packages
    • resolveAnnotatedBeanNameGenerator

      @Nonnull public static org.springframework.beans.factory.support.BeanNameGenerator resolveAnnotatedBeanNameGenerator(org.springframework.beans.factory.support.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);
       
      Parameters:
      registry - the BeanDefinitionRegistry used to look up or create a bean name generator
      Returns:
      a non-null instance of BeanNameGenerator
      See Also:
      • AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR
      • AnnotationBeanNameGenerator
      • ConfigurationClassPostProcessor.processConfigBeanDefinitions(org.springframework.beans.factory.support.BeanDefinitionRegistry)
      • SingletonBeanRegistry
    • findBeanDefinitionHolders

      @Nonnull public static Set<org.springframework.beans.factory.config.BeanDefinitionHolder> findBeanDefinitionHolders(org.springframework.context.annotation.ClassPathBeanDefinitionScanner scanner, String packageToScan, org.springframework.beans.factory.support.BeanDefinitionRegistry registry, org.springframework.beans.factory.support.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.

      Example Usage

      
       ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry);
       BeanNameGenerator beanNameGenerator = AnnotatedBeanDefinitionRegistryUtils.resolveAnnotatedBeanNameGenerator(registry);
       Set<BeanDefinitionHolder> holders = AnnotatedBeanDefinitionRegistryUtils.findBeanDefinitionHolders(scanner, "com.example.app", registry, beanNameGenerator);
       
      Parameters:
      scanner - the ClassPathBeanDefinitionScanner used to scan for components
      packageToScan - the package to scan for Spring components
      registry - the BeanDefinitionRegistry used to generate and register bean names
      beanNameGenerator - the BeanNameGenerator used to generate bean names for discovered components
      Returns:
      a non-null set of BeanDefinitionHolder instances representing the discovered bean definitions