Quick revision
The short answer
Besides @Value, Spring uses SpEL in method security annotations such as @PreAuthorize("hasRole('ADMIN') or #id == principal.id"), in cache annotations for keys and conditions such as key = "#order.id", and in @ConditionalOnExpression and @EventListener(condition = ...). The problem with putting logic there is that it lives in strings, which are not compiled or type-checked and are hard to refactor and test. The fix is to move anything complex into a bean method and call that method from the expression.
What to remember
In this answer, we saw where Spring uses SpEL besides @Value: method security annotations such as @PreAuthorize, cache keys and conditions, @EventListener conditions and @ConditionalOnExpression. Logic in those strings is not compiled or type-checked, breaks silently on refactoring, is awkward to test and hard to review. Keeping expressions to a single call into a bean method, where the logic is plain Java with unit tests, keeps the convenience of the annotations without those costs.
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.
Besides @Value, Spring uses SpEL in method security annotations such as @PreAuthorize("hasRole('ADMIN') or #id == principal.id"), in cache annotations for keys and conditions such as key = "#order.id", and in @ConditionalOnExpression and @EventListener(condition = ...). The problem with putting logic there is that it lives in strings, which are not compiled or type-checked and are hard to refactor and test. The fix is to move anything complex into a bean method and call that method from the expression.
Why this question is asked
SpEL makes some Spring features very compact: one annotation can say who may call a method or how to build a cache key. That compactness tempts teams to put more and more logic into strings, until a security rule spans three lines of SpEL that nobody dares to change. Interviewers ask this to see whether we know where SpEL appears and whether we understand the cost of logic that the compiler cannot see. In this answer, we will recap what SpEL is, walk through the main places Spring uses it, look at the problems logic in strings causes, and then refactor an expression into a bean method.
1A Quick Recap of SpEL
SpEL, the Spring Expression Language, is a small language that Spring evaluates at run time. An expression can read properties, call methods, compare values and refer to variables with #name. Each Spring feature that accepts SpEL decides what is available inside the expression: method parameters, the current user, the method's return value, or the event being handled. That is what makes it powerful in annotations: the expression can refer to the call it is attached to.
2Method Security
Spring Security's method security annotations, enabled with @EnableMethodSecurity, take SpEL expressions that decide whether a call is allowed. Inside them, built-in functions such as hasRole and hasAuthority check the current user's authorities, principal and authentication refer to the current user, and #name refers to a method parameter.
Notice that #id only works if the parameter names are available at run time. Since Spring Framework 6.1, that requires compiling with the -parameters flag, which the Spring Boot build plugins configure; without it the expression cannot find id. Also, principal.id assumes the principal object has an id property, which is true only for a custom user type, so the expression silently depends on the authentication setup.
3Caching, Events and Conditions
Spring's cache annotations use SpEL to build keys and decide whether to cache. key chooses what identifies the entry, condition decides before the call whether to use the cache, and unless decides after the call whether to store the result.
| Place | Typical expression | Available inside |
|---|---|---|
@Value | #{${app.limit:10} * 2} | beans, system properties |
@PreAuthorize, @PostAuthorize | hasRole('ADMIN') or #id == principal.id | parameters, principal, returnObject |
@Cacheable, @CacheEvict | key = "#order.id" | parameters, #result |
@EventListener(condition) | #event.amount > 10000 | the event, its fields |
@ConditionalOnExpression | '${app.region}' == 'eu' | properties, beans |
@EventListener conditions filter events before the listener method runs, and @ConditionalOnExpression in Spring Boot registers a configuration only when an expression is true. SpEL also appears in other projects, for example in Spring Data query methods and in Spring Integration and Spring Kafka configuration attributes.
4What Goes Wrong With Logic in Strings
An expression is a string, so the Java compiler treats it as text. A typo in a property name, a renamed parameter or a changed method signature compiles fine and fails only when the expression is evaluated, which for a security rule might be the first call by a particular role in production. IDE support helps but is incomplete, and automated refactoring tools usually do not rename the identifiers inside the string.
Testing is the second problem. The logic in the string can only be tested by calling the annotated method through the proxy with a prepared security context, which is slower and more awkward than calling a method. So teams write fewer tests for exactly the rules that matter most. Third, long expressions are hard to read and review: an access rule with three conditions, two roles and a null check in one string is easy to get subtly wrong, and the wrongness is invisible in a diff.
5Moving Complex Logic Into a Bean Method
The fix keeps the annotation and moves the logic into an ordinary Spring bean. The expression becomes a single method call, and the method is plain Java that the compiler checks and a unit test can call directly.
The remaining string is short enough to check at a glance, and the rule itself is now refactorable, readable and covered by fast tests. We still keep one integration test per annotated method to prove the annotation is wired, for example that a non-admin gets AccessDeniedException. The same pattern works for cache keys, with a key generator bean, and for event conditions, by moving the decision into the listener or a helper bean. A good rule of thumb is that an expression should fit on one line and contain at most one operator; anything more becomes a method.
6Follow-up questions
Is SpEL in annotations slow?
Usually not noticeably. Spring parses each expression once and caches it, and evaluation is cheap compared with a database call. For very hot paths, such as a cache key computed millions of times per second, SpEL's compiler or a custom KeyGenerator can help.
Can we validate SpEL expressions at build time?
Only partly. Expressions are parsed when first used, so a test that calls each annotated method once catches syntax errors and missing names early. Some IDEs check expressions in common annotations, but none can check every place, which is another reason to keep them short.
Why is principal.id risky in a @PreAuthorize expression?
Because the principal's type depends on how authentication is configured: with one setup it is a custom user object with an id, with another it is a String or a JWT. The expression then fails or compares the wrong thing. A bean method can check the type explicitly and fail clearly.
7Conclusion
In this answer, we saw where Spring uses SpEL besides @Value: method security annotations such as @PreAuthorize, cache keys and conditions, @EventListener conditions and @ConditionalOnExpression. Logic in those strings is not compiled or type-checked, breaks silently on refactoring, is awkward to test and hard to review. Keeping expressions to a single call into a bean method, where the logic is plain Java with unit tests, keeps the convenience of the annotations without those costs.
Open with
Beyond @Value, SpEL shows up mainly in four places. Method security: PreAuthorize and PostAuthorize take expressions like hasRole('ADMIN') or #id == principal.id, with method parameters as variables, principal and authentication for the current user, and returnObject after the call.
Then, if they want more
Caching: Cacheable and CacheEvict use SpEL for the key, like #order.id, and for condition and unless, where #result is the return value. Events: EventListener has a condition that filters events, like #event.amount > 10000. And in Spring Boot, ConditionalOnExpression registers configuration only when an expression is true. Spring Data, Integration and Kafka use it in places too.
The problem with logic there is that it is a string. The compiler does not check it, so renaming a parameter from id to profileId still compiles, and at runtime #id is null and users are silently denied.
Refactoring tools miss it, it can only be tested through the proxy with a security context, and long expressions are hard to review. Parameter names also need the -parameters compiler flag since Spring 6.1.
So my rule is that an expression is at most a single call: @profileAccess.canView(#profileId, authentication), with the rule written as plain Java in a bean that has fast unit tests, plus one integration test per annotated method to prove the wiring.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Framework 6
- 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 "Where does Spring use SpEL besides @Value, and what problems does putting logic in SpEL...": https://scrivio-interview-library.netlify.app/spring-boot/spring-configuration/b93851b537/