Skip to content

io microsphere spring beans factory annotation AnnotatedInjectionBeanPostProcessor

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

AnnotatedInjectionBeanPostProcessor

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

Source: microsphere-spring-context/src/main/java/io/microsphere/spring/beans/factory/annotation/AnnotatedInjectionBeanPostProcessor.java

Overview

A BeanPostProcessor implementation that provides dependency injection support for custom annotation types.

This class supports the following features:

- **Custom Annotation Injection**: Allows dependency injection based on one or more custom annotations.
- **Annotation Processing Options**:
    
        - `#setClassValuesAsString(boolean)`: Whether to convert Class references to Strings for compatibility with `org.springframework.core.type.AnnotationMetadata`.
        - `#setNestedAnnotationsAsMap(boolean)`: Whether to convert nested annotations into `AnnotationAttributes` maps.
        - `#setIgnoreDefaultValue(boolean)`: Whether to ignore default values from annotations.
        - `#setTryMergedAnnotation(boolean)`: Whether to attempt resolving merged annotations.
    

- **Caching Mechanism**: Metadata such as injection points and constructor information is cached to improve performance.
- **Integration with Spring Container**: Implements various Spring extension interfaces like
    `MergedBeanDefinitionPostProcessor`, `BeanFactoryAware`, and `EnvironmentAware`.

Example Usage

Defining a Custom Injection Annotation

{@code

### Declaration

```java
public class AnnotatedInjectionBeanPostProcessor implements SmartInstantiationAwareBeanPostProcessor,
```

**Author:** Mercy

## Version Information

- **Introduced in:** `1.0.0`
- **Current Project Version:** `0.2.27-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

```java
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MyInject {}
```

### Example 2

```java
@Configuration
public class AppConfig {

    @Bean
    public AnnotatedInjectionBeanPostProcessor myInjectBeanPostProcessor() {
        return new AnnotatedInjectionBeanPostProcessor(MyInject.class);
    }
}
```

### Example 3

```java
public class MyService {
    @MyInject
    private Dependency dependency;
}
```

### Example 4

```java
@Bean
public AnnotatedInjectionBeanPostProcessor myInjectBeanPostProcessor() {
    AnnotatedInjectionBeanPostProcessor processor = new AnnotatedInjectionBeanPostProcessor(MyInject.class);
    processor.setClassValuesAsString(true);
    processor.setNestedAnnotationsAsMap(true);
    processor.setCacheSize(128);
    return processor;
}
```

### Method Examples

#### `determineCandidateConstructors`

```java
// Given a class with an annotated constructor:
  public class GenericChild extends GenericParent {
      @Referenced
      public GenericChild(@Referenced User user) {
          this.user = user;
      }
  }
  // The processor automatically determines the annotated constructor as a candidate
  Constructor>[] constructors = processor.determineCandidateConstructors(GenericChild.class, "genericChild");
```

#### `postProcessPropertyValues`

```java
// Typically invoked by the Spring container during bean creation:
  PropertyValues result = processor.postProcessPropertyValues(pvs, pds, myBean, "myBean");
```

#### `postProcessProperties`

```java
// Automatically called by Spring during bean population phase:
  AnnotatedInjectionBeanPostProcessor processor = new MyAnnotationBeanPostProcessor();
  PropertyValues result = processor.postProcessProperties(pvs, myServiceBean, "myService");
```

## Usage

### Maven Dependency

Add the following dependency to your `pom.xml`:

```xml

    io.github.microsphere-projects
    microsphere-spring-context
    ${microsphere-spring.version}

```

> **Tip:** Use the BOM (`microsphere-spring-dependencies`) for consistent version management. See the [Getting Started](https://github.com/microsphere-projects/microsphere-spring#getting-started) guide.

### Import

```java
import io.microsphere.spring.beans.factory.annotation.AnnotatedInjectionBeanPostProcessor;
```

## API Reference

### Public Methods

| Method | Description |
|--------|-------------|
| `determineCandidateConstructors` | The property name of metadata cache size : "microsphere.spring.injection.metadata.cache.size" |
| `postProcessPropertyValues` | Post-processes property values before they are applied to the given bean, delegating |
| `postProcessProperties` | Post-processes property values by finding and injecting dependencies annotated with |
| `setOrder` | Finds `InjectedElement` Metadata from annotated fields |
| `setClassValuesAsString` |  |
| `setNestedAnnotationsAsMap` |  |
| `setIgnoreDefaultValue` |  |
| `setTryMergedAnnotation` |  |
| `setCacheSize` | Set the size of cache |
| `afterPropertiesSet` |  |
| `destroy` |  |
| `getOrder` |  |
| `getEnvironment` |  |
| `getClassLoader` |  |
| `getBeanFactory` |  |
| `getAttributes` |  |
| `isRequired` |  |
| `getInjectionPoint` |  |
| `resolveShortcut` |  |

### Method Details

#### `determineCandidateConstructors`

```java
public final Constructor>[] determineCandidateConstructors(Class> beanClass, String beanName)
```

The property name of metadata cache size : "microsphere.spring.injection.metadata.cache.size"
/
public static final String CACHE_SIZE_PROPERTY_NAME = MICROSPHERE_SPRING_PROPERTY_NAME_PREFIX + "injection.metadata.cache.size";

/**
The default cache size of metadata cache : "32"
/
public static final String DEFAULT_CACHE_SIZE_PROPERTY_VALUE = "32";

/**
The property name of metadata cache expire time : "microsphere.spring.injection.metadata.cache.expire.time"
/
public static final int DEFAULT_CACHE_SIZE = parseInt(DEFAULT_CACHE_SIZE_PROPERTY_VALUE);

#### `postProcessPropertyValues`

```java
public final PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)
```

Post-processes property values before they are applied to the given bean, delegating
to `#postProcessProperties(PropertyValues, Object, String)`.

### Example Usage
`// Typically invoked by the Spring container during bean creation:
  PropertyValues result = processor.postProcessPropertyValues(pvs, pds, myBean, "myBean");
`

postProcessProperties

public final PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)

Post-processes property values by finding and injecting dependencies annotated with the configured annotation types into the given bean.

Example Usage

`// Automatically called by Spring during bean population phase:
  AnnotatedInjectionBeanPostProcessor processor = new MyAnnotationBeanPostProcessor();
  PropertyValues result = processor.postProcessProperties(pvs, myServiceBean, "myService");
`

setOrder

public final void setOrder(int order)

Finds InjectedElement Metadata from annotated fields

See Also

  • AutowiredAnnotationBeanPostProcessor

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