-
Notifications
You must be signed in to change notification settings - Fork 39
io microsphere spring context annotation AnnotatedBeanDefinitionRegistryUtils
Type: Class | Module: microsphere-spring-context | Package: io.microsphere.spring.context.annotation | Since: 1.0.0
Annotated BeanDefinition Utilities
public abstract class AnnotatedBeanDefinitionRegistryUtils implements UtilsAuthor: Mercy
-
Introduced in:
1.0.0 -
Current Project Version:
0.2.36-SNAPSHOT
This component is tested and compatible with the following Java versions:
| Java Version | Status |
|---|---|
| Java 17 | ✅ Compatible |
| Java 21 | ✅ Compatible |
| Java 25 | ✅ Compatible |
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.");
}// 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);int componentCount = AnnotatedBeanDefinitionRegistryUtils.scanBasePackages(registry, "com.example.app");
System.out.println("Registered " + componentCount + " components.");BeanNameGenerator beanNameGenerator = AnnotatedBeanDefinitionRegistryUtils.resolveAnnotatedBeanNameGenerator(registry);ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry);
Set<BeanDefinitionHolder> holders = AnnotatedBeanDefinitionRegistryUtils.findBeanDefinitionHolders(scanner, "com.example.app");ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry);
BeanNameGenerator beanNameGenerator = AnnotatedBeanDefinitionRegistryUtils.resolveAnnotatedBeanNameGenerator(registry);
Set<BeanDefinitionHolder> holders = AnnotatedBeanDefinitionRegistryUtils.findBeanDefinitionHolders(scanner, "com.example.app", beanNameGenerator);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 io.microsphere.spring.context.annotation.AnnotatedBeanDefinitionRegistryUtils;| 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, |
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.
`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.");
}
}
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.
`// 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.
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.
`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.
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.
`BeanNameGenerator beanNameGenerator = AnnotatedBeanDefinitionRegistryUtils.resolveAnnotatedBeanNameGenerator(registry); `
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.
`ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry); Set holders = AnnotatedBeanDefinitionRegistryUtils.findBeanDefinitionHolders(scanner, "com.example.app"); `
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.
`ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry); BeanNameGenerator beanNameGenerator = AnnotatedBeanDefinitionRegistryUtils.resolveAnnotatedBeanNameGenerator(registry); Set holders = AnnotatedBeanDefinitionRegistryUtils.findBeanDefinitionHolders(scanner, "com.example.app", beanNameGenerator); `
BeanDefinition
This documentation was auto-generated from the source code of microsphere-spring.
spring-context
- AbstractInjectionPointDependencyResolver
- AbstractSmartLifecycle
- AbstractSpringResourceURLConnection
- AnnotatedBeanCapableImportBeanDefinitionRegistrar
- AnnotatedBeanCapableImportCandidate
- AnnotatedBeanCapableImportSelector
- AnnotatedBeanDefinitionRegistryUtils
- AnnotatedInjectionBeanPostProcessor
- AnnotatedInjectionPointDependencyResolver
- AnnotatedPropertySourceLoader
- AnnotationBeanDefinitionRegistryPostProcessor
- AnnotationUtils
- ApplicationContextUtils
- ApplicationEventInterceptor
- ApplicationEventInterceptorChain
- ApplicationListenerInterceptor
- ApplicationListenerInterceptorChain
- AutoRegistrationBean
- AutoRegistrationBeanInitializer
- AutoRegistrationBeanRegistrar
- AutowireCandidateResolvingListener
- AutowiredInjectionPointDependencyResolver
- BeanCapableImportCandidate
- BeanDefinitionUtils
- BeanDependencyResolver
- BeanFactoryListener
- BeanFactoryListenerAdapter
- BeanFactoryListeners
- BeanFactoryUtils
- BeanListener
- BeanListenerAdapter
- BeanListeners
- BeanMethodInjectionPointDependencyResolver
- BeanPropertyChangedEvent
- BeanRegistrar
- BeanSource
- BeanTimeStatistics
- BeanUtils
- CollectingConfigurationPropertyListener
- CompositeAutowireCandidateResolvingListener
- ConfigurableApplicationContextInitializer
- ConfigurationBeanAliasGenerator
- ConfigurationBeanBinder
- ConfigurationBeanBindingPostProcessor
- ConfigurationBeanBindingRegistrar
- ConfigurationBeanBindingsRegister
- ConfigurationBeanCustomizer
- ConfigurationPropertyOverrideAnnotationAttributesStrategy
- ConfigurationPropertyRepository
- ConstructionInjectionPointDependencyResolver
- ConversionServiceResolver
- ConversionServiceUtils
- DefaultApplicationEventInterceptorChain
- DefaultApplicationListenerInterceptorChain
- DefaultBeanDependencyResolver
- DefaultConfigurationBeanAliasGenerator
- DefaultConfigurationBeanBinder
- DefaultPropertiesPropertySource
- DefaultPropertiesPropertySourceLoader
- DefaultPropertiesPropertySources
- DefaultPropertiesPropertySourcesLoader
- DefaultResourceComparator
- DelegatingFactoryBean
- Dependency
- DependencyAnalysisBeanFactoryListener
- DependencyTreeWalker
- EnableAutoRegistrationBean
- EnableConfigurationBeanBinding
- EnableConfigurationBeanBindings
- EnableEventExtension
- EnableSpringConverterAdapter
- EnableSpringConverterAdapterRegistrar
- EnableTTLCaching
- EnvironmentEnabled
- EnvironmentListener
- EnvironmentUtils
- EventExtensionAttributes
- EventExtensionRegistrar
- EventPublishingBeanAfterProcessor
- EventPublishingBeanBeforeProcessor
- EventPublishingBeanInitializer
- ExposingClassPathBeanDefinitionScanner
- FilterMode
- GenericAnnotationAttributes
- GenericApplicationListenerAdapter
- GenericBeanNameGenerator
- GenericBeanPostProcessorAdapter
- HyphenAliasGenerator
- ImmutableMapPropertySource
- ImportOptional
- ImportOptionalSelector
- InjectionPointDependencyResolver
- InjectionPointDependencyResolvers
- InterceptingApplicationEventMulticaster
- InterceptingApplicationEventMulticasterProxy
- InterceptingApplicationListener
- JavaBeansPropertyChangeListenerAdapter
- JoinAliasGenerator
- JsonPropertySource
- JsonPropertySourceFactory
- ListenableAutowireCandidateResolver
- ListenableAutowireCandidateResolverInitializer
- ListenableConfigurableEnvironment
- ListenableConfigurableEnvironmentInitializer
- LoggingAutowireCandidateResolvingListener
- LoggingBeanFactoryListener
- LoggingBeanListener
- LoggingEnvironmentListener
- LoggingSmartLifecycle
- MethodParameterUtils
- MimeTypeUtils
- NamedBeanHolderComparator
- OnceApplicationContextEventListener
- OverrideAnnotationAttributes
- OverrideAnnotationAttributesStrategy
- ParallelPreInstantiationSingletonsBeanFactoryListener
- ProfileListener
- PropertiesUtils
- PropertyConstants
- PropertyResolverListener
- PropertyResolverUtils
- PropertySourceChangedEvent
- PropertySourceExtension
- PropertySourceExtensionAttributes
- PropertySourceExtensionLoader
- PropertySourcesChangedEvent
- PropertySourcesUtils
- PropertyValuesUtils
- ResolvableDependencyTypeFilter
- ResolvablePlaceholderAnnotationAttributes
- ResourceInjectionPointDependencyResolver
- ResourceLoaderUtils
- ResourcePropertySource
- ResourcePropertySourceLoader
- ResourcePropertySources
- ResourcePropertySourcesLoader
- ResourceUtils
- ResourceYamlProcessor
- SpringConverterAdapter
- SpringDelegatingBeanProtocolURLConnectionFactory
- SpringEnvironmentURLConnectionFactory
- SpringFactoriesLoaderUtils
- SpringProfilesURLConnectionAdapter
- SpringPropertySourcesURLConnectionAdapter
- SpringProtocolURLStreamHandler
- SpringResourceURLConnection
- SpringResourceURLConnectionAdapter
- SpringResourceURLConnectionFactory
- SpringSubProtocolURLConnectionFactory
- SpringVersion
- SpringVersionUtils
- TTLCachePut
- TTLCacheResolver
- TTLCacheable
- TTLCachingConfiguration
- TTLContext
- UnderScoreJoinAliasGenerator
- YamlPropertySource
- YamlPropertySourceFactory
spring-guice
spring-jdbc
- CompoundJdbcEventListenerFactory
- EnableP6DataSource
- NoOpP6LoadableOptions
- P6DataSourceBeanDefinitionRegistrar
- P6DataSourceBeanPostProcessor
- PropertySourcesP6LoadableOptionsAdapter
- SpringP6SpyURLConnectionFactory
spring-test
- AbstractWebFluxTest
- AbstractWebMvcTest
- AnnotatedTypeMetadataTestFactory
- EmbeddedDataBaseBeanDefinitionRegistrar
- EmbeddedDataBaseBeanDefinitionsRegistrar
- EmbeddedDatabaseType
- EmbeddedTomcatConfiguration
- EmbeddedTomcatContextLoader
- EmbeddedTomcatMergedContextConfiguration
- EmbeddedTomcatTestContextBootstrapper
- EmbeddedZookeeperServer
- EmbeddedZookeeperServerTestExecutionListener
- EnableEmbeddedDatabase
- EnableEmbeddedDatabases
- MockServletWebRequest
- PersonHandler
- PersonHandler
- RouterFunctionTestConfig
- RouterFunctionTestConfig
- ServletTestUtils
- SimpleUrlHandlerMappingTestConfig
- SimpleUrlHandlerMappingTestConfig
- SpringLoggingTest
- SpringTestUtils
- SpringTestWebUtils
- TestConditionContext
- TestController
- TestFilter
- TestFilterRegistration
- TestServlet
- TestServletContext
- TestServletContextListener
- TestServletRegistration
- User
- WebTestUtils
spring-web
- AbstractNameValueExpression
- AbstractWebEndpointMappingFactory
- AbstractWebRequestRule
- CompositeWebEndpointMappingRegistry
- CompositeWebRequestRule
- ConsumeMediaTypeExpression
- DelegatingHandlerMethodAdvice
- EnableWebExtension
- FilterRegistrationWebEndpointMappingFactory
- FilteringWebEndpointMappingRegistry
- GenericMediaTypeExpression
- HandlerMetadata
- HandlerMethodAdvice
- HandlerMethodArgumentInterceptor
- HandlerMethodArgumentsResolvedEvent
- HandlerMethodInterceptor
- HandlerMethodMetadata
- HttpUtils
- Jackson2WebEndpointMappingFactory
- MediaTypeExpression
- MediaTypeUtils
- NameValueExpression
- ProduceMediaTypeExpression
- PropertyConstants
- RegistrationWebEndpointMappingFactory
- RequestAttributesUtils
- RequestContextStrategy
- ServletRegistrationWebEndpointMappingFactory
- ServletWebEndpointMappingResolver
- SimpleWebEndpointMappingRegistry
- SmartWebEndpointMappingFactory
- SpringWebHelper
- SpringWebType
- UnknownSpringWebHelper
- WebEndpointMapping
- WebEndpointMappingFactory
- WebEndpointMappingFilter
- WebEndpointMappingRegistrar
- WebEndpointMappingRegistry
- WebEndpointMappingResolver
- WebEndpointMappingsReadyEvent
- WebEventPublisher
- WebExtensionBeanDefinitionRegistrar
- WebRequestConsumesRule
- WebRequestHeaderExpression
- WebRequestHeadersRule
- WebRequestMethodsRule
- WebRequestParamExpression
- WebRequestParamsRule
- WebRequestPattensRule
- WebRequestProducesRule
- WebRequestRule
- WebRequestUtils
- WebScope
- WebSource
- WebTarget
- WebType
- WebUtils
spring-webflux
- CompositeWebFilter
- ConsumingWebEndpointMappingAdapter
- DelegatingWebFilter
- EnableWebFluxExtension
- HandlerMappingWebEndpointMappingFactory
- HandlerMappingWebEndpointMappingResolver
- HandlerMetadataWebEndpointMappingFactory
- InterceptingHandlerMethodProcessor
- MonoUtils
- RequestContextWebFilter
- RequestHandledEventPublishingWebFilter
- RequestMappingMetadataWebEndpointMappingFactory
- RequestPredicateKind
- RequestPredicateVisitorAdapter
- ReversedProxyHandlerMapping
- RouterFunctionVisitorAdapter
- ServerRequestHandledEvent
- ServerWebRequest
- SpringWebFluxHelper
- StoringRequestBodyArgumentInterceptor
- StoringResponseBodyReturnValueInterceptor
- WebFluxExtensionBeanDefinitionRegistrar
- WebServerScope
- WebServerUtils
spring-webmvc
- AbstractPageRenderContextHandlerInterceptor
- AnnotatedMethodHandlerInterceptor
- ConfigurableContentNegotiationManagerWebMvcConfigurer
- ConsumingWebEndpointMappingAdapter
- ContentCachingFilter
- EnableWebMvcExtension
- EnableWebMvcExtensionListener
- ExclusiveViewResolverApplicationListener
- HandlerMappingWebEndpointMappingFactory
- HandlerMappingWebEndpointMappingResolver
- HandlerMetadataWebEndpointMappingFactory
- HandlerMethodArgumentResolverAdvice
- InterceptingHandlerMethodProcessor
- LazyCompositeHandlerInterceptor
- LoggingHandlerMethodArgumentResolverAdvice
- LoggingMethodHandlerInterceptor
- LoggingPageRenderContextHandlerInterceptor
- MethodHandlerInterceptor
- PropertyConstants
- RequestBodyAdviceAdapter
- RequestMappingMetadata
- RequestMappingMetadataWebEndpointMappingFactory
- RequestPredicateVisitorAdapter
- ResponseBodyAdviceAdapter
- ReversedProxyHandlerMapping
- RouterFunctionVisitorAdapter
- SpringWebMvcHelper
- StoringRequestBodyArgumentAdvice
- StoringResponseBodyReturnValueAdvice
- ViewResolverUtils
- ViewUtils
- WebMvcExtensionBeanDefinitionRegistrar
- WebMvcExtensionConfiguration
- WebMvcUtils
- WebUtils