Skip to content

Commit b4e9d91

Browse files
committed
Better document #result semantic
Commit 240f254 has introduced support for `java.util.Optional` in the cache abstraction. If such type is present, the contained value is cached if it is present. This new feature slightly changed the semantic of `#result` that was documented up till this commit as the "return value of the method invocation". This is no longer true as `#result` for `Optional<T>` refers to the `T` instance and not the `Optional` instance. This commit clarifies both the javadoc and the documentation. Issue: SPR-14587
1 parent 2756c36 commit b4e9d91

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

spring-context/src/main/java/org/springframework/cache/annotation/CacheEvict.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@
6969
* following meta-data:
7070
* <ul>
7171
* <li>{@code #result} for a reference to the result of the method invocation, which
72-
* can only be used if {@link #beforeInvocation()} is {@code false}.</li>
72+
* can only be used if {@link #beforeInvocation()} is {@code false}. For supported
73+
* wrappers such as {@code Optional}, {@code #result} refers to the actual object,
74+
* not the wrapper</li>
7375
* <li>{@code #root.method}, {@code #root.target}, and {@code #root.caches} for
7476
* references to the {@link java.lang.reflect.Method method}, target object, and
7577
* affected cache(s) respectively.</li>

spring-context/src/main/java/org/springframework/cache/annotation/CachePut.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
*
3232
* <p>In contrast to the {@link Cacheable @Cacheable} annotation, this annotation
3333
* does not cause the advised method to be skipped. Rather, it always causes the
34-
* method to be invoked and its result to be stored in the associated cache.
34+
* method to be invoked and its result to be stored in the associated cache. Note
35+
* that Java8's {@code Optional} return types are automatically handled and its
36+
* content is stored in the cache if present.
3537
*
3638
* <p>This annotation may be used as a <em>meta-annotation</em> to create custom
3739
* <em>composed annotations</em> with attribute overrides.
@@ -73,7 +75,9 @@
7375
* <p>The SpEL expression evaluates against a dedicated context that provides the
7476
* following meta-data:
7577
* <ul>
76-
* <li>{@code #result} for a reference to the result of the method invocation.</li>
78+
* <li>{@code #result} for a reference to the result of the method invocation. For
79+
* supported wrappers such as {@code Optional}, {@code #result} refers to the actual
80+
* object, not the wrapper</li>
7781
* <li>{@code #root.method}, {@code #root.target}, and {@code #root.caches} for
7882
* references to the {@link java.lang.reflect.Method method}, target object, and
7983
* affected cache(s) respectively.</li>
@@ -138,7 +142,9 @@
138142
* <p>The SpEL expression evaluates against a dedicated context that provides the
139143
* following meta-data:
140144
* <ul>
141-
* <li>{@code #result} for a reference to the result of the method invocation.</li>
145+
* <li>{@code #result} for a reference to the result of the method invocation. For
146+
* supported wrappers such as {@code Optional}, {@code #result} refers to the actual
147+
* object, not the wrapper</li>
142148
* <li>{@code #root.method}, {@code #root.target}, and {@code #root.caches} for
143149
* references to the {@link java.lang.reflect.Method method}, target object, and
144150
* affected cache(s) respectively.</li>

spring-context/src/main/java/org/springframework/cache/annotation/Cacheable.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
* replace the default one (see {@link #keyGenerator}).
3939
*
4040
* <p>If no value is found in the cache for the computed key, the target method
41-
* will be invoked and the returned value stored in the associated cache.
41+
* will be invoked and the returned value stored in the associated cache. Note
42+
* that Java8's {@code Optional} return types are automatically handled and its
43+
* content is stored in the cache if present.
4244
*
4345
* <p>This annotation may be used as a <em>meta-annotation</em> to create custom
4446
* <em>composed annotations</em> with attribute overrides.
@@ -144,7 +146,9 @@
144146
* <p>The SpEL expression evaluates against a dedicated context that provides the
145147
* following meta-data:
146148
* <ul>
147-
* <li>{@code #result} for a reference to the result of the method invocation.</li>
149+
* <li>{@code #result} for a reference to the result of the method invocation. For
150+
* supported wrappers such as {@code Optional}, {@code #result} refers to the actual
151+
* object, not the wrapper</li>
148152
* <li>{@code #root.method}, {@code #root.target}, and {@code #root.caches} for
149153
* references to the {@link java.lang.reflect.Method method}, target object, and
150154
* affected cache(s) respectively.</li>

src/asciidoc/integration.adoc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8459,6 +8459,18 @@ only want to cache paperback books:
84598459
public Book findBook(String name)
84608460
----
84618461

8462+
The cache abstraction supports `java.util.Optional`, using its content as cached value
8463+
only if it present. `#result` always refers to the business entity and never on a
8464+
supported wrapper so the previous example can be rewritten as follows:
8465+
8466+
[source,java,indent=0]
8467+
[subs="verbatim,quotes"]
8468+
----
8469+
@Cacheable(cacheNames="book", condition="#name.length < 32", **unless="#result.hardback"**)
8470+
public Optional<Book> findBook(String name)
8471+
----
8472+
8473+
Note that `result` still refers to `Book` and not `Optional`.
84628474

84638475
[[cache-spel-context]]
84648476
===== Available caching SpEL evaluation context
@@ -8515,7 +8527,8 @@ conditional computations:
85158527
| evaluation context
85168528
| The result of the method call (the value to be cached). Only available in `unless`
85178529
expressions, `cache put` expressions (to compute the `key`), or `cache evict`
8518-
expressions (when `beforeInvocation` is `false`).
8530+
expressions (when `beforeInvocation` is `false`). For supported wrappers such as
8531+
`Optional`, `#result` refers to the actual object, not the wrapper.
85198532
| `#result`
85208533
|===
85218534

0 commit comments

Comments
 (0)