Flat Preloader Icon

Operators in SPEL

  • The Spring Expression Language (SPEL) supports a wide range of operators that allow you to perform various operations on data, evaluate conditions, and manipulate expressions. Here is an overview of the operators available in SPEL:

Arithmetic Operators:

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • %: Modulus (Remainder)
				
					#{
    10 + 5,      // 15
    20 - 8,      // 12
    4 * 3,       // 12
    15 / 2,      // 7.5
    17 % 4       // 1
}

				
			

Relational Operators:

  • ==: Equal to
  • !=: Not equal to
  • <: Less than
  • <=: Less than or equal to
  • >: Greater than
  • >=: Greater than or equal to
				
					#{
    5 == 5,      // true
    10 != 7,     // true
    8 < 12,      // true
    15 <= 15,    // true
    20 > 25,     // false
    30 >= 30     // true
}

				
			

Logical Operators:

  • and, &&: Logical AND
  • or, ||: Logical OR
  • not, !: Logical NOT
				
					#{
    true and false,     // false
    true or false,      // true
    not true,           // false
    true && (false || true)  // true
}

				
			

Conditional (Ternary) Operator:

  • ? :: Conditional operator, similar to the Java ternary operator.
				
					#{
    (5 > 3) ? "Yes" : "No",     // "Yes"
    (2 == 2) ? "A" : "B"        // "A"
}

				
			

Instanceof Operator:

  • instanceof: Checks if an object is an instance of a specific class or interface.
				
					#{
    'Hello' instanceof String,             
    // true
    new ArrayList() instanceof List       
    // true
}

				
			

Safe Navigation Operator:

  • ?.: Used to safely navigate through potentially null object references, avoiding null pointer exceptions.
				
					#{
    someObject?.getProperty(),     
   
    someObject?.list?[0]           
    // If someObject or the list is null, 
    this expression returns null
}

				
			

Elvis Operator:

  • ?:: Also known as the null-safe operator, it provides a default value if the expression on the left is null.
				
					#{
    someValue ?: defaultValue,     //
    If someValue is null, this expression 
    returns defaultValue
    (nullValue != null) ? nullValue 
    : defaultValue 
    // Same as above
}

				
			
These operators make SPEL a powerful language for working with expressions, making decisions, and manipulating data within the Spring Framework. They are commonly used in Spring configurations, annotations, and expressions to handle dynamic values and conditions.