Skip to content

io microsphere spring beans factory support BeanRegistrar

github-actions[bot] edited this page Jul 11, 2026 · 29 revisions

BeanRegistrar

Type: Class | Module: microsphere-spring-context | Package: io.microsphere.spring.beans.factory.support | Since: 1.0.0

Source: microsphere-spring-context/src/main/java/io/microsphere/spring/beans/factory/support/BeanRegistrar.java

Overview

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.

Key Features

- 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.

Example Usage

Registering an Infrastructure Bean

`boolean registered = BeanRegistrar.registerInfrastructureBean(registry, MyService.class);
if (registered) {
    logger.info("Infrastructure bean registered successfully.");
` else {
    logger.warn("Infrastructure bean was already registered.");
}
}

Registering a Bean with Explicit Name

`boolean registered = BeanRegistrar.registerBeanDefinition(registry, "customName", MyService.class);
if (registered) {
    logger.info("Bean registered under name 'customName'.");
`
}

Registering a Singleton Bean

`MySingleton mySingleton = new MySingleton();
BeanRegistrar.registerSingleton(registry, "mySingleton", mySingleton);
`

Registering Beans from Spring Factories

`int count = BeanRegistrar.registerSpringFactoriesBeans(registry, MyFactory.class);
logger.info("{` beans registered from spring.factories.", count);
}

Declaration

public abstract class BeanRegistrar

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.2.36-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

Example 1

boolean registered = BeanRegistrar.registerInfrastructureBean(registry, MyService.class);
if (registered) {
    logger.info("Infrastructure bean registered successfully.");
} else {
    logger.warn("Infrastructure bean was already registered.");
}

Example 2

boolean registered = BeanRegistrar.registerBeanDefinition(registry, "customName", MyService.class);
if (registered) {
    logger.info("Bean registered under name 'customName'.");
}

Example 3

MySingleton mySingleton = new MySingleton();
BeanRegistrar.registerSingleton(registry, "mySingleton", mySingleton);

Example 4

int count = BeanRegistrar.registerSpringFactoriesBeans(registry, MyFactory.class);
logger.info("{} beans registered from spring.factories.", count);

Method Examples

registerInfrastructureBean

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.");
}

registerInfrastructureBean

// 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.");
}

registerBeanDefinition

boolean registered = registerBeanDefinition(registry, MyBean.class);
if (registered) {
    System.out.println("Bean registered successfully.");
} else {
    System.out.println("Bean was already registered.");
}

registerBeanDefinition

// 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.");
}

registerBeanDefinition

// 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.");
}

registerBeanDefinition

// 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.");
}

registerBeanDefinition

// 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.");
}

registerBeanDefinition

// 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.");
}

registerBeanDefinition

// 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.");
}

registerBeanDefinition

// 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.");
}

registerBeanDefinition

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.");
}

registerBeanDefinition

// 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.");
}

registerBeanDefinition

// 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).");
}

registerSingleton

MyService myService = new MyServiceImpl();
registerSingleton(registry, "myService", myService);

hasAlias

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'.");
}

registerFactoryBean

// 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);

registerFactoryBean

MyService myService = new MyServiceImpl();
registerFactoryBean(registry, "myServiceFactory", myService, false);
registerFactoryBean(registry, "myPrimaryServiceFactory", myService, true);

registerBean

MyService myService = new MyServiceImpl();
BeanRegistrar.registerBean(registry, "myService", myService);

registerBean

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);

registerInfrastructureBean

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);
}

Usage

Maven Dependency

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

import io.microsphere.spring.beans.factory.support.BeanRegistrar;

API Reference

Public Methods

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.

Method Details

registerInfrastructureBean

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.

Example Usage

`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.");
}
}

registerInfrastructureBean

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.

Example Usage

`// 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.");
}
}

registerBeanDefinition

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.

Example Usage

`boolean registered = registerBeanDefinition(registry, MyBean.class);
if (registered) {
    System.out.println("Bean registered successfully.");
` else {
    System.out.println("Bean was already registered.");
}
}

registerBeanDefinition

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.

Example Usage

`// 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.");
}
}

registerBeanDefinition

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.

Example Usage

`// 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.");
}
}

registerBeanDefinition

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.

Example Usage

`// 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.");
}
}

registerBeanDefinition

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.

Example Usage

`// 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.");
}
}

registerBeanDefinition

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.

Example Usage

`// 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.");
}
}

registerBeanDefinition

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.

Example Usage

`// 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.");
}
}

registerBeanDefinition

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.

Example Usage

`// 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.");
}
}

registerBeanDefinition

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.

Example Usage

`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.");
}
}

registerBeanDefinition

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.

Example Usage

`// 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.");
}
}

registerBeanDefinition

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.

Example Usage

`// 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).");
}
}

registerSingleton

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.

Example Usage

`MyService myService = new MyServiceImpl();
registerSingleton(registry, "myService", myService);
`

hasAlias

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.

Example Usage

`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'.");
}
}

registerFactoryBean

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).

Example Usage

`// 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());
`);
}

registerFactoryBean

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.

Example Usage

`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);
`

registerBean

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.

Example Usage

`MyService myService = new MyServiceImpl();
BeanRegistrar.registerBean(registry, "myService", myService);
`

registerBean

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.

Example Usage

`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);
`

registerInfrastructureBean

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.

Example Usage

`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.

Home

spring-context

spring-guice

spring-jdbc

spring-test

spring-web

spring-webflux

spring-webmvc

Clone this wiki locally