Quick revision
The short answer
A static method is called with the T operator, as in T(java.lang.Math).max(1, 2), which gives 2. Another bean's property is read with @configService.timeout, which works where the evaluation context has a bean resolver, as it does in @Value. Safe navigation and a default are combined as user?.address?.city ?: 'unknown', and SpEL can also filter and map collections with selection, items.?[price > 10], and projection, items.![name].
What to remember
In this answer, we wrote the common SpEL expressions with their results. T(java.lang.Math).max(1, 2) calls a static method and gives 2. @configService.timeout reads another bean's property where a bean resolver is present, as in @Value. #user?.address?.city ?: 'unknown' combines safe navigation with the Elvis operator for a default. Finally, selection with .?[price > 10] filters a collection and projection with .![name] maps it, and both can be chained.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A static method is called with the T operator, as in T(java.lang.Math).max(1, 2), which gives 2. Another bean's property is read with @configService.timeout, which works where the evaluation context has a bean resolver, as it does in @Value. Safe navigation and a default are combined as user?.address?.city ?: 'unknown', and SpEL can also filter and map collections with selection, items.?[price > 10], and projection, items.![name].
Background to this question
SpEL, the Spring Expression Language, appears in @Value, security annotations, cache keys and event conditions, so every Spring developer reads it, and many write small expressions without knowing the syntax well. A coding question like this checks that we can write the common operators correctly and know where each one works. In this answer, we will recap how SpEL is evaluated, then write each expression with its result: a static method call, a bean reference, safe navigation with the Elvis operator, and finally selection and projection on collections.
1A Quick Recap of How SpEL Is Evaluated
SpEL expressions are parsed by an ExpressionParser and evaluated against an evaluation context. The context supplies the root object, whose properties we can name directly, variables, which we refer to with #name, and optionally a bean resolver, which lets @beanName refer to Spring beans. In @Value and most Spring annotations, Spring prepares that context for us; in our own code we create it ourselves.
To try the expressions, we use a small model and a parser. SpEL reads properties through getters, so the model uses ordinary classes with getters.
2Calling a Static Method With T()
The T operator turns a fully qualified class name into a reference to that class, so we can call its static methods or read static fields. Classes in java.lang may be written without the package, but writing the full name is clearer.
Notice that the last line mixes a placeholder into the expression. Spring replaces ${...} first, so SpEL sees T(java.lang.Math).max(1, 2). T() needs a StandardEvaluationContext; the restricted SimpleEvaluationContext, which Spring uses where expressions might come from untrusted input, does not allow type references at all.
3Referencing Another Bean's Property
In @Value, a bean can be referenced with @ followed by its name, and its property read with a dot. Spring's expression resolver for @Value includes a bean resolver, so @configService finds the bean named configService and .timeout calls its getTimeout() method. In @Value we can also write the bean name without @, because the bean factory is available as a source of names there.
The reason the last case fails is that a plain context knows nothing about Spring beans. The error message says no bean resolver is registered in the context, which is the hint to add a BeanFactoryResolver.
4Safe Navigation and the Elvis Operator
Reading user.address.city throws an exception when user or address is null. The safe navigation operator ?. stops and returns null instead. The Elvis operator ?: then supplies a default when its left side is null. Together they read as 'the city if we can get to it, otherwise unknown'.
Here the user is a variable, so we write #user. If User were the root object, the expression would be user?.address?.city or, more naturally, address?.city. Notice that the Elvis operator in SpEL also treats an empty string as missing in recent Spring versions, so check your version if that distinction matters.
5Selection and Projection on Collections
SpEL has two operators for collections. Selection, written .?[condition], keeps the elements for which the condition is true, like a filter. Projection, written .![expression], maps each element to a value. Inside the brackets, property names refer to the current element.
| Operator | Example | Result |
|---|---|---|
T(type) | T(java.lang.Math).max(1, 2) | 2 |
| @bean | @configService.timeout | PT3S |
| ?. | #user?.address?.city | null instead of an exception |
| ?: | #user?.address?.city ?: 'unknown' | "unknown" when null |
| .?[ ] | #items.?[price > 10] | [bag, lamp] |
| .![ ] | #items.![name] | [pen, bag, lamp] |
Selection and projection can be chained, as the third line shows, and .^[...] and .$[...] return only the first or last match. They also work on maps, where selection sees each entry with key and value. These operators are handy in a security or cache expression, but anything longer than one line is usually clearer as Java code in a bean method.
6Follow-up questions
Why does SpEL use getters instead of fields?
SpEL's default property accessor follows JavaBeans conventions: for name it looks for getName() or isName(), and it can also read public fields. That is why the example model has getters. Custom PropertyAccessor implementations can add other access styles; check your version for built-in support of record-style accessors.
Is it safe to evaluate SpEL from user input?
Not with StandardEvaluationContext, because T() and method calls let an expression run arbitrary code. If expressions can come from users, use SimpleEvaluationContext, which supports only property access and a restricted set of features, or better, do not evaluate user-supplied expressions at all.
Can SpEL expressions be compiled for speed?
Yes. SpEL has a compiler that turns frequently evaluated expressions into bytecode, enabled with a SpelParserConfiguration in IMMEDIATE or MIXED mode, or the spring.expression.compiler.mode property. It helps for expressions evaluated millions of times, but not every expression can be compiled.
7Conclusion
In this answer, we wrote the common SpEL expressions with their results. T(java.lang.Math).max(1, 2) calls a static method and gives 2. @configService.timeout reads another bean's property where a bean resolver is present, as in @Value. #user?.address?.city ?: 'unknown' combines safe navigation with the Elvis operator for a default. Finally, selection with .?[price > 10] filters a collection and projection with .![name] maps it, and both can be chained.
Open with
For a static method I use the T operator with the fully qualified class name: T(java.lang.Math).max(1, 2), which evaluates to 2. It needs a standard evaluation context; the restricted SimpleEvaluationContext does not allow type references.
Then, if they want more
To reference another bean's property I write @configService.timeout: the at sign asks the bean resolver for the bean named configService, and .timeout calls its getter.
In @Value Spring sets up a bean resolver for me, so that just works; in my own code I would call setBeanResolver with a BeanFactoryResolver on the context, otherwise I get an error that no bean resolver is registered. For safe navigation and a default I write user?.address?.city ?: 'unknown'.
The question-mark-dot operator returns null instead of throwing when user or address is null, and the Elvis operator replaces that null with unknown. If the user is a variable rather than the root object, it is #user.
And for collections, SpEL has selection and projection: items.?[price > 10] keeps the items whose price is over 10, items.![name] maps each item to its name, and I can chain them to get the names of the expensive items. There are also caret and dollar variants for the first and last match. I keep these expressions short; anything longer belongs in a bean method.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Core technologies
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write SpEL expressions that (1) call a static method, (2) reference another bean's...": https://scrivio-interview-library.netlify.app/spring-boot/spring-configuration/2f2a1af389/