Skip to content

Support @Cache* as merged composed annotations [SPR-13475] #18054

Closed

Description

@spring-projects-issues

Nicolas Labrot opened SPR-13475 and commented

Given this composed annotation:

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@Cacheable
public @interface MyCacheable {
	String[] cacheNames() default {};
	String key() default "";
}

The resulting @Cacheable attributes are not merged. Issue seems to come from SpringCacheAnnotationParser#parseCacheAnnotations which does not use AnnotatedElementUtils.findMergedAnnotation(*, Cacheable.class);.


Affects: 4.2 GA

Issue Links:

Referenced from: commits ea09e57, 59c88eb, 54703bf

1 votes, 7 watchers

Activity

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

    @spring-projects-issues
    CollaboratorAuthor

    Stéphane Nicoll commented

    Can you share a project that reproduces the problem because we have tests for that stuff.

    Composable annotations may not work indeed but meta-annotations should.

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

    @spring-projects-issues
    CollaboratorAuthor

    Stéphane Nicoll commented

    Alright, scratch that. The merging thing isn't tested. I'll look at that in time for 4.2.2.RELEASE

  3. spring-projects-issues commented on Sep 21, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Nicolas Labrot commented

    If you agree, I can give it a try to a PR.

  4. spring-projects-issues commented on Sep 21, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Nicolas Labrot, there's no need to create a PR for this, since it's straightforward.

    Cheers,

    Sam

  5. spring-projects-issues commented on Sep 22, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Hi guys,

    I unfortunately spoke too soon: this is actually not straightforward.

    The code in question is SpringCacheAnnotationParser.getAnnotations() which collects all annotations of the desired type (e.g., @Cacheable, @CacheEvict, etc.) present or meta-present on the annotated element.

    In other words, even though Spring's caching annotations are not @Repeatable it is in fact possible to declare multiple instances of a given annotation if one or more such annotations are meta-present -- for example:

    @Retention(RUNTIME)
    @Target(METHOD)
    @CacheEvict("foo")
    public @interface EvictFoo {
    
    	@AliasFor(annotation = Cacheable.class, attribute = "key")
    	String key() default "";
    }
    
    @Retention(RUNTIME)
    @Target(METHOD)
    @CacheEvict("bar")
    public @interface EvictBar {
    
    	@AliasFor(annotation = Cacheable.class, attribute = "key")
    	String key() default "";
    }
    
    @EvictFoo(key = "#id + 'foo'")
    @EvictBar(key = "#id + 'bar'")
    public void multipleEvictions(Long id) {}

    Since AnnotatedElementUtils.findMergedAnnotation(...) finds a single target annotation, it is therefore (currently) impossible to support both @EvictFoo and @EvictBar as merged composed annotations on a single annotated element like the multipleEvictions() method above.

    In conclusion: supporting @Cache\* annotations as merged composed annotations will require new functionality in AnnotatedElementUtils and extensive testing. As a consequence, I am pushing this off until Spring Framework 4.3.

    Regards,

    Sam

    p.s. I have raised #17490 to address the missing functionality.

  6. spring-projects-issues commented on Mar 20, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    FYI: #17490 has now been resolved.

  7. spring-projects-issues commented on Mar 21, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Completed as described in GitHub commit 59c88eb:

    Support @Cache* as merged composed annotations

    Prior to this commit, @Cacheable, @CacheEvict, @CachePut, and @Caching
    could be used to create custom stereotype annotations with hardcoded
    values for their attributes; however, it was not possible to create
    composed annotations with attribute overrides.

    This commit addresses this issue by refactoring
    SpringCacheAnnotationParser to use the newly introduced
    findAllMergedAnnotations() method in AnnotatedElementUtils. As a
    result, @Cacheable, @CacheEvict, @CachePut, and @Caching can now be
    used to create custom composed annotations with attribute overrides
    configured via @AliasFor.

  8. spring-projects-issues commented on Oct 6, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Marc Vanbrabant commented

    Sam Brannen could it be that the refactoring from 59c88eb has broken some @CacheConfig configurations? I have created a (hopefully clear enough) bug report at #19347

  9. added
    type: enhancement
    in: core
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: coretype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions