Skip to content

9.0: Cache annotations use grailsCacheManager, as the cache tags do - #17157

Open
codeconsole wants to merge 3 commits into
apache:9.0.xfrom
codeconsole:feat/cache-annotations-use-grails-cache-manager-9.0.x
Open

codeconsole wants to merge 3 commits into
apache:9.0.xfrom
codeconsole:feat/cache-annotations-use-grails-cache-manager-9.0.x

Conversation

@codeconsole

@codeconsole codeconsole commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Closes #17152.

The cache annotations and GrailsCacheAdminService get their cache manager through the GrailsCacheManagerAware trait, which injected a CacheManager by type. So:

  • a cache manager of the application's own, marked primary or with a @Priority, took the annotations away from grailsCacheManager, while the cache tags and the plugin's startup hook still used grailsCacheManager by name;
  • with grails.cache.enabled: false, the annotations took whatever single or primary cache manager the context had, such as the one the Spring Security plugin defines when cacheUsers is set.

#16980 kept that by-type choice on 8.0.x, as a patch release must, and added an alias so Spring takes grailsCacheManager only where it cannot choose. This changes the rule for 9.0.0.

The change

The trait's field is qualified with the bean name:

@Autowired(required = false)
@GrailsCacheManagerQualifier
private CacheManager grailsCacheManager

GrailsCacheManagerQualifier is a Spring qualifier annotation whose value defaults to grailsCacheManager. Spring then injects the bean named grailsCacheManager, or the bean with that alias, and no other:

Beside grailsCacheManager Grails 8 Grails 9
a cache manager marked primary the primary one grailsCacheManager
one with a @Priority that one grailsCacheManager
one marked primary that declares @Qualifier("grailsCacheManager") that one grailsCacheManager
another, as with cacheUsers grailsCacheManager (8.0.1) grailsCacheManager
no grailsCacheManager at all, as with grails.cache.enabled: false the only or primary cache manager none: the methods run uncached
  • Why not Spring's @Qualifier('grailsCacheManager'). Spring matches that to a bean's name or alias, and also to a bean that declares the same qualifier itself, on its class, its @Bean method or its bean definition. Marked primary, such a bean would take the field from the bean named grailsCacheManager, and with no grailsCacheManager it would be injected. GrailsCacheManagerQualifier can only be put on fields, so no class or @Bean method can declare it, and the @Qualifier that marks it has no value, so a bean matches it only by its name or alias.
  • Spring does all of the injection. Matching a qualifier's value to a bean's name or alias is documented Spring behaviour, so Grails reimplements none of Spring's choice. The field keeps its place among the members Spring injects, so autowired methods see it set, and autowireBean (GORM data services), prototypes and the code Spring's AOT processing generates behave as Spring makes them behave. No other cache manager is created to choose one, so a lazy one stays uncreated.
  • To keep caching with a cache manager of the application's own, name it grailsCacheManager or give it that alias; declaring @Qualifier("grailsCacheManager") on it is not enough. With caching enabled it then replaces the plugin's, which a cache provider plugin does too, and must implement GrailsCacheManager, as the startup hook requires.
  • A primary cache manager given the alias grailsCacheManager beside a bean of that name: whichever was registered last owns the name, and the annotations and a lookup by name, as the tags do, get the same one.
  • A grailsCacheManager created from a cache-aware bean no longer starts, as that bean asks for it while it is created. Any rule that takes grailsCacheManager first has this.
  • A grailsCacheManager that is cache-aware itself is given itself, as Spring gives a bean its only candidate. Where circular references are not allowed, it does not start.
  • A CacheManager registered with registerResolvableDependency is still injected where there is no grailsCacheManager, as Spring applies no qualifier to resolvable dependencies. Beside grailsCacheManager it is passed over.
  • Classes compiled against Grails 8, as in plugins not yet compiled for Grails 9, keep their unqualified field until they are compiled again. GrailsCacheManagerAliasRegistrar stays, so they keep the behaviour of Grails 8.0.1.

The cache guide, section 32 of the upgrade guide and the grails-upgrade skill describe the rule and what to do.

