Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Write SpEL expressions that (1) call a static method, (2) reference another bean's property, and (3) use a safe navigation and default value.

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.

Java
public class Address { private final String city; public Address(String c) { city = c; } public String getCity() { return city; } }
public class User { private final Address address; public User(Address a) { address = a; } public Address getAddress() { return address; } }
public class Item { private final String name; private final int price;
public Item(String n, int p) { name = n; price = p; }
public String getName() { return name; } public int getPrice() { return price; } }
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext ctx = new StandardEvaluationContext();

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.

Java
parser.parseExpression("T(java.lang.Math).max(1, 2)").getValue(Integer.class); // 2
parser.parseExpression("T(java.time.Duration).ofSeconds(90).toMinutes()").getValue(); // 1
parser.parseExpression("T(Integer).MAX_VALUE").getValue(); // 2147483647
@Value("#{T(java.lang.Math).max(${app.min-workers:1}, 2)}") int workers; // 2 when the property is missing

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.

Java
@Component("configService")
class ConfigService { public Duration getTimeout() { return Duration.ofSeconds(3); } }
@Value("#{@configService.timeout}") Duration timeout; // PT3S
@Value("#{@configService.timeout.toMillis()}") long timeoutMs; // 3000
// outside @Value, we must supply a bean resolver ourselves
ctx.setBeanResolver(new BeanFactoryResolver(applicationContext));
parser.parseExpression("@configService.timeout").getValue(ctx); // PT3S
StandardEvaluationContext plain = new StandardEvaluationContext(); // no bean resolver
parser.parseExpression("@configService.timeout").getValue(plain); // SpelEvaluationException: no bean resolver registered

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'.

Java
ctx.setVariable("user", new User(new Address("Oslo")));
parser.parseExpression("#user?.address?.city ?: 'unknown'").getValue(ctx); // "Oslo"
ctx.setVariable("user", new User(null));
parser.parseExpression("#user?.address?.city ?: 'unknown'").getValue(ctx); // "unknown"
ctx.setVariable("user", null);
parser.parseExpression("#user?.address?.city ?: 'unknown'").getValue(ctx); // "unknown"
parser.parseExpression("#user.address.city").getValue(ctx); // SpelEvaluationException: property on null

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.

Java
ctx.setVariable("items", List.of(new Item("pen", 2), new Item("bag", 40), new Item("lamp", 25)));
parser.parseExpression("#items.?[price > 10]").getValue(ctx); // [bag, lamp] as Item objects
parser.parseExpression("#items.![name]").getValue(ctx); // [pen, bag, lamp]
parser.parseExpression("#items.?[price > 10].![name]").getValue(ctx); // [bag, lamp]
parser.parseExpression("#items.^[price > 10].name").getValue(ctx); // "bag": first match
parser.parseExpression("#items.$[price > 10].name").getValue(ctx); // "lamp": last match
The operators in this answer
OperatorExampleResult
T(type)T(java.lang.Math).max(1, 2)2
@bean@configService.timeoutPT3S
?.#user?.address?.citynull 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.

What to say in the interviewAbout 80 seconds aloud

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.
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/