-
Notifications
You must be signed in to change notification settings - Fork 39
io microsphere spring beans factory support BeanRegistrar
Type: Class | Module: microsphere-spring-context | Package: io.microsphere.spring.beans.factory.support | Since: 1.0.0
BeanRegistrar provides utility methods for registering beans within a Spring BeanDefinitionRegistry.
This abstract class offers various static methods to register bean definitions, singleton beans, and factory beans. It supports functionalities such as bean overriding control, role-based registration, and custom naming strategies.
- Register infrastructure or application beans with auto-generated or explicit names.
- Control bean overriding behavior during registration.
- Supports registration from Spring factories using `spring.factories` resources.
- Provides logging via the Microsphere Logger for trace, debug, warn, and error levels.
`boolean registered = BeanRegistrar.registerInfrastructureBean(registry, MyService.class);
if (registered) {
logger.info("Infrastructure bean registered successfully.");
` else {
logger.warn("Infrastructure bean was already registered.");
}
}
`boolean registered = BeanRegistrar.registerBeanDefinition(registry, "customName", MyService.class);
if (registered) {
logger.info("Bean registered under name 'customName'.");
`
}
`MySingleton mySingleton = new MySingleton(); BeanRegistrar.registerSingleton(registry, "mySingleton", mySingleton); `
`int count = BeanRegistrar.registerSpringFactoriesBeans(registry, MyFactory.class);
logger.info("{` beans registered from spring.factories.", count);
}
public abstract class BeanRegistrarAuthor: 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 registered = BeanRegistrar.registerInfrastructureBean(registry, MyService.class);
if (registered) {
logger.info("Infrastructure bean registered successfully.");
} else {
logger.warn("Infrastructure bean was already registered.");
}boolean registered = BeanRegistrar.registerBeanDefinition(registry, "customName", MyService.class);
if (registered) {
logger.info("Bean registered under name 'customName'.");
}MySingleton mySingleton = new MySingleton();
BeanRegistrar.registerSingleton(registry, "mySingleton", mySingleton);int count = BeanRegistrar.registerSpringFactoriesBeans(registry, MyFactory.class);
logger.info("{} beans registered from spring.factories.", count);boolean registered = registerInfrastructureBean(registry, MyInfrastructureComponent.class);
if (registered) {
logger.info("Infrastructure bean registered successfully with auto-generated name.");
} else {
logger.warn("Infrastructure bean was already registered.");
}// Register with auto-generated name
boolean registered = registerInfrastructureBean(registry, null, MyInfrastructureComponent.class);
// Register with explicit name
boolean registered = registerInfrastructureBean(registry, "myInfraBean", MyInfrastructureComponent.class);
if (registered) {
logger.info("Infrastructure bean registered successfully.");
} else {
logger.warn("Infrastructure bean was already registered.");
}boolean registered = registerBeanDefinition(registry, MyBean.class);
if (registered) {
System.out.println("Bean registered successfully.");
} else {
System.out.println("Bean was already registered.");
}// Register with auto-generated name
boolean registered = registerBeanDefinition(registry, null, MyBean.class);
// Register with explicit name
boolean registered = registerBeanDefinition(registry, "myCustomBean", MyBean.class);
if (registered) {
System.out.println("Bean registered successfully.");
} else {
System.out.println("Bean was already registered.");
}// Register with auto-generated name and constructor arguments
boolean registered = registerBeanDefinition(registry, null, MyBean.class, "arg1", 42);
// Register with explicit name and constructor arguments
boolean registered = registerBeanDefinition(registry, "myCustomBean", MyBean.class, "arg1", 42);
if (registered) {
System.out.println("Bean registered successfully.");
} else {
System.out.println("Bean was already registered.");
}// Register as an infrastructure bean (ROLE_INFRASTRUCTURE)
boolean registered = registerBeanDefinition(registry, null, MyInfrastructure.class, BeanDefinition.ROLE_INFRASTRUCTURE);
// Register as a support bean (ROLE_SUPPORT)
boolean registered = registerBeanDefinition(registry, "mySupportBean", MySupport.class, BeanDefinition.ROLE_SUPPORT);
if (registered) {
System.out.println("Bean registered successfully with the specified role.");
} else {
System.out.println("Bean was already registered.");
}// Register with auto-generated name and custom property
boolean registered = registerBeanDefinition(registry, MyBean.class, builder ->
builder.addPropertyValue("myProperty", "myValue")
);
// Register with auto-generated name and custom constructor argument
boolean registered = registerBeanDefinition(registry, MyBean.class, builder ->
builder.addConstructorArgValue("arg1")
);
if (registered) {
System.out.println("Bean registered successfully with custom configuration.");
} else {
System.out.println("Bean was already registered.");
}// Register with auto-generated name and custom property
boolean registered = registerBeanDefinition(registry, null, MyBean.class, builder ->
builder.addPropertyValue("myProperty", "myValue")
);
// Register with explicit name and custom constructor argument
boolean registered = registerBeanDefinition(registry, "myCustomBean", MyBean.class, builder ->
builder.addConstructorArgValue("arg1")
);
if (registered) {
System.out.println("Bean registered successfully with custom configuration.");
} else {
System.out.println("Bean was already registered.");
}// Register as a primary bean
boolean registered = registerBeanDefinition(registry, MyService.class, true);
if (registered) {
System.out.println("Primary bean registered successfully.");
} else {
System.out.println("Bean was already registered.");
}// Register as a primary bean with auto-generated name
boolean registered = registerBeanDefinition(registry, null, MyService.class, true);
// Register as a non-primary bean with explicit name
boolean registered = registerBeanDefinition(registry, "mySecondaryService", MyService.class, false);
if (registered) {
System.out.println("Bean registered successfully with primary flag.");
} else {
System.out.println("Bean was already registered.");
}BeanDefinition beanDefinition = genericBeanDefinition(MyService.class);
boolean registered = registerBeanDefinition(registry, beanDefinition);
if (registered) {
System.out.println("Bean registered successfully with auto-generated name.");
} else {
System.out.println("Bean was already registered.");
}// Register with auto-generated name
BeanDefinition beanDefinition = genericBeanDefinition(MyService.class);
boolean registered = registerBeanDefinition(registry, null, beanDefinition);
// Register with explicit name
BeanDefinition anotherDefinition = genericBeanDefinition(AnotherService.class);
boolean registered = registerBeanDefinition(registry, "anotherService", anotherDefinition);
if (registered) {
System.out.println("Bean registered successfully.");
} else {
System.out.println("Bean was already registered.");
}// Register with auto-generated name, allowing overriding
BeanDefinition beanDefinition = genericBeanDefinition(MyService.class);
boolean registered = registerBeanDefinition(registry, null, beanDefinition, true);
// Register with explicit name, disallowing overriding
BeanDefinition anotherDefinition = genericBeanDefinition(AnotherService.class);
boolean registered = registerBeanDefinition(registry, "anotherService", anotherDefinition, false);
if (registered) {
System.out.println("Bean registered successfully.");
} else {
System.out.println("Bean was not registered (already exists or error).");
}MyService myService = new MyServiceImpl();
registerSingleton(registry, "myService", myService);boolean hasAlias = BeanRegistrar.hasAlias(registry, "myBean", "myAlias");
if (hasAlias) {
logger.info("Alias 'myAlias' is registered for bean 'myBean'.");
} else {
logger.info("Alias 'myAlias' is not registered for bean 'myBean'.");
}// Assuming beanFactory is an instance of ConfigurableApplicationContext or similar
Map<Class, String> registeredBeans = registerSpringFactoriesBeans(beanFactory, MyFactory.class);
registeredBeans.forEach((clazz, name) -> {
System.out.println("Registered bean: " + name + " of type: " + clazz.getName());
});Map<Class, String> registeredBeans = registerSpringFactoriesBeans(registry, MyFactory.class);
registeredBeans.forEach((clazz, name) -> {
System.out.println("Registered bean: " + name + " of type: " + clazz.getName());
});String beanName = BeanUtils.generateBeanName("io.example.MyServiceImpl"); // returns "myServiceImpl"MyService myServiceInstance = new MyServiceImpl();
BeanRegistrar.registerFactoryBean(registry, "myServiceFactory", myServiceInstance);MyService myService = new MyServiceImpl();
registerFactoryBean(registry, "myServiceFactory", myService, false);registerFactoryBean(registry, "myPrimaryServiceFactory", myService, true);MyService myService = new MyServiceImpl();
BeanRegistrar.registerBean(registry, "myService", myService);MyService myService = new MyServiceImpl();
BeanRegistrar.registerBean(registry, "myService", myService, false);BeanRegistrar.registerBean(registry, "myPrimaryService", myService, true);AnotherBean anotherBean = new AnotherBean();
BeanRegistrar.registerBean(registry, "anotherBean", anotherBean, false);List<Class<?>> beanClasses = Arrays.asList(MyService.class, MyRepository.class);
Map<Class<?>, String> registeredBeans = registerGenericBeans(registry, beanClasses);
registeredBeans.forEach((clazz, name) -> {
logger.info("Registered bean: {} of type: {}", name, clazz.getName());
});Map<Class<?>, String> registeredBeans = registerGenericBeans(registry, MyService.class, MyRepository.class);
registeredBeans.forEach((clazz, name) -> {
logger.info("Registered bean: {} of type: {}", name, clazz.getName());
});Entry<String, Boolean> result = registerGenericBean(registry, MyService.class);
String beanName = result.getKey(); // "myService"
boolean registered = result.getValue();
if (registered) {
logger.info("Bean registered with name: {}", beanName);
} else {
logger.warn("Bean with name '{}' was already registered.", beanName);
}BeanNameGenerator customGenerator = new CustomBeanNameGenerator();
Entry<String, Boolean> result = registerGenericBean(registry, MyService.class, customGenerator);
String beanName = result.getKey();
boolean registered = result.getValue();
if (registered) {
logger.info("Bean registered with name: {}", beanName);
} else {
logger.warn("Bean with name '{}' was already registered.", beanName);
}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.support.BeanRegistrar;| Method | Description |
|---|---|
registerInfrastructureBean |
Registers an infrastructure bean with the specified type into the given registry. |
registerInfrastructureBean |
Registers an infrastructure bean with the specified type and optional name into the given registry. |
registerBeanDefinition |
Register a BeanDefinition for the specified bean type. |
registerBeanDefinition |
Register a BeanDefinition for the specified bean type with an optional name. |
registerBeanDefinition |
Register a BeanDefinition for the specified bean type with constructor arguments. |
registerBeanDefinition |
Registers a BeanDefinition for the specified bean type with a specific role. |
registerBeanDefinition |
Registers a BeanDefinition for the specified bean type with custom configuration via a builder consumer. |
registerBeanDefinition |
Registers a BeanDefinition for the specified bean type with custom configuration via a builder consumer. |
registerBeanDefinition |
Registers a BeanDefinition for the specified bean type with the primary flag. |
registerBeanDefinition |
Registers a BeanDefinition for the specified bean type with the primary flag and an optional name. |
registerBeanDefinition |
Registers a BeanDefinition into the given registry. |
registerBeanDefinition |
Registers a BeanDefinition with an optional name into the given registry. |
registerBeanDefinition |
Registers a BeanDefinition with an optional name and overriding control into the given registry. |
registerSingleton |
Registers a singleton bean with the specified name into the given registry. |
hasAlias |
Checks whether the specified alias is associated with the given bean name in the registry. |
registerFactoryBean |
Registers beans into the Spring BeanFactory by loading their implementation class names from |
registerFactoryBean |
Registers a org.springframework.beans.factory.FactoryBean with the specified name into the given registry. |
registerBean |
Registers a bean instance with the specified name into the given registry. |
registerBean |
Registers a bean instance with the specified name and primary status into the given registry. |
registerInfrastructureBean |
Registers generic bean definitions for the specified collection of bean classes into the given registry. |
public static boolean registerInfrastructureBean(BeanDefinitionRegistry registry, Class<?> beanType)Registers an infrastructure bean with the specified type into the given registry.
This method delegates to #registerInfrastructureBean(BeanDefinitionRegistry, String, Class)
with a null bean name, causing a unique bean name to be generated automatically.
The registered bean definition will have its role set to BeanDefinition#ROLE_INFRASTRUCTURE.
`boolean registered = registerInfrastructureBean(registry, MyInfrastructureComponent.class);
if (registered) {
logger.info("Infrastructure bean registered successfully with auto-generated name.");
` else {
logger.warn("Infrastructure bean was already registered.");
}
}
public static boolean registerInfrastructureBean(BeanDefinitionRegistry registry, @Nullable String beanName, Class<?> beanType)Registers an infrastructure bean with the specified type and optional name into the given registry.
This method creates a generic bean definition for the provided bean type with the infrastructure role,
uses the provided bean name if present, or generates a unique bean name otherwise. It then attempts to
register the bean definition. If the bean is successfully registered, it returns true; otherwise,
it returns false.
`// Register with auto-generated name
boolean registered = registerInfrastructureBean(registry, null, MyInfrastructureComponent.class);
// Register with explicit name
boolean registered = registerInfrastructureBean(registry, "myInfraBean", MyInfrastructureComponent.class);
if (registered) {
logger.info("Infrastructure bean registered successfully.");
` else {
logger.warn("Infrastructure bean was already registered.");
}
}
public static boolean registerBeanDefinition(BeanDefinitionRegistry registry, Class<?> beanType)Register a BeanDefinition for the specified bean type.
This method generates a unique bean name based on the provided bean type and registers its bean definition
in the given registry. If the bean definition is successfully registered, it returns true;
otherwise, it returns false.
`boolean registered = registerBeanDefinition(registry, MyBean.class);
if (registered) {
System.out.println("Bean registered successfully.");
` else {
System.out.println("Bean was already registered.");
}
}
public static boolean registerBeanDefinition(BeanDefinitionRegistry registry, @Nullable String beanName, Class<?> beanType)Register a BeanDefinition for the specified bean type with an optional name.
This method creates a generic bean definition for the provided bean type. If a bean name is provided,
it will be used; otherwise, a unique bean name will be generated automatically based on the bean type.
The bean definition is then registered in the given registry. If the bean is successfully registered,
it returns true; otherwise, it returns false.
`// Register with auto-generated name
boolean registered = registerBeanDefinition(registry, null, MyBean.class);
// Register with explicit name
boolean registered = registerBeanDefinition(registry, "myCustomBean", MyBean.class);
if (registered) {
System.out.println("Bean registered successfully.");
` else {
System.out.println("Bean was already registered.");
}
}
public static boolean registerBeanDefinition(BeanDefinitionRegistry registry, @Nullable String beanName,
Class<?> beanType, Object... constructorArguments)Register a BeanDefinition for the specified bean type with constructor arguments.
This method creates a generic bean definition for the provided bean type, applying the specified
constructor arguments. If a bean name is provided, it will be used; otherwise, a unique bean name
will be generated automatically based on the bean type. The bean definition is then registered in
the given registry. If the bean is successfully registered, it returns true; otherwise,
it returns false.
`// Register with auto-generated name and constructor arguments
boolean registered = registerBeanDefinition(registry, null, MyBean.class, "arg1", 42);
// Register with explicit name and constructor arguments
boolean registered = registerBeanDefinition(registry, "myCustomBean", MyBean.class, "arg1", 42);
if (registered) {
System.out.println("Bean registered successfully.");
` else {
System.out.println("Bean was already registered.");
}
}
public static boolean registerBeanDefinition(BeanDefinitionRegistry registry, @Nullable String beanName,
Class<?> beanType, int role)Registers a BeanDefinition for the specified bean type with a specific role.
This method creates a generic bean definition for the provided bean type and assigns it the specified role.
If a bean name is provided, it will be used; otherwise, a unique bean name will be generated automatically
based on the bean type. The bean definition is then registered in the given registry. If the bean is
successfully registered, it returns true; otherwise, it returns false.
`// Register as an infrastructure bean (ROLE_INFRASTRUCTURE)
boolean registered = registerBeanDefinition(registry, null, MyInfrastructure.class, BeanDefinition.ROLE_INFRASTRUCTURE);
// Register as a support bean (ROLE_SUPPORT)
boolean registered = registerBeanDefinition(registry, "mySupportBean", MySupport.class, BeanDefinition.ROLE_SUPPORT);
if (registered) {
System.out.println("Bean registered successfully with the specified role.");
` else {
System.out.println("Bean was already registered.");
}
}
public static boolean registerBeanDefinition(BeanDefinitionRegistry registry, Class<?> beanType,
Consumer<BeanDefinitionBuilder> builderConsumer)Registers a BeanDefinition for the specified bean type with custom configuration via a builder consumer.
This method creates a generic bean definition for the provided bean type and applies custom configurations
using the specified Consumer of BeanDefinitionBuilder. A unique bean name will be generated
automatically based on the bean type. The bean definition is then registered in the given registry.
If the bean is successfully registered, it returns true; otherwise, it returns false.
`// Register with auto-generated name and custom property
boolean registered = registerBeanDefinition(registry, MyBean.class, builder ->
builder.addPropertyValue("myProperty", "myValue")
);
// Register with auto-generated name and custom constructor argument
boolean registered = registerBeanDefinition(registry, MyBean.class, builder ->
builder.addConstructorArgValue("arg1")
);
if (registered) {
System.out.println("Bean registered successfully with custom configuration.");
` else {
System.out.println("Bean was already registered.");
}
}
public static boolean registerBeanDefinition(BeanDefinitionRegistry registry, @Nullable String beanName,
Class<?> beanType, Consumer<BeanDefinitionBuilder> builderConsumer)Registers a BeanDefinition for the specified bean type with custom configuration via a builder consumer.
This method creates a generic bean definition for the provided bean type and applies custom configurations
using the specified Consumer of BeanDefinitionBuilder. If a bean name is provided,
it will be used; otherwise, a unique bean name will be generated automatically based on the bean type.
The bean definition is then registered in the given registry. If the bean is successfully registered,
it returns true; otherwise, it returns false.
`// Register with auto-generated name and custom property
boolean registered = registerBeanDefinition(registry, null, MyBean.class, builder ->
builder.addPropertyValue("myProperty", "myValue")
);
// Register with explicit name and custom constructor argument
boolean registered = registerBeanDefinition(registry, "myCustomBean", MyBean.class, builder ->
builder.addConstructorArgValue("arg1")
);
if (registered) {
System.out.println("Bean registered successfully with custom configuration.");
` else {
System.out.println("Bean was already registered.");
}
}
public static boolean registerBeanDefinition(BeanDefinitionRegistry registry, Class<?> beanType, boolean primary)Registers a BeanDefinition for the specified bean type with the primary flag.
This method creates a generic bean definition for the provided bean type and sets its primary status.
A unique bean name will be generated automatically based on the bean type. The bean definition is then
registered in the given registry. If the bean is successfully registered, it returns true;
otherwise, it returns false.
`// Register as a primary bean
boolean registered = registerBeanDefinition(registry, MyService.class, true);
if (registered) {
System.out.println("Primary bean registered successfully.");
` else {
System.out.println("Bean was already registered.");
}
}
public static boolean registerBeanDefinition(BeanDefinitionRegistry registry, @Nullable String beanName,
Class<?> beanType, boolean primary)Registers a BeanDefinition for the specified bean type with the primary flag and an optional name.
This method creates a generic bean definition for the provided bean type and sets its primary status.
If a bean name is provided, it will be used; otherwise, a unique bean name will be generated automatically
based on the bean type. The bean definition is then registered in the given registry. If the bean is
successfully registered, it returns true; otherwise, it returns false.
`// Register as a primary bean with auto-generated name
boolean registered = registerBeanDefinition(registry, null, MyService.class, true);
// Register as a non-primary bean with explicit name
boolean registered = registerBeanDefinition(registry, "mySecondaryService", MyService.class, false);
if (registered) {
System.out.println("Bean registered successfully with primary flag.");
` else {
System.out.println("Bean was already registered.");
}
}
public static boolean registerBeanDefinition(BeanDefinitionRegistry registry, BeanDefinition beanDefinition)Registers a BeanDefinition into the given registry.
This method delegates to #registerBeanDefinition(BeanDefinitionRegistry, String, BeanDefinition)
with a null bean name, causing a unique bean name to be generated automatically based on the
provided bean definition. The registration process respects the default bean overriding settings of the registry.
If the bean is successfully registered, it returns true; otherwise, it returns false.
`BeanDefinition beanDefinition = genericBeanDefinition(MyService.class);
boolean registered = registerBeanDefinition(registry, beanDefinition);
if (registered) {
System.out.println("Bean registered successfully with auto-generated name.");
` else {
System.out.println("Bean was already registered.");
}
}
public static boolean registerBeanDefinition(BeanDefinitionRegistry registry, @Nullable String beanName,
BeanDefinition beanDefinition)Registers a BeanDefinition with an optional name into the given registry.
This method attempts to register the provided bean definition. If a bean name is provided,
it will be used; otherwise, a unique bean name will be generated automatically based on the
bean definition. The registration process respects the default bean overriding settings of the registry.
If the bean is successfully registered, it returns true; otherwise, it returns false.
`// Register with auto-generated name
BeanDefinition beanDefinition = genericBeanDefinition(MyService.class);
boolean registered = registerBeanDefinition(registry, null, beanDefinition);
// Register with explicit name
BeanDefinition anotherDefinition = genericBeanDefinition(AnotherService.class);
boolean registered = registerBeanDefinition(registry, "anotherService", anotherDefinition);
if (registered) {
System.out.println("Bean registered successfully.");
` else {
System.out.println("Bean was already registered.");
}
}
public static boolean registerBeanDefinition(BeanDefinitionRegistry registry, @Nullable String beanName,
BeanDefinition beanDefinition, boolean allowBeanDefinitionOverriding)Registers a BeanDefinition with an optional name and overriding control into the given registry.
This method attempts to register the provided bean definition. If a bean name is provided,
it will be used; otherwise, a unique bean name will be generated automatically based on the
bean definition. The registration process respects the allowBeanDefinitionOverriding flag.
If overriding is not allowed and a bean with the same name already exists, the registration is skipped,
and a warning is logged. If the bean is successfully registered, it returns true; otherwise,
it returns false.
`// Register with auto-generated name, allowing overriding
BeanDefinition beanDefinition = genericBeanDefinition(MyService.class);
boolean registered = registerBeanDefinition(registry, null, beanDefinition, true);
// Register with explicit name, disallowing overriding
BeanDefinition anotherDefinition = genericBeanDefinition(AnotherService.class);
boolean registered = registerBeanDefinition(registry, "anotherService", anotherDefinition, false);
if (registered) {
System.out.println("Bean registered successfully.");
` else {
System.out.println("Bean was not registered (already exists or error).");
}
}
public static void registerSingleton(SingletonBeanRegistry registry, String beanName, Object bean)Registers a singleton bean with the specified name into the given registry.
This method registers the provided bean as a singleton instance under the specified bean name. If the registration is successful and logging at INFO level is enabled, an informational log message is generated.
`MyService myService = new MyServiceImpl(); registerSingleton(registry, "myService", myService); `
public static boolean hasAlias(AliasRegistry registry, String beanName, String alias)Checks whether the specified alias is associated with the given bean name in the registry.
This method verifies if both the bean name and alias are non-empty, and then checks if the alias exists in the list of aliases for the specified bean name.
`boolean hasAlias = BeanRegistrar.hasAlias(registry, "myBean", "myAlias");
if (hasAlias) {
logger.info("Alias 'myAlias' is registered for bean 'myBean'.");
` else {
logger.info("Alias 'myAlias' is not registered for bean 'myBean'.");
}
}
public static void registerFactoryBean(BeanDefinitionRegistry registry, String beanName, Object bean)Registers beans into the Spring BeanFactory by loading their implementation class names from
the classpath resource file META-INF/spring.factories. This method delegates to
#registerSpringFactoriesBeans(BeanDefinitionRegistry, Class...) after converting the
BeanFactory to a BeanDefinitionRegistry.
If the provided BeanFactory does not implement BeanDefinitionRegistry, this method
will attempt to unwrap it or return an empty map depending on the implementation of
io.microsphere.spring.beans.factory.BeanFactoryUtils#asBeanDefinitionRegistry(Object).
`// Assuming beanFactory is an instance of ConfigurableApplicationContext or similar
Map registeredBeans = registerSpringFactoriesBeans(beanFactory, MyFactory.class);
registeredBeans.forEach((clazz, name) -> {
System.out.println("Registered bean: " + name + " of type: " + clazz.getName());
`);
}
public static void registerFactoryBean(BeanDefinitionRegistry registry, String beanName, Object bean, boolean primary)Registers a org.springframework.beans.factory.FactoryBean with the specified name into the given registry.
This method creates a bean definition for a DelegatingFactoryBean, which wraps the provided bean instance,
and registers it under the specified bean name. The registered bean will act as a FactoryBean that delegates to
the supplied object.
`MyService myService = new MyServiceImpl(); registerFactoryBean(registry, "myServiceFactory", myService, false); `
If you want this FactoryBean to be marked as the primary bean in case of multiple candidates:
`registerFactoryBean(registry, "myPrimaryServiceFactory", myService, true); `
public static void registerBean(BeanDefinitionRegistry registry, String beanName, Object bean)Registers a bean instance with the specified name into the given registry.
This method delegates to #registerBean(BeanDefinitionRegistry, String, Object, boolean)
with the default value of false for the primary flag. If the bean is successfully registered,
it will be treated as a regular bean unless further configuration is needed.
`MyService myService = new MyServiceImpl(); BeanRegistrar.registerBean(registry, "myService", myService); `
public static void registerBean(BeanDefinitionRegistry registry, String beanName, Object bean, boolean primary)Registers a bean instance with the specified name and primary status into the given registry.
This method attempts to register the provided bean instance directly using an AbstractBeanDefinition
with an instance supplier. If setting the instance supplier fails (e.g., due to configuration constraints),
it falls back to registering the bean via a DelegatingFactoryBean.
`MyService myService = new MyServiceImpl(); BeanRegistrar.registerBean(registry, "myService", myService, false); `
To mark the bean as primary:
`BeanRegistrar.registerBean(registry, "myPrimaryService", myService, true); `
If direct instance registration is not possible (e.g., due to proxying or complex lifecycle), the bean will be registered via a FactoryBean:
`AnotherBean anotherBean = new AnotherBean(); BeanRegistrar.registerBean(registry, "anotherBean", anotherBean, false); `
public static boolean registerInfrastructureBean(BeanFactory beanFactory, Class<?> beanType)Registers generic bean definitions for the specified collection of bean classes into the given registry.
This method iterates through the provided collection of bean classes, creates a generic bean definition for each, generates a unique bean name, and attempts to register it in the registry. It returns an unmodifiable map containing the successfully registered bean classes mapped to their assigned bean names.
`List> beanClasses = Arrays.asList(MyService.class, MyRepository.class);
Map, String> registeredBeans = registerGenericBeans(registry, beanClasses);
registeredBeans.forEach((clazz, name) -> {
logger.info("Registered bean: {` of type: {}", name, clazz.getName());
});
}
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