Skip to content

Introduce support for explicit annotation attribute overrides [SPR-11513] #16138

Closed
@spring-projects-issues

Description

@spring-projects-issues

Sam Brannen opened SPR-11513 and commented

Status Quo

The Spring Framework provides support for meta-annotation attribute overrides in custom composed annotations. However, as of Spring 4.0, such overrides are only supported for attributes with names other than value, and the overriding attribute must have the exact same name and type as the attribute it overrides.

If multiple meta-annotations are used to compose the custom annotation, and if these meta-annotations declare attributes of the same name and type (e.g., String name() default "";), then there is a conflict. In such scenarios:

  1. It is impossible to know which attribute is being overridden.
    • The attribute in the composed annotation effectively overrides all attributes of the same name and type within the annotation hierarchy.
  2. It is impossible to specify different overriding values for attributes with the same name.

Furthermore, it is currently impossible to override a value attribute from a meta-annotation.


Deliverables

  1. Introduce a new annotation that indicates the name of the attribute being overridden as well as the annotation type.
  2. Revise the search algorithms in AnnotationUtils and AnnotatedElementUtils so that they honor this new annotation.

Proposals for Annotation Name

  • @OverridesMeta
  • @MetaAnnotationAttribute
  • @AnnotationAttributeMapping

Example: Overriding Multiple value Attributes

The following example demonstrates how to:

  1. Override a value attribute
  2. Override attributes from multiple meta-annotations with the same attribute name (in this case value)
@Async
@Transactional
public @interface AsyncTransactional {

    /**
     * Qualifier for the name of the TaskExecutor.
     */
    @OverridesMeta(annotation = Async.class, attribute = "value") 
    String taskExecutor();

    /**
     * Qualifier for the name of the PlatformTransactionManager.
     */
    @OverridesMeta(annotation = Transactional.class, attribute = "value") 
    String transactionManager();

}
@AsyncTransactional(taskExecutor = "userTaskExecutor", transactionManager = "txMgr")
@Service
public class UserService {
    // ...
}

Example: Overriding Attribute With Custom Name

@Scope("request")
@Component
public @interface RequestScopedWebComponent {

    /**
     * Proxy mode.
     */
    @OverridesMeta(annotation = Scope.class, attribute = "proxyMode") 
    ScopedProxyMode mode() default ScopedProxyMode.TARGET_CLASS;

}
@RequestScopedWebComponent(mode = ScopedProxyMode.INTERFACES)
public class ProductionWebComponent implements WebComponent {
    // ...
}

Affects: 4.0 GA

Issue Links:

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions