Class AnnotatedBeanDefinitionRegistryUtils
- java.lang.Object
-
- io.microsphere.spring.context.annotation.AnnotatedBeanDefinitionRegistryUtils
-
- All Implemented Interfaces:
io.microsphere.util.Utils
public abstract class AnnotatedBeanDefinitionRegistryUtils extends java.lang.Object implements io.microsphere.util.Utils
AnnotatedBeanDefinition
Utilities- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
BeanDefinition
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.util.Set<org.springframework.beans.factory.config.BeanDefinitionHolder>
findBeanDefinitionHolders(org.springframework.context.annotation.ClassPathBeanDefinitionScanner scanner, java.lang.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 ofBeanDefinitionHolder
objects encapsulating the found bean definitions.static boolean
isPresentBean(org.springframework.beans.factory.support.BeanDefinitionRegistry registry, java.lang.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, java.lang.Class<?>... annotatedClasses)
Registers the specified annotated classes as beans in the givenBeanDefinitionRegistry
, if they are not already present.static org.springframework.beans.factory.support.BeanNameGenerator
resolveAnnotatedBeanNameGenerator(org.springframework.beans.factory.support.BeanDefinitionRegistry registry)
Resolves the appropriateBeanNameGenerator
instance for generating bean names during annotation-based configuration.static int
scanBasePackages(org.springframework.beans.factory.support.BeanDefinitionRegistry registry, java.lang.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.
-
-
-
Method Detail
-
isPresentBean
public static boolean isPresentBean(org.springframework.beans.factory.support.BeanDefinitionRegistry registry, java.lang.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
- theBeanDefinitionRegistry
to check for the presence of the beanannotatedClass
- 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, java.lang.Class<?>... annotatedClasses)
Registers the specified annotated classes as beans in the givenBeanDefinitionRegistry
, 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
- theBeanDefinitionRegistry
where beans will be registeredannotatedClasses
- 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, java.lang.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
- theBeanDefinitionRegistry
where beans will be registeredbasePackages
- 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 appropriateBeanNameGenerator
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 theSingletonBeanRegistry
interface. The bean name generator is typically namedAnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR
. If it cannot be found, a new instance ofAnnotationBeanNameGenerator
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
- theBeanDefinitionRegistry
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 java.util.Set<org.springframework.beans.factory.config.BeanDefinitionHolder> findBeanDefinitionHolders(org.springframework.context.annotation.ClassPathBeanDefinitionScanner scanner, java.lang.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 ofBeanDefinitionHolder
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
- theClassPathBeanDefinitionScanner
used to scan for componentspackageToScan
- the package to scan for Spring componentsregistry
- theBeanDefinitionRegistry
used to generate and register bean namesbeanNameGenerator
- theBeanNameGenerator
used to generate bean names for discovered components- Returns:
- a non-null set of
BeanDefinitionHolder
instances representing the discovered bean definitions
-
-