Tests

  • GrailsCacheManagerQualifierSpec runs the 1,596 arrangements of cache managers from 8.0 (fix): Start with another CacheManager next to the Grails cache plugin's #16980, now shared in CacheManagerArrangementsSpec, with the alias registrar and without it.
    • What a class compiled against this version gets depends only on how grailsCacheManager is defined: it gets grailsCacheManager where that is plain, primary, lazy, a fallback bean, not a default candidate, with a @Priority or cache-aware itself, and nothing where it is absent, not an autowire candidate, of the wrong type or Spring's null bean. Where grailsCacheManager is created from the cache-aware bean, the context does not start.
    • Three instances of a prototype get the same, the class depends on grailsCacheManager alone, and nothing else is created for the field.
    • A cache manager that declares Spring's @Qualifier('grailsCacheManager') on its class, its @Bean method or its bean definition, primary, with a priority or plain, with the alias and without it, is not taken for the bean, its prototypes, autowireBean or the code Spring's AOT processing generates, compiled and started. With no grailsCacheManager the field stays empty, in a context and in generated code.
    • It also covers the field's annotation, which only fields can carry; autowired methods in the class, inherited, in a subclass and in a superclass, beside a primary cache manager; autowireBean; a provider's cache manager with the alias; a primary cache manager aliased grailsCacheManager beside a bean of that name, in both orders; resolvable dependencies; and generated code beside a plain primary cache manager.
  • GrailsCacheManagerAliasMatrixSpec now injects a class compiled against the Grails 8 trait, from a fixture identical to the trait of v8.0.0. It keeps 8.0.0 as its oracle over the same arrangements, with the known differences of 8.0.1, and the registrar's own features.
  • CacheAutoConfigurationSpec covers the changed cases through the auto-configuration:
    • a primary or @Priority cache manager beside the plugin's, and a primary one whose @Bean method declares @Qualifier("grailsCacheManager");
    • caching disabled, with the application's own cache managers, with one named grailsCacheManager, and with one that only declares the qualifier;
    • a lazy one;
    • a grailsCacheManager built from a cache-aware bean;
    • a cache-aware grailsCacheManager, with circular references allowed and not.
  • Without the qualifier, 47 features of GrailsCacheManagerQualifierSpec and 12 of CacheAutoConfigurationSpec fail. With Spring's @Qualifier('grailsCacheManager') in its place, 19 and 2 fail.
  • All grails-cache tests pass, as do the grails-test-examples/cache app's tests and the CachingSpec of grails-test-examples/app1 and hibernate7/app1.

…tags do

The cache annotations and GrailsCacheAdminService were given a cache
manager by type, so a cache manager of the application's own marked
primary or with a @priority took them from grailsCacheManager, which the
cache tags and the plugin's startup hook use by name, and with
grails.cache.enabled: false they took whatever single or primary cache
manager the context had, such as Spring Security's cacheUsers one.

The trait's field is now qualified with the name grailsCacheManager, so
Spring injects the bean of that name, or the bean with that alias, and
no other, and creates no other to choose. Without one the annotated
methods run uncached. Spring still does all of the injection, so the
field keeps its place among the members it injects, on every path it
injects. A class compiled against Grails 8 keeps its unqualified field
until it is compiled again, and the alias registrar of 8.0.1 stays for
it.

GrailsCacheManagerQualifierSpec checks, over the arrangements now shared
in CacheManagerArrangementsSpec, that what a class compiled against this
version gets depends only on how grailsCacheManager is defined, with the
alias and without it, and covers autowired methods, autowireBean, a
provider's alias, resolvable dependencies and code generated ahead of
time. GrailsCacheManagerAliasMatrixSpec now injects a class compiled
against the Grails 8 trait, and keeps 8.0.0 as its oracle.
CacheAutoConfigurationSpec covers the changed cases. The cache guide,
section 32 of the upgrade guide and the grails-upgrade skill describe it.

Closes apache#17152
@codecov

codecov Bot commented Nov 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 65.7617%. Comparing base (e551bd3) to head (8d05864).

Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##                9.0.x     #17157        +/-   ##
==================================================
- Coverage     65.7675%   65.7617%   -0.0058%     
+ Complexity      33106      33104         -2     
==================================================
  Files            2448       2448                
  Lines          121519     121519                
  Branches        22550      22550                
==================================================
- Hits            79920      79913         -7     
- Misses          31514      31522         +8     
+ Partials        10085      10084         -1     
Files with missing lines Coverage Δ
...roovy/grails/plugin/cache/CacheGrailsPlugin.groovy 68.1818% <ø> (ø)
...ugin/cache/GrailsCacheManagerAliasRegistrar.groovy 76.1905% <ø> (ø)

... and 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A cache annotation can still use a manager other than the one named grailsCacheManager.

…ager

The trait's field was qualified with Spring's @qualifier('grailsCacheManager'),
which Spring also matches to a bean that declares that qualifier itself, on
its class, its @bean method or its bean definition. Such a bean marked
primary was taken for the cache annotations over the bean named
grailsCacheManager, which the cache tags and the plugin's startup hook use,
and with no grailsCacheManager it was injected where the annotated methods
should run uncached.

