Skip to content

The expression language used in cache annotations can be error prone when referencing explict parameter names [SPR-9525] #14159

@spring-projects-issues

Description

@spring-projects-issues

Travis Schneeberger opened SPR-9525 and commented

We are using the Spring Cache Abstraction pretty heavily. One of the standards we have adopted is always placing cache annotations on the service interface. This allow various implementations of a service to have caching whether the implementation is hand coded, a remote proxy, etc.

For example:

public interface FooService {
  @Cacheable(value=Foo, key="'firstArg=' + #firstArg + '|' + 'secondArg=' + #secondArg")
  Foo getFoo(String firstArg, String secondArg);
}

Many times we are finding that Spring is literally using the following as the cache key:

firstArg=#firstArg|secondArg=#secondArg

This is obviously problematic because everything will generate the same cachekey making caching completely broken.

This happens when Spring cannot find the argument names on the method that spring is proxying for caching. I suspect this is happening when the spring caching proxy is proxying another proxy (for remoting for example) and that proxy is not keeping the method variable names in tact.

It would be nice is spring would throw an exception or at least log an error when parsing a spring expression referencing argument (or other implicit variables ex: root.methodName) names that do not exist. Throwing an exception would be preferred because in the case of caching, this is a hard error.

As a work around for this problem we are doing the following which always works:

public interface FooService {
  @Cacheable(value=Foo, key="'firstArg=' + #p0 + '|' + 'secondArg=' + #p1")
  Foo getFoo(String firstArg, String secondArg);
}

The added benefit of this approach is it can be used without debug symbols. We probably will not switch back to using real parameter names in our SpEl but for the benefit of the community, I figured I file a jira.


Affects: 3.1 GA

Reference URL: https://jira.kuali.org/browse/KULRICE-7514

Issue Links:

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)status: bulk-closedAn outdated, unresolved issue that's closed in bulk as part of a cleaning process

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions