Skip to content

io microsphere spring config CollectingConfigurationPropertyListener

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

CollectingConfigurationPropertyListener

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

Source: microsphere-spring-context/src/main/java/io/microsphere/spring/config/CollectingConfigurationPropertyListener.java

Overview

A listener implementation that collects and manages configuration properties during property resolution and autowiring processes.

CollectingConfigurationPropertyListener integrates with Spring's property resolution and autowire candidate resolving mechanisms to:

- Capture configuration properties as they are resolved by a `ConfigurablePropertyResolver`
- Track metadata such as the property name, target type, value, default value, and whether it's required
- Register itself as a Spring bean for integration into the application context lifecycle

Property Resolution Example

When a property is resolved via a ConfigurablePropertyResolver, this listener records its details:

{@code

### Declaration

```java
public class CollectingConfigurationPropertyListener implements PropertyResolverListener, AutowireCandidateResolvingListener,
```

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

```java
@Component
public class MyPropertyResolverListener implements PropertyResolverListener {
    @Override
    public void afterGetProperty(ConfigurablePropertyResolver resolver, String name, Class> targetType, Object value, Object defaultValue) {
        System.out.println("Captured property: " + name);
        System.out.println("Target type: " + targetType.getName());
        System.out.println("Value: " + value);
        System.out.println("Default Value: " + defaultValue);
    }
}
```

### Example 2

```java
@Component
public class MyAutowireCandidateResolvingListener implements AutowireCandidateResolvingListener {
    @Override
    public void suggestedValueResolved(DependencyDescriptor descriptor, Object suggestedValue) {
        System.out.println("Suggested value for dependency " + descriptor + ": " + suggestedValue);
    }

    @Override
    public void lazyProxyResolved(DependencyDescriptor descriptor, String beanName, Object proxy) {
        System.out.println("Lazy proxy created for " + descriptor + " in bean " + beanName);
    }
}
```

### Example 3

```java
BeanDefinitionRegistry registry = ...;
registerBean(registry, CollectingConfigurationPropertyListener.BEAN_NAME, new CollectingConfigurationPropertyListener());
```

### Method Examples

#### `afterGetProperty`

```java
// Triggered automatically when resolving a property:
  String value = environment.getProperty("test-name");
  // After resolution, the listener records the property details:
  ConfigurationProperty property = repository.get("test-name");
  assertEquals("test-value", property.getValue());
  assertEquals(String.class.getName(), property.getType());
```

#### `afterGetRequiredProperty`

```java
// Triggered automatically when resolving a required property:
  String value = environment.getRequiredProperty("test-name");
  // After resolution, the listener records the property as required:
  ConfigurationProperty property = repository.get("test-name");
  assertEquals("test-value", property.getValue());
  assertTrue(property.isRequired());
```

#### `afterSetPlaceholderPrefix`

```java
// Change the placeholder prefix on the environment:
  environment.setPlaceholderPrefix("#(");
  // The listener tracks the updated prefix:
  assertEquals("#(", listener.getPlaceholderPrefix());
```

#### `afterSetPlaceholderSuffix`

```java
// Change the placeholder suffix on the environment:
  environment.setPlaceholderSuffix(")");
  // The listener tracks the updated suffix:
  assertEquals(")", listener.getPlaceholderSuffix());
```

#### `setBeanFactory`

```java
// Typically invoked automatically by Spring during context initialization:
  CollectingConfigurationPropertyListener listener = new CollectingConfigurationPropertyListener();
  listener.setBeanFactory(beanFactory);
  // After setBeanFactory, the ConfigurationPropertyRepository is registered:
  assertTrue(beanFactory.containsBean(ConfigurationPropertyRepository.BEAN_NAME));
```

#### `getPlaceholderPrefix`

```java
CollectingConfigurationPropertyListener listener = ...; // obtained from Spring context
  String prefix = listener.getPlaceholderPrefix();
  assertEquals("${", prefix);
```

#### `getPlaceholderSuffix`

```java
CollectingConfigurationPropertyListener listener = ...; // obtained from Spring context
  String suffix = listener.getPlaceholderSuffix();
  assertEquals("}", suffix);
```

## 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.config.CollectingConfigurationPropertyListener;
```

## API Reference

### Public Methods

| Method | Description |
|--------|-------------|
| `afterGetProperty` | Invoked after a property is retrieved from the `ConfigurablePropertyResolver`. |
| `afterGetRequiredProperty` | Invoked after a required property is retrieved from the `ConfigurablePropertyResolver`. |
| `afterSetPlaceholderPrefix` | Invoked after the placeholder prefix is changed on the `ConfigurablePropertyResolver`. |
| `afterSetPlaceholderSuffix` | Invoked after the placeholder suffix is changed on the `ConfigurablePropertyResolver`. |
| `setBeanFactory` | Sets the `BeanFactory` and registers the `ConfigurationPropertyRepository` bean definition |
| `getPlaceholderPrefix` | Returns the current placeholder prefix used for property resolution. |
| `getPlaceholderSuffix` | Returns the current placeholder suffix used for property resolution. |

### Method Details

#### `afterGetProperty`

```java
public void afterGetProperty(ConfigurablePropertyResolver propertyResolver, String name, Class> targetType,
                                 Object value, Object defaultValue)
```

Invoked after a property is retrieved from the `ConfigurablePropertyResolver`.
Records the property's target type, resolved value, and default value in the
`ConfigurationPropertyRepository`.

### Example Usage
`// Triggered automatically when resolving a property:
  String value = environment.getProperty("test-name");
  // After resolution, the listener records the property details:
  ConfigurationProperty property = repository.get("test-name");
  assertEquals("test-value", property.getValue());
  assertEquals(String.class.getName(), property.getType());
`

afterGetRequiredProperty

public void afterGetRequiredProperty(ConfigurablePropertyResolver propertyResolver, String name, Class<?> targetType,
                                         Object value)

Invoked after a required property is retrieved from the ConfigurablePropertyResolver. Records the property's target type and resolved value, and marks it as required in the ConfigurationPropertyRepository.

Example Usage

`// Triggered automatically when resolving a required property:
  String value = environment.getRequiredProperty("test-name");
  // After resolution, the listener records the property as required:
  ConfigurationProperty property = repository.get("test-name");
  assertEquals("test-value", property.getValue());
  assertTrue(property.isRequired());
`

afterSetPlaceholderPrefix

public void afterSetPlaceholderPrefix(ConfigurablePropertyResolver propertyResolver, String prefix)

Invoked after the placeholder prefix is changed on the ConfigurablePropertyResolver. Updates the internally tracked placeholder prefix.

Example Usage

`// Change the placeholder prefix on the environment:
  environment.setPlaceholderPrefix("#(");
  // The listener tracks the updated prefix:
  assertEquals("#(", listener.getPlaceholderPrefix());
`

afterSetPlaceholderSuffix

public void afterSetPlaceholderSuffix(ConfigurablePropertyResolver propertyResolver, String suffix)

Invoked after the placeholder suffix is changed on the ConfigurablePropertyResolver. Updates the internally tracked placeholder suffix.

Example Usage

`// Change the placeholder suffix on the environment:
  environment.setPlaceholderSuffix(")");
  // The listener tracks the updated suffix:
  assertEquals(")", listener.getPlaceholderSuffix());
`

setBeanFactory

public void setBeanFactory(BeanFactory beanFactory)

Sets the BeanFactory and registers the ConfigurationPropertyRepository bean definition as well as this listener as a named bean in the BeanDefinitionRegistry.

Example Usage

`// Typically invoked automatically by Spring during context initialization:
  CollectingConfigurationPropertyListener listener = new CollectingConfigurationPropertyListener();
  listener.setBeanFactory(beanFactory);
  // After setBeanFactory, the ConfigurationPropertyRepository is registered:
  assertTrue(beanFactory.containsBean(ConfigurationPropertyRepository.BEAN_NAME));
`

getPlaceholderPrefix

public String getPlaceholderPrefix()

Returns the current placeholder prefix used for property resolution. Defaults to "${" unless changed via #afterSetPlaceholderPrefix(ConfigurablePropertyResolver, String).

Example Usage

`CollectingConfigurationPropertyListener listener = ...; // obtained from Spring context
  String prefix = listener.getPlaceholderPrefix();
  assertEquals("${", prefix);
`

getPlaceholderSuffix

public String getPlaceholderSuffix()

Returns the current placeholder suffix used for property resolution. Defaults to ""} unless changed via #afterSetPlaceholderSuffix(ConfigurablePropertyResolver, String).

Example Usage

`CollectingConfigurationPropertyListener listener = ...; // obtained from Spring context
  String suffix = listener.getPlaceholderSuffix();
  assertEquals("`", suffix);
}

See Also

  • PropertyResolverListener
  • AutowireCandidateResolvingListener
  • ConfigurationPropertyRepository
  • ConfigurationProperty

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