The field is now qualified with GrailsCacheManagerQualifier, a qualifier
that can only be put on fields, so no class or @bean method can declare it.
Its value, grailsCacheManager, still selects the bean of that name or alias,
and Spring still does all of the injection. The @qualifier that marks it as
a qualifier has no value, so it matches no bean by name either.

GrailsCacheManagerQualifierSpec covers a cache manager declaring the
qualifier in each of the three ways, primary, with a priority and plain,
with and without the alias, for the bean, its prototypes, autowireBean and
code generated ahead of time, and with no grailsCacheManager. It also pins
that a primary cache manager given the alias grailsCacheManager beside a
bean of that name is what the field and a lookup by name both get.
CacheAutoConfigurationSpec covers the case through the auto-configuration.
The cache guide, the upgrade guide and the grails-upgrade skill say that
declaring the qualifier does not select a cache manager.
The feature run from the code Spring's AOT processing generates checked a
cache manager declaring Spring's qualifier grailsCacheManager only on its
class, only marked primary, and only with the alias registrar. It now runs
each declaration, on the class, the @bean method and the bean definition,
primary, with a priority and plain, with the alias and without it, and with
no grailsCacheManager, where the field must stay empty. With Spring's
@qualifier('grailsCacheManager') on the trait's field, the primary rows and
the rows with no grailsCacheManager fail.
@testlens-app

testlens-app Bot commented Nov 24, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 2469a80
▶️ Tests: 135894 executed
⚪️ Checks: 98/98 completed


Learn more about TestLens at testlens.app/docs.

@matrei matrei left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of #17157 at 2469a805ec (base 9.0.x): three commits on e551bd3f88, which has #16980 merged up. 9.0.x is at 814ef026fc now, with nothing new in grails-cache or in the cache and upgrade chapters of the guide, and the PR merges cleanly.

What I ran, on Java 21, without the build cache:

  • The grails-cache suite at this head: 126 tests, no failures.

  • A/B on the trait's field:

    • With Spring's @Qualifier('grailsCacheManager') in place of @GrailsCacheManagerQualifier, 19 features of GrailsCacheManagerQualifierSpec and 2 of CacheAutoConfigurationSpec fail. They are every row with a primary cache manager that declares that qualifier, every row with no grailsCacheManager, and the check of the field's annotation.
    • With no qualifier, 47 and 12 fail.

    Both counts are the ones the description gives.

  • The grails-test-examples/cache app: test (19) and integrationTest (25), and the CachingSpec of grails-test-examples/app1 (20), at this head, no failures.

  • At 8d05864997, the first commit, I also checked two things in a running context:

    • cacheUsers, as on #16980: spring-security/ui/simple with grails-cache added starts, and GrailsCacheAdminService has grailsCacheManager.
    • The alias route that the upgrade note gives, through the auto-configuration: a GrailsCacheManager defined as @Bean(name = ['appCacheManager', 'grailsCacheManager']), beside a primary cache manager. The plugin's grailsCacheManager backs off, the cache-aware bean and GrailsCacheAdminService get the application's, and the startup hook runs.

    The later commits change only which annotation qualifies the field, and the alias rows of the spec cover that, so I didn't repeat these two.

@codeconsole, thanks for writing this up as #17152 and following it so closely. A qualifier is the simplest form the rule could take: one annotation, and Spring does all the rest. It's on 9.0.x, as a change of behaviour has to be, with the upgrade note and the skill entry. James's case holds: with Spring's @Qualifier, a primary cache manager that declares that qualifier takes the field from grailsCacheManager, as the A/B shows. The qualifier that can go only on a field closes that gap without taking any of the choice away from Spring. Nothing below blocks this.

1. A row for a parent context's grailsCacheManager (optional)

The test plan in #17152 lists "an alias grailsCacheManager, and a parent context's". The alias is covered, but the parent context's no longer is: this PR removes the feature of GrailsCacheManagerAliasMatrixSpec that covered it, "the bean named grailsCacheManager is the one a child bean factory sees by that name". It behaves right at this head. This feature passes in GrailsCacheManagerQualifierSpec, with the imports that the spec already has:

    void 'a child context gets the grailsCacheManager it sees by that name beside a primary cache manager of its own: #where'() {
        given:
        GenericApplicationContext parent = new GenericApplicationContext()
        CacheManager parentNamed = new ConcurrentMapCacheManager()
        parent.registerBean(NAMED, CacheManager, { -> parentNamed } as Supplier<CacheManager>)
        parent.refresh()
        GenericApplicationContext child = new GenericApplicationContext(parent)
        AnnotationConfigUtils.registerAnnotationConfigProcessors(child)
        CacheManager childNamed = new ConcurrentMapCacheManager()
        child.registerBean('primaryCacheManager', CacheManager, { -> new ConcurrentMapCacheManager() } as Supplier<CacheManager>,
                { BeanDefinition definition -> definition.primary = true } as BeanDefinitionCustomizer)
        if (childHasOne) {
            child.registerBean(NAMED, CacheManager, { -> childNamed } as Supplier<CacheManager>)
        }
        child.registerBean('probe', CacheAware)
        child.refresh()

        expect:
        child.getBean('probe', CacheAware).grailsCacheManager.is(childHasOne ? childNamed : parentNamed)

        cleanup:
        child?.close()
        parent?.close()

        where:
        where                                    | childHasOne
        'the parent\'s'                          | false
        'the child\'s own, beside the parent\'s' | true
    }

