Skip to content

Commit cca2450

Browse files
committed
Improve documentation for SpelCompilerMode
Closes gh-33223
1 parent de8a94f commit cca2450

File tree

2 files changed

+64
-22
lines changed

2 files changed

+64
-22
lines changed

framework-docs/modules/ROOT/pages/core/expressions/evaluation.adoc

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -444,18 +444,27 @@ component. This section discusses both of these options.
444444
The compiler can operate in one of three modes, which are captured in the
445445
`org.springframework.expression.spel.SpelCompilerMode` enum. The modes are as follows.
446446

447-
* `OFF` (default): The compiler is switched off.
448-
* `IMMEDIATE`: In immediate mode, the expressions are compiled as soon as possible. This
449-
is typically after the first interpreted evaluation. If the compiled expression fails
450-
(typically due to a type changing, as described earlier), the caller of the expression
451-
evaluation receives an exception.
452-
* `MIXED`: In mixed mode, the expressions silently switch between interpreted and
453-
compiled mode over time. After some number of interpreted runs, they switch to compiled
454-
form and, if something goes wrong with the compiled form (such as a type changing, as
455-
described earlier), the expression automatically switches back to interpreted form
456-
again. Sometime later, it may generate another compiled form and switch to it.
457-
Basically, the exception that the user gets in `IMMEDIATE` mode is instead handled
458-
internally.
447+
`OFF` ::
448+
The compiler is switched off, and all expressions will be evaluated in _interpreted_
449+
mode. This is the default mode.
450+
`IMMEDIATE` ::
451+
In immediate mode, expressions are compiled as soon as possible, typically after the
452+
first interpreted evaluation. If evaluation of the compiled expression fails (for
453+
example, due to a type changing, as described earlier), the caller of the expression
454+
evaluation receives an exception. If the types of various expression elements change
455+
over time, consider switching to `MIXED` mode or turning off the compiler.
456+
`MIXED` ::
457+
In mixed mode, expression evaluation silently switches between _interpreted_ and
458+
_compiled_ over time. After some number of successful interpreted runs, the expression
459+
gets compiled. If evaluation of the compiled expression fails (for example, due to a
460+
type changing), that failure will be caught internally, and the system will switch back
461+
to interpreted mode for the given expression. Basically, the exception that the caller
462+
receives in `IMMEDIATE` mode is instead handled internally. Sometime later, the
463+
compiler may generate another compiled form and switch to it. This cycle of switching
464+
between interpreted and compiled mode will continue until the system determines that it
465+
does not make sense to continue trying — for example, when a certain failure threshold
466+
has been reached — at which point the system will permanently switch to interpreted
467+
mode for the given expression.
459468

460469
`IMMEDIATE` mode exists because `MIXED` mode could cause issues for expressions that
461470
have side effects. If a compiled expression blows up after partially succeeding, it

spring-expression/src/main/java/org/springframework/expression/spel/SpelCompilerMode.java

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,30 +17,63 @@
1717
package org.springframework.expression.spel;
1818

1919
/**
20-
* Captures the possible configuration settings for a compiler that can be
21-
* used when evaluating expressions.
20+
* Captures the possible configuration settings for a compiler that can be used
21+
* when evaluating expressions.
22+
*
23+
* <p>Spring provides a basic compiler for SpEL expressions. Expressions are usually
24+
* interpreted, which provides a lot of dynamic flexibility during evaluation but does
25+
* not provide optimum performance. For occasional expression usage, this is fine, but
26+
* when used by other components such as Spring Integration, performance can be very
27+
* important, and there is no real need for the dynamism.
28+
*
29+
* <p>The SpEL compiler is intended to address this need. During evaluation, the compiler
30+
* generates a Java class that embodies the expression behavior at runtime and uses that
31+
* class to achieve much faster expression evaluation. Due to the lack of typing around
32+
* expressions, the compiler uses information gathered during the interpreted evaluations
33+
* of an expression when performing compilation. For example, SpEL does not know the type
34+
* of a property reference purely from the expression, but during the first interpreted
35+
* evaluation, it finds out what it is. Of course, basing compilation on such derived
36+
* information can cause trouble later if the types of the various expression elements
37+
* change over time. For this reason, compilation is best suited to expressions whose
38+
* type information is not going to change on repeated evaluations.
2239
*
2340
* @author Andy Clement
41+
* @author Sam Brannen
2442
* @since 4.1
2543
*/
2644
public enum SpelCompilerMode {
2745

2846
/**
29-
* The compiler is switched off; this is the default.
47+
* The compiler is turned off, and all expressions will be evaluated in
48+
* <em>interpreted</em> mode.
49+
* <p>This is the default.
3050
*/
3151
OFF,
3252

3353
/**
34-
* In immediate mode, expressions are compiled as soon as possible (usually after 1 interpreted run).
35-
* If a compiled expression fails it will throw an exception to the caller.
54+
* In immediate mode, expressions are compiled as soon as possible, typically
55+
* after the first interpreted evaluation.
56+
* <p>If evaluation of the compiled expression fails (for example, due to a type
57+
* changing), the caller of the expression evaluation receives an exception.
58+
* <p>If the types of various expression elements change over time, consider
59+
* switching to {@link #MIXED} mode or turning {@linkplain #OFF off} the compiler.
3660
*/
3761
IMMEDIATE,
3862

3963
/**
40-
* In mixed mode, expression evaluation silently switches between interpreted and compiled over time.
41-
* After a number of runs the expression gets compiled. If it later fails (possibly due to inferred
42-
* type information changing) then that will be caught internally and the system switches back to
43-
* interpreted mode. It may subsequently compile it again later.
64+
* In mixed mode, expression evaluation silently switches between <em>interpreted</em>
65+
* and <em>compiled</em> over time.
66+
* <p>After some number of successful interpreted runs, the expression gets
67+
* compiled. If evaluation of the compiled expression fails (for example, due
68+
* to a type changing), that failure will be caught internally, and the system
69+
* will switch back to interpreted mode for the given expression. Basically,
70+
* the exception that the caller receives in {@link #IMMEDIATE} mode is instead
71+
* handled internally. Sometime later, the compiler may generate another compiled form and switch to it.
72+
* This cycle of switching between interpreted and compiled mode will continue
73+
* until the system determines that it does not make sense to continue trying
74+
* &mdash; for example, when a certain failure threshold has been reached &mdash;
75+
* at which point the system will permanently switch to interpreted mode for the
76+
* given expression.
4477
*/
4578
MIXED
4679

0 commit comments

Comments
 (0)