This rule reports syntax errors in Spring Expression Language (SpEL) expressions and property placeholders. It verifies that every SpEL expression and property placeholder is properly closed and that the content of each expression or placeholder is syntactically correct.

Why is this an issue?

Only the Spring framework, not the Java compiler, parses SpEL expression inside Spring annotations. This means that the Java compiler does not detect invalid SpEL expressions during compile time. They will cause exceptions during runtime instead, or even fail silently when Spring interprets the expression as a simple string literal.

Exceptions

This rule reports syntactical errors in SpEL expressions but does not consider semantic errors, such as unknown identifiers or incompatible operand data types.

How to fix it

Correct the syntax error in the SpEL expression.

Code examples

Noncompliant code example

@Value("#{systemProperties['user.region'}") // Noncompliant: unclosed "["
private String region;
@Value("#{'${listOfValues}' split(',')}") // Noncompliant: missing operator
private List<String> valuesList;
@Value("#{T(java.lang.Math).random() * 64h}") // Noncompliant: invalid number
private Double randPercent;
@Query("SELECT u FROM User u WHERE u.status = :#{#status+}") // Noncompliant: missing operand for "+"
List<User> findUsersByStatus(@Param("status") String status);
@Value("${myapp.!prop}") // Noncompliant: property key contains an exclamation mark "!"
private String myProperty;
@Value("${my.property:#{1**1}}") // Noncompliant: invalid SpEL expression "1**1"
private Integer myValue;

Compliant solution

@Value("#{systemProperties['user.region']}") // Compliant
private String region;
@Value("#{'${listOfValues}'.split(',')}") // Compliant
private List<String> valuesList;
@Value("#{T(java.lang.Math).random() * 100.0}") // Compliant
private Double randPercent;
@Query("SELECT u FROM User u WHERE u.status = :#{#status+42}") // Compliant
List<User> findUsersByStatus(@Param("status") String status);
@Value("${myapp.prop}") // Compliant
private String myProperty;
@Value("${my.property:#{1*1}}") // Compliant
private Integer myValue;

Resources

Documentation