Skip to content

io microsphere spring config context annotation PropertySourceExtension

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

PropertySourceExtension

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

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

Overview

Extension meta-annotation for Spring's PropertySource @PropertySource to overcome its limitations:

- The `PropertySource @PropertySource` annotation can't auto-refresh the `PropertySources property sources`
- The `PropertySource @PropertySource` annotation can't control the order of `org.springframework.core.env.PropertySource`
- The `PropertySource @PropertySource` annotation can't be `Inherited inherited`
- The `PropertySource#value PropertySource#value()` attribute does not support the `Resource resource` location wildcards
- The `PropertySource#encoding() PropertySource#encoding()` attribute does not specify the default encoding for the `Resource resource`

Features:

- Supports auto-refreshing property sources when configurations change
- Allows specifying the order of property sources using:
    
        - `#first()` - Place this property source at the top
        - `#before()` - Place before a specific property source
        - `#after()` - Place after a specific property source
    

- Supports inheritance via the `Inherited @Inherited` annotation
- Resource location wildcards are supported in the `#value()` attribute
- Provides control over resource loading behavior with:
    
        - `#ignoreResourceNotFound()`
        - `#encoding()`
        - `#resourceComparator()`
    

- Customizable property source creation via the `#factory()` attribute

Example Usage

Basic Usage

{@code
// Define a custom annotation using PropertySourceExtension

### Declaration

```java
public @interface PropertySourceExtension
```

**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
// Define a custom annotation using PropertySourceExtension
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@PropertySourceExtension(
    value = "classpath:/my-config/*.properties",
    first = true,
    autoRefreshed = true,
    encoding = "UTF-8",
    ignoreResourceNotFound = true,
    resourceComparator = MyCustomResourceComparator.class
)
public @interface MyCustomPropertySource {
}

// Use the custom annotation on a configuration class
@MyCustomPropertySource
@Configuration
public class MyConfig {
}
```

### Example 2

```java
@Target(TYPE)
@Retention(RUNTIME)
@Inherited
@Documented
@PropertySourceExtension
@Repeatable(ResourcePropertySources.class)
@Import(ResourcePropertySourceLoader.class)
public @interface ResourcePropertySource {
    @AliasFor(annotation = PropertySourceExtension.class)
    String name() default "";

    @AliasFor(annotation = PropertySourceExtension.class)
    boolean autoRefreshed() default false;

    @AliasFor(annotation = PropertySourceExtension.class)
    boolean first() default false;

    @AliasFor(annotation = PropertySourceExtension.class)
    String before() default "";

    @AliasFor(annotation = PropertySourceExtension.class)
    String after() default "";

    @AliasFor(annotation = PropertySourceExtension.class)
    String[] value() default {};

    @AliasFor(annotation = PropertySourceExtension.class)
    Class extends Comparator> resourceComparator() default DefaultResourceComparator.class;

    @AliasFor(annotation = PropertySourceExtension.class)
    boolean ignoreResourceNotFound() default false;

    @AliasFor(annotation = PropertySourceExtension.class)
    String encoding() default "${file.encoding:UTF-8}";

    @AliasFor(annotation = PropertySourceExtension.class)
    Class extends PropertySourceFactory> factory() default DefaultPropertySourceFactory.class;
}
```

## 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.context.annotation.PropertySourceExtension;
```

## See Also

- `PropertySource`
- `ResourcePropertySource`
- `org.springframework.core.env.PropertySource`
- `PropertySourceExtensionLoader`

---

*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