Skip to content

io microsphere spring beans factory support BeanRegistrar

github-actions[bot] edited this page May 26, 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.18-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, MyInfrastructureClass.class);
if (registered) {
    System.out.println("Infrastructure bean registered successfully.");
} else {
    System.out.println("Infrastructure bean was already registered.");
}

registerInfrastructureBean

boolean registered = registerInfrastructureBean(registry, "myBean", MyBeanClass.class);
if (registered) {
    System.out.println("Bean registered successfully.");
} else {
    System.out.println("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

boolean registered = registerBeanDefinition(registry, "myBean", MyBean.class);
if (registered) {
    logger.info("Bean registered successfully.");
} else {
    logger.warn("Bean was already registered.");
}

registerBeanDefinition

boolean registered = registerBeanDefinition(registry, "myService", MyService.class, "arg1", 123);
if (registered) {
    logger.info("Bean with custom constructor arguments registered successfully.");
} else {
    logger.warn("Bean was already registered.");
}

registerBeanDefinition

boolean registered = registerBeanDefinition(registry, "myService", MyService.class, BeanDefinition.ROLE_INFRASTRUCTURE);
if (registered) {
    logger.info("Bean with custom role registered successfully.");
} else {
    logger.warn("Bean was already registered.");
}

registerBeanDefinition

BeanDefinition beanDefinition = genericBeanDefinition(MyService.class);
String beanName = "myService";
boolean registered = registerBeanDefinition(registry, beanName, beanDefinition);
if (registered) {
    logger.info("Bean was successfully registered.");
} else {
    logger.warn("Bean was already registered and not overridden.");
}

registerBeanDefinition

BeanDefinition beanDefinition = genericBeanDefinition(MyService.class);
String beanName = "myService";
boolean allowBeanDefinitionOverriding = false;

boolean registered = registerBeanDefinition(registry, beanName, beanDefinition, allowBeanDefinitionOverriding);
if (registered) {
    logger.info("Bean was successfully registered.");
} else {
    logger.warn("Bean was already registered and not overridden.");
}

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

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 of the specified type into the given registry.
registerInfrastructureBean Registers an infrastructure bean with the specified name and type into the given registry.
registerBeanDefinition Register a BeanDefinition for the specified bean type.
registerBeanDefinition Registers a bean definition with the specified name and type into the given registry.
registerBeanDefinition Registers a bean definition with the specified name, type, and constructor arguments into the given registry.
registerBeanDefinition Register a bean definition with the specified name, type, and role.
registerBeanDefinition Register a BeanDefinition with name if absent.
registerBeanDefinition Registers a bean definition with the specified name 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.

Method Details

registerInfrastructureBean

public static boolean registerInfrastructureBean(BeanDefinitionRegistry registry, Class<?> beanType)

Registers an infrastructure bean of the specified type into the given registry.

This method generates a unique bean name based on the bean definition and registers it as an infrastructure bean. If the bean definition is successfully registered, it returns true; otherwise, it returns false.

Example Usage

`boolean registered = registerInfrastructureBean(registry, MyInfrastructureClass.class);
if (registered) {
    System.out.println("Infrastructure bean registered successfully.");
` else {
    System.out.println("Infrastructure bean was already registered.");
}
}

registerInfrastructureBean

public static boolean registerInfrastructureBean(BeanDefinitionRegistry registry, String beanName, Class<?> beanType)

Registers an infrastructure bean with the specified name and type into the given registry.

This method creates a generic bean definition for the provided bean type with the default role, and attempts to register it under the specified bean name. If the bean is successfully registered, it returns true; otherwise, it returns false.

Example Usage

`boolean registered = registerInfrastructureBean(registry, "myBean", MyBeanClass.class);
if (registered) {
    System.out.println("Bean registered successfully.");
` else {
    System.out.println("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, String beanName, Class<?> beanType)

Registers a bean definition with the specified name and type into the given registry.

This method creates a generic bean definition for the provided bean type and attempts to register it under the specified bean name. If the bean is successfully registered, it returns true; otherwise, it returns false.

Example Usage

`boolean registered = registerBeanDefinition(registry, "myBean", MyBean.class);
if (registered) {
    logger.info("Bean registered successfully.");
` else {
    logger.warn("Bean was already registered.");
}
}

registerBeanDefinition

public static boolean registerBeanDefinition(BeanDefinitionRegistry registry, String beanName, Class<?> beanType, Object... constructorArguments)

Registers a bean definition with the specified name, type, and constructor arguments into the given registry.

This method creates a generic bean definition for the provided bean type using the supplied constructor arguments, and attempts to register it under the specified bean name. If the bean is successfully registered, it returns true; otherwise, it returns false.

Example Usage

`boolean registered = registerBeanDefinition(registry, "myService", MyService.class, "arg1", 123);
if (registered) {
    logger.info("Bean with custom constructor arguments registered successfully.");
` else {
    logger.warn("Bean was already registered.");
}
}

registerBeanDefinition

public static boolean registerBeanDefinition(BeanDefinitionRegistry registry, String beanName, Class<?> beanType, int role)

Register a bean definition with the specified name, type, and role.

This method creates a generic bean definition for the provided bean type with the specified role, and attempts to register it under the specified bean name. If the bean is successfully registered, it returns true; otherwise, it returns false.

Example Usage

`boolean registered = registerBeanDefinition(registry, "myService", MyService.class, BeanDefinition.ROLE_INFRASTRUCTURE);
if (registered) {
    logger.info("Bean with custom role registered successfully.");
` else {
    logger.warn("Bean was already registered.");
}
}

registerBeanDefinition

public static final boolean registerBeanDefinition(BeanDefinitionRegistry registry, String beanName, BeanDefinition beanDefinition)

Register a BeanDefinition with name if absent.

This method attempts to register the given bean definition under the specified name only if there is no existing bean definition with the same name in the registry. It internally delegates to #registerBeanDefinition(BeanDefinitionRegistry, String, BeanDefinition, boolean) with allowBeanDefinitionOverriding set to false.

Example Usage

`BeanDefinition beanDefinition = genericBeanDefinition(MyService.class);
String beanName = "myService";
boolean registered = registerBeanDefinition(registry, beanName, beanDefinition);
if (registered) {
    logger.info("Bean was successfully registered.");
` else {
    logger.warn("Bean was already registered and not overridden.");
}
}

registerBeanDefinition

public static final boolean registerBeanDefinition(BeanDefinitionRegistry registry, String beanName, BeanDefinition beanDefinition, boolean allowBeanDefinitionOverriding)

Registers a bean definition with the specified name into the given registry.

This method attempts to register the provided bean definition under the specified bean name. If the bean definition is successfully registered, it returns true; otherwise, it returns false.

If overriding is not allowed and a bean definition with the same name already exists in the registry, the new definition will not be registered, a warning log message will be generated, and this method will return false.

If overriding is allowed and a bean definition with the same name already exists, the existing definition will be replaced with the new one.

Example Usage

`BeanDefinition beanDefinition = genericBeanDefinition(MyService.class);
String beanName = "myService";
boolean allowBeanDefinitionOverriding = false;

boolean registered = registerBeanDefinition(registry, beanName, beanDefinition, allowBeanDefinitionOverriding);
if (registered) {
    logger.info("Bean was successfully registered.");
` else {
    logger.warn("Bean was already registered and not overridden.");
}
}

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

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