Skip to content

io microsphere spring context annotation ConfigurationPropertyOverrideAnnotationAttributesStrategy

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

ConfigurationPropertyOverrideAnnotationAttributesStrategy

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/ConfigurationPropertyOverrideAnnotationAttributesStrategy.java

Overview

A strategy implementation of OverrideAnnotationAttributesStrategy that overrides annotation attributes with values from Spring Environment configuration properties.

This strategy allows externalizing annotation attribute values into configuration properties (e.g., in application.properties or application.yml). It maps annotation attributes to property keys using a configurable prefix.

Property Mapping Logic

- **Prefix Determination:** The property prefix is determined by:
    
        - Checking for a custom prefix defined by the property key:
            `microsphere.spring.prefix.` (e.g., `microsphere.spring.prefix.org.springframework.context.annotation.PropertySource`)
        
        - Falling back to the default prefix:
            `microsphere.spring.@.` (e.g., `microsphere.spring.@PropertySource.`)
        
    

- **Attribute Mapping:** Each annotation attribute name is appended to the resolved prefix to form the full property key.
    For example, for an attribute `value`, the property key would be `<prefix>value`.

- **Type Conversion:** If a configuration property exists for an attribute, it is converted to the attribute's type
    using the Spring `org.springframework.core.convert.ConversionService`. If conversion fails or the property is missing,
    the original annotation attribute value is retained.

Example Usage

1. Default Prefix Behavior

Given an annotation @PropertySource(name = "default", ignoreResourceNotFound = false):

`// config.properties
microsphere.spring.@PropertySource.name=my-custom-source
microsphere.spring.@PropertySource.ignoreResourceNotFound=true

// Resulting AnnotationAttributes:
// name = "my-custom-source"
// ignoreResourceNotFound = true
`

2. Custom Prefix Behavior

You can define a custom prefix for a specific annotation type:

`// config.properties
# Define a custom prefix for PropertySource annotation
microsphere.spring.prefix.org.springframework.context.annotation.PropertySource=app.prop.source.

# Use the custom prefix to override attributes
app.prop.source.name=overridden-name
app.prop.source.ignoreResourceNotFound=true

// Resulting AnnotationAttributes for @PropertySource:
// name = "overridden-name"
// ignoreResourceNotFound = true
`

Declaration

public class ConfigurationPropertyOverrideAnnotationAttributesStrategy implements OverrideAnnotationAttributesStrategy,

Author: Mercy

Version Information

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

// config.properties
microsphere.spring.@PropertySource.name=my-custom-source
microsphere.spring.@PropertySource.ignoreResourceNotFound=true

// Resulting AnnotationAttributes:
// name = "my-custom-source"
// ignoreResourceNotFound = true

Example 2

// config.properties
# Define a custom prefix for PropertySource annotation
microsphere.spring.prefix.org.springframework.context.annotation.PropertySource=app.prop.source.

# Use the custom prefix to override attributes
app.prop.source.name=overridden-name
app.prop.source.ignoreResourceNotFound=true

// Resulting AnnotationAttributes for @PropertySource:
// name = "overridden-name"
// ignoreResourceNotFound = true

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.ConfigurationPropertyOverrideAnnotationAttributesStrategy;

API Reference

Public Methods

Method Description
override Overrides the original annotation attributes with values from configuration properties.
getPrefixPropertyName Gets the configuration properties associated with the specified annotation type.
getDefaultPropertyNamePrefix Gets the default property name prefix for the given annotation type.
setEnvironment

Method Details

override

public AnnotationAttributes override(AnnotationAttributes originalAttributes, Class<? extends Annotation> annotationType,
                                         AnnotationMetadata annotationMetadata)

Overrides the original annotation attributes with values from configuration properties.

This method attempts to find configuration properties that match the annotation's attributes. If a matching property is found and can be converted to the attribute's type, it replaces the original value. Otherwise, the original value is retained.

getPrefixPropertyName

public static String getPrefixPropertyName(Class<? extends Annotation> annotationType)

Gets the configuration properties associated with the specified annotation type.

This method retrieves sub-properties from the environment using the property name prefix derived from the given annotation type. These properties are intended to override the default attributes of the annotation.

Example Usage

`// Assume the environment contains the following properties:
// microsphere.spring.prefix.PropertySource.name = "myProperties"
// microsphere.spring.prefix.PropertySource.ignoreResourceNotFound = true

Map properties = getConfigurationProperties(PropertySource.class);
// properties will contain:
// { "name": "myProperties", "ignoreResourceNotFound": true `
}

getDefaultPropertyNamePrefix

public static String getDefaultPropertyNamePrefix(Class<? extends Annotation> annotationType)

Gets the default property name prefix for the given annotation type.

The default prefix is constructed by concatenating the io.microsphere.spring.constants.PropertyConstants#MICROSPHERE_SPRING_PROPERTY_NAME_PREFIX with the simple name of the annotation class and a dot character.

Example Usage

`String defaultPropertyNamePrefix = getDefaultPropertyNamePrefix(PropertySource.class);
 // defaultPropertyNamePrefix == "microsphere.spring.@PropertySource."
`

See Also

  • OverrideAnnotationAttributesStrategy
  • org.springframework.core.env.Environment

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