-
Notifications
You must be signed in to change notification settings - Fork 39
io microsphere spring beans factory config BeanDefinitionUtils
Type: Class | Module: microsphere-spring-context | Package: io.microsphere.spring.beans.factory.config | Since: 1.0.0
BeanDefinition Utilities class
public abstract class BeanDefinitionUtils implements UtilsAuthor: Mercy
-
Introduced in:
1.0.0 -
Current Project Version:
0.2.7-SNAPSHOT
This component is tested and compatible with the following Java versions:
| Java Version | Status |
|---|---|
| Java 17 | ✅ Compatible |
| Java 21 | ✅ Compatible |
| Java 25 | ✅ Compatible |
Class<?> beanType = MyService.class;
AbstractBeanDefinition beanDefinition = genericBeanDefinition(beanType);Class<?> beanType = MyService.class;
Object[] constructorArgs = new Object[]{"arg1", 123};
AbstractBeanDefinition beanDefinition = genericBeanDefinition(beanType, constructorArgs);Class<?> beanType = MyService.class;
int role = BeanDefinition.ROLE_APPLICATION;
AbstractBeanDefinition beanDefinition = genericBeanDefinition(beanType, role);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);RootBeanDefinition beanDefinition = new RootBeanDefinition();
Class<?> beanType = resolveBeanType(beanDefinition);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 internallyConfigurableListableBeanFactory 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);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);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);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.beans.factory.config.BeanDefinitionUtils;| 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. |
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.
`Class beanType = MyService.class; AbstractBeanDefinition beanDefinition = genericBeanDefinition(beanType); `
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.
`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.
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.
`Class beanType = MyService.class; int role = BeanDefinition.ROLE_APPLICATION; AbstractBeanDefinition beanDefinition = genericBeanDefinition(beanType, role); `
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.
`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.
`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.
public static AbstractBeanDefinition genericBeanDefinition(Class<?> beanType, Consumer<BeanDefinitionBuilder> builderConsumer)Build a generic instance of AbstractBeanDefinition with the given bean type and builder consumer.
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.
`RootBeanDefinition beanDefinition = new RootBeanDefinition(); Class beanType = resolveBeanType(beanDefinition); `
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.
`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 `
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.
`ConfigurableListableBeanFactory beanFactory = ...; // Obtain from ApplicationContext
Set infrastructureBeanNames = findInfrastructureBeanNames(beanFactory);
for (String beanName : infrastructureBeanNames) {
System.out.println("Infrastructure Bean: " + beanName);
`
}
`ApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class); ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); Set infrastructureBeans = findInfrastructureBeanNames(beanFactory); `
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.
`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);
`
}
`Predicate isInfrastructure = bd -> bd.getRole() == BeanDefinition.ROLE_INFRASTRUCTURE;
Predicate hasAInName = bd -> bd.getBeanClassName() != null && bd.getBeanClassName().contains("A");
Set beanNames = findBeanNames(beanFactory, isInfrastructure, hasAInName);
`
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.
`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 infrastructureBeans = BeanDefinitionUtils.findBeanNames(beanFactory,
BeanDefinitionUtils::isInfrastructureBean);
`
AbstractBeanDefinitionBeanDefinition#ROLE_APPLICATIONBeanDefinition#ROLE_INFRASTRUCTURE
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