2. What an application's own cache manager needs (optional)

On 8.0.x, the guide says that the annotations cache with an application's own cache manager that is marked primary. That is often a plain Spring one, and the guide itself names JCacheCacheManager. Under section 32, such an application names it grailsCacheManager or gives it that alias, and with caching enabled "it must then implement GrailsCacheManager". The note doesn't tell the reader how much work that is. Half a sentence would show that it's small:

it must then implement GrailsCacheManager, as the plugin's startup hook requires: for a Spring cache manager such as JCacheCacheManager, a subclass that implements cacheExists and destroyCache.

3. FYI: #17074 also adds a section 32

#17074, open against 9.0.x, adds "32. A Sort by an Alias That the Query Does Not Define Is Rejected" to upgrading90x.adoc. Whichever merges second renumbers; nothing to do here now.

Verified as correct

  • How Spring matches the qualifier:
    • Spring documents a bean's name as its default qualifier value. QualifierAnnotationAutowireCandidateResolver matches the value of @GrailsCacheManagerQualifier through BeanDefinitionHolder.matchesName, which includes the aliases. So the bean named grailsCacheManager and a bean with that alias both qualify.
    • Spring also takes a bean that declares the same qualifier annotation on its class, its @Bean method or its bean definition. With @Target(ElementType.FIELD), only a bean definition could declare this one, by naming GrailsCacheManagerQualifier in an AutowireCandidateQualifier, which no application does by accident.
    • The @Qualifier without a value that marks the annotation matches no bean of its own. At this head a primary cache manager that declares Spring's qualifier, in any of the three ways, loses to grailsCacheManager, in a context and in the code that AOT processing generates.
  • The field's annotation: Groovy carries @GrailsCacheManagerQualifier over to the field that it creates in the implementing class, as the first feature of GrailsCacheManagerQualifierSpec checks. A class compiled against the Grails 8 trait keeps its field without one. The fixture is identical to the trait of v8.0.0, and the registrar still serves those classes against 8.0.0 as the oracle.
  • The features removed from GrailsCacheManagerAliasMatrixSpec: the 20 features injected classes compiled in the spec. Those classes now have the qualifier, so the features no longer exercised the alias. They checked the fallback and alias designs against 8.0.0 on paths where Grails code once took part. Spring now handles those on its own, and GrailsCacheManagerQualifierSpec covers the ones that still matter for the qualified field: autowired methods, autowireBean, resolvable dependencies and AOT. Point 1 is the one left out.
  • Caching disabled: with grails.cache.enabled: false, the whole auto-configuration is off. There is no grailsCacheManager, and the annotations run uncached, also beside a cache manager that only declares the qualifier. An application's own cache manager named grailsCacheManager is still used. That's what the guide, the note and the skill say.
  • A cache-aware grailsCacheManager is given itself. It still starts in an application, since GrailsApp defaults spring.main.allow-circular-references to true.
  • The docs:
    • The guide, section 32, the skill and the trait's Javadoc describe the rule as the code has it, including that declaring Spring's @Qualifier("grailsCacheManager") doesn't select a cache manager.
    • The required actions match what the tests show: the BeanCurrentlyInCreationException for a grailsCacheManager built from a cache-aware bean, and the name or alias route.
    • The guide doesn't name GrailsCacheManagerQualifier, which lives in org.grails.plugin.cache beside the trait.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A cache annotation can still use a manager other than the one named grailsCacheManager.


/** The bean named {@code grailsCacheManager}, as the cache tags and the plugin use it. */
@Autowired(required = false)
@GrailsCacheManagerQualifier

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two cache managers exist. One is named grailsCacheManager. The other is primary and also answers to that name. A cache annotation uses the other one. Use the one that is named grailsCacheManager.

This branch has not been deployed

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

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants