Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
2 of 3 tasks
spring-projects-issues opened this issue Mar 4, 2014 · 1 comment
Closed
2 of 3 tasks
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Mar 4, 2014

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:

@spring-projects-issues
Copy link
Collaborator Author

Sam Brannen commented

Implemented as described in the comments for GitHub commit ca66e07:

Support annotation attribute aliases and overrides via @AliasFor

This commit introduces first-class support for aliases for annotation attributes. Specifically, this commit introduces a new @AliasFor annotation that can be used to declare a pair of aliased attributes within a single annotation or an alias from an attribute in a custom composed annotation to an attribute in a meta-annotation.

To support @AliasFor within annotation instances, AnnotationUtils has been overhauled to "synthesize" any annotations returned by "get" and "find" searches. A SynthesizedAnnotation is an annotation that is wrapped in a JDK dynamic proxy which provides run-time support for @AliasFor semantics. SynthesizedAnnotationInvocationHandler is the actual handler behind the proxy.

In addition, the contract for @AliasFor is fully validated, and an AnnotationConfigurationException is thrown in case invalid configuration is detected.

For example, @ContextConfiguration from the spring-test module is now declared as follows:

public @interface ContextConfiguration {

    @AliasFor(attribute = "locations")
    String[] value() default {};

    @AliasFor(attribute = "value")
    String[] locations() default {};

    // ...
}

The following annotations and their related support classes have been
modified to use @AliasFor.

  • @ManagedResource
  • @ContextConfiguration
  • @ActiveProfiles
  • @TestExecutionListeners
  • @TestPropertySource
  • @Sql
  • @ControllerAdvice
  • @RequestMapping

Similarly, support for AnnotationAttributes has been reworked to support @AliasFor as well. This allows for fine-grained control over exactly which attributes are overridden within an annotation hierarchy. In fact, it is now possible to declare an alias for the value attribute of a meta-annotation.

For example, given the revised declaration of @ContextConfiguration above, one can now develop a composed annotation with a custom attribute override as follows.

@ContextConfiguration
public @interface MyTestConfig {

    @AliasFor(annotation = ContextConfiguration.class, attribute = "locations")
    String[] xmlFiles();

    // ...
}

Consequently, the following are functionally equivalent.

  • @MyTestConfig(xmlFiles = "test.xml")
  • @ContextConfiguration("test.xml")
  • @ContextConfiguration(locations = "test.xml")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants