Skip to content

Provide a mechanism for composed annotations to signal that they want to override attributes [SPR-13448] #18028

Closed

Description

@spring-projects-issues

Dave Syer opened SPR-13448 and commented

AnnotatedElementUtils is now available for processing merged (overridden) attributes from meta-annotated custom annotations, but Spring doesn't use it uniformly (because of performance concerns). The one that bit me was an ImportBeanDefinitionRegistrar where the annotation was a custom one and the AnnotationMetadata that is passed in does not contain the overridden/inherited values.

The reason, it turns out, is that StandardAnnotationMetadata, while it uses AnnotatedElementUtils, explicitly chooses not to get the overridden attributes by invoking getAllAnnotationAttributes() instead of getMergedAnnotationAttributes() (and this is all that is directly available to the ImportBeanDefinitionRegistrar).

Maybe there's a way to address the performance concerns and use the deeper AnnotatedElementUtils.getMergedAnnotationAttributes() utility method(s) only if the user signals that the annotation needs to be processed this way -- for example, by introducing an annotation such as @Mergeable (or an explicit @AliasFor on the attribute).

See also: #18020


Affects: 4.2 GA

Issue Links:

0 votes, 6 watchers

Activity

  1. spring-projects-issues commented on Sep 9, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Dave Syer commented

    There is a workaround for my current specific issue (because I control the ImportBeanDefinitionRegistrar), which is to use the more powerful method in AnnotatedElementUtils explicitly:

    public class SomethingRegistrar implements ImportBeanDefinitionRegistrar {
    
    	@Override
    	public void registerBeanDefinitions(AnnotationMetadata metadata,
    			BeanDefinitionRegistry registry) {
    		AnnotationAttributes attrs = AnnotatedElementUtils.getMergedAnnotationAttributes(
    				ClassUtils.resolveClassName(metadata.getClassName(), null),
    				EnableSomething.class);
    		for (Class<?> type : collectClasses(attrs.get("value"))) {
    			BeanDefinition beanDefinition = new RootBeanDefinition(type);
    			registry.registerBeanDefinition("something", beanDefinition);
    		}
    	}

    N.B. the custom annotation has to use @AliasFor on all its inherited attributes for this to work.

  2. spring-projects-issues commented on Sep 10, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Dave Syer,

    I'm glad you found a work-around for the time being.

    FYI: please note that getAllAnnotationAttributes() and getMergedAnnotationAttributes() actually have quite different semantics. getAllAnnotationAttributes() aggregates attributes across all annotations present above the annotated element; whereas, getMergedAnnotationAttributes() only merges attributes from a single branch in the annotation hierarchy.

    This subtlety may not have any impact on your particular use case, but...

    The reason I mention this difference is that getAllAnnotationAttributes() will find multiple instances of the target annotation (if present), but getMergedAnnotationAttributes() will only find one instance of the target annotation, ignoring any other instances of the target annotation that may be present. Typically, only @Profile and @Conditional require the "aggregate" semantics.

    Make sense?

    N.B. the custom annotation has to use @AliasFor on all its inherited attributes for this to work.

    What do you mean by "all" in this context?

    Are you saying that convention-based attribute overrides do not work? If so, then that's a bug, and please report it.

    Cheers,

    Sam

  3. added
    status: waiting-for-triage
    type: enhancement
    in: core
    and removed
    type: enhancement
    on Jan 11, 2019
  4. added
    status: bulk-closed
    and removed
    status: waiting-for-triage
    on Jan 11, 2019
  5. spring-projects-issues commented on Jan 12, 2019

    @spring-projects-issues
    CollaboratorAuthor

    Bulk closing outdated, unresolved issues. Please, reopen if still relevant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: corestatus: bulk-closed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions