Skip to content

io microsphere spring context annotation BeanCapableImportCandidate

github-actions[bot] edited this page Jun 6, 2026 · 28 revisions

BeanCapableImportCandidate

Type: Class | Module: microsphere-spring-context | Package: io.microsphere.spring.context.annotation | Since: 1.0.0

Source: microsphere-spring-context/src/main/java/io/microsphere/spring/context/annotation/BeanCapableImportCandidate.java

Overview

The Import @Import candidate is an instance of org.springframework.context.annotation.ImportSelector or ImportBeanDefinitionRegistrar and not a regular Spring bean, which only invokes BeanClassLoaderAware, BeanFactoryAware, EnvironmentAware, and ResourceLoaderAware contracts in order if they are implemented, thus it will not be AbstractAutowireCapableBeanFactory#populateBean(String, RootBeanDefinition, BeanWrapper) populated and AutowireCapableBeanFactory#initializeBean(Object, String) initialized .

The current abstract implementation is a template class supports the Spring bean lifecycles : AbstractAutowireCapableBeanFactory#populateBean(String, RootBeanDefinition, BeanWrapper) population, AutowireCapableBeanFactory#initializeBean(Object, String) initialization, and ConfigurableBeanFactory#destroyBean(String, Object) destroy, the sub-class must implement the interface ImportSelector or ImportBeanDefinitionRegistrar, and can't override those methods:

- `#setBeanClassLoader(ClassLoader)`
- `#setBeanFactory(BeanFactory)`
- `#setEnvironment(Environment)`
- `#setResourceLoader(ResourceLoader)`
- `#setApplicationContext(ApplicationContext)`

Example Usage

`public class MyImportRegistrar extends BeanCapableImportCandidate implements ImportBeanDefinitionRegistrar {

    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        logger.info("Registering beans from custom registrar");
        registry.registerBeanDefinition("myBean", new RootBeanDefinition(MyBean.class));
    `
}
}

Declaration

public abstract class BeanCapableImportCandidate implements BeanClassLoaderAware, BeanFactoryAware, EnvironmentAware,

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.2.23-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

public class MyImportRegistrar extends BeanCapableImportCandidate implements ImportBeanDefinitionRegistrar {

    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        logger.info("Registering beans from custom registrar");
        registry.registerBeanDefinition("myBean", new RootBeanDefinition(MyBean.class));
    }
}

Method Examples

setBeanClassLoader

// Automatically invoked by the Spring container during @Import processing.
  // For example, when using:
  @Import(MyImportSelector.class)
  public class AppConfig { }

setBeanFactory

// Automatically invoked by the Spring container during @Import processing.
  // After this call, getBeanFactory() returns the ConfigurableListableBeanFactory.
  @Import(MyImportBeanDefinitionRegistrar.class)
  public class AppConfig { }

setEnvironment

// Automatically invoked by the Spring container during @Import processing.
  // After this call, getEnvironment() returns the ConfigurableEnvironment.
  @Import(MyImportSelector.class)
  public class AppConfig { }

setResourceLoader

// Automatically invoked by the Spring container during @Import processing.
  // After this call, getResourceLoader() returns the ResourceLoader and the
  // bean is fully initialized with Spring bean lifecycle support.
  @Import(MyImportBeanDefinitionRegistrar.class)
  public class AppConfig { }

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.context.annotation.BeanCapableImportCandidate;

API Reference

Public Methods

Method Description
setBeanClassLoader Indicates that no class to import
setBeanFactory Sets the BeanFactory that created this bean, stored as a
setEnvironment Sets the Environment in which this bean operates, stored as a
setResourceLoader Sets the ResourceLoader for this bean. This is the last callback in the
setApplicationContext
getClassLoader Get the ClassLoader instance
getBeanFactory The ConfigurableListableBeanFactory instance
getApplicationContext The ConfigurableApplicationContext instance
getEnvironment The ConfigurableEnvironment instance
getResourceLoader The ResourceLoader instance

Method Details

setBeanClassLoader

public final void setBeanClassLoader(ClassLoader classLoader)

Indicates that no class to import / public static final String[] NO_CLASS_TO_IMPORT = EMPTY_STRING_ARRAY;

protected final Logger logger = getLogger(this.getClass());

protected ClassLoader classLoader;

protected ConfigurableListableBeanFactory beanFactory;

protected ConfigurableApplicationContext applicationContext;

protected ConfigurableEnvironment environment;

protected ResourceLoader resourceLoader;

/** Sets the ClassLoader used by the bean. This method also asserts that the subclass properly implements ImportSelector or ImportBeanDefinitionRegistrar.

Example Usage

{@code
  // Automatically invoked by the Spring container during @Import processing.
  // For example, when using:

#### `setBeanFactory`

```java
public final void setBeanFactory(BeanFactory beanFactory)
```

Sets the `BeanFactory` that created this bean, stored as a
`ConfigurableListableBeanFactory`.

### Example Usage
{@code
  // Automatically invoked by the Spring container during @Import processing.
  // After this call, getBeanFactory() returns the ConfigurableListableBeanFactory.

#### `setEnvironment`

```java
public final void setEnvironment(Environment environment)
```

Sets the `Environment` in which this bean operates, stored as a
`ConfigurableEnvironment`.

### Example Usage
{@code
  // Automatically invoked by the Spring container during @Import processing.
  // After this call, getEnvironment() returns the ConfigurableEnvironment.

#### `setResourceLoader`

```java
public final void setResourceLoader(ResourceLoader resourceLoader)
```

Sets the `ResourceLoader` for this bean. This is the last callback in the
sequence, and triggers the self-initialization of this bean as a Spring-managed bean
to support full bean lifecycle (population, initialization, and destruction).

### Example Usage
{@code
  // Automatically invoked by the Spring container during @Import processing.
  // After this call, getResourceLoader() returns the ResourceLoader and the
  // bean is fully initialized with Spring bean lifecycle support.

#### `getBeanFactory`

```java
public final ConfigurableListableBeanFactory getBeanFactory()
```

The `ConfigurableListableBeanFactory` instance

#### `getApplicationContext`

```java
public final ConfigurableApplicationContext getApplicationContext()
```

The `ConfigurableApplicationContext` instance

## See Also

- `AbstractAutowireCapableBeanFactory#populateBean(String, RootBeanDefinition, BeanWrapper)`
- `AutowireCapableBeanFactory#initializeBean(Object, String)`
- `ConfigurableBeanFactory#destroyBean(String, Object)`
- `org.springframework.context.support.ApplicationContextAwareProcessor`

---

*This documentation was auto-generated from the source code of [microsphere-spring](https://github.com/microsphere-projects/microsphere-spring).*

Home

spring-context

spring-guice

spring-jdbc

spring-test

spring-web

spring-webflux

spring-webmvc

Clone this wiki locally