Spring EL Lists, Maps example

In this article, we show you how to use Spring EL to get value from Map and List. Actually, the way of SpEL works with Map and List is exactly same with Java. See example : //get map whete key = ‘MapA’ @Value("#{testBean.map[‘MapA’]}") private String mapA; //get first value from list, list is 0-based. @Value("#{testBean.list[0]}") …

Read more

Spring EL Operators example

Spring EL supports most of the standard mathematical, logical or relational operators. For example, Relational operators – equal (==, eq), not equal (!=, ne), less than (<, lt), less than or equal (<= , le), greater than (>, gt), and greater than or equal (>=, ge). Logical operators – and, or, and not (!). Mathematical …

Read more

Spring EL bean reference example

In Spring EL, you can reference a bean, and nested properties using a ‘dot (.)‘ symbol. For example, “bean.property_name“. public class Customer { @Value("#{addressBean.country}") private String country; In above code snippet, it inject the value of “country” property from “addressBean” bean into current “customer” class, “country” property. Spring EL in Annotation See following example, show …

Read more

Spring EL regular expression example

Spring EL supports regular expression using a simple keyword “matches“, which is really awesome! For examples, @Value("#{‘100’ matches ‘\\d+’ }") private boolean isDigit; It test whether ‘100‘ is a valid digit via regular expression ‘\\d+‘. Spring EL in Annotation See following Spring EL regular expression examples, some mixed with ternary operator, which makes Spring EL …

Read more

Spring EL ternary operator (if-then-else) example

Spring EL supports ternary operator , perform “if then else” conditional checking. For example, condition ? true : false Spring EL in Annotation Spring EL ternary operator with @Value annotation. In this example, if “itemBean.qtyOnHand” is less than 100, then set “customerBean.warning” to true, else set it to false. package com.mkyong.core; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; …

Read more

Spring EL method invocation example

Spring expression language (SpEL) allow developer uses expression to execute method and inject the method returned value into property, or so called “SpEL method invocation“. Spring EL in Annotation See how to do Spring EL method invocation with @Value annotation. package com.mkyong.core; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("customerBean") public class Customer { @Value("#{‘mkyong’.toUpperCase()}") private String name; …

Read more

Test Spring el with ExpressionParser

Spring expression language (SpEL) supports many functionality, and you can test those expression features with this special “ExpressionParser” interface. Here’s two code snippets, show the basic usage of using Spring EL. SpEL to evaluate the literal string expression. ExpressionParser parser = new SpelExpressionParser(); Expression exp = parser.parseExpression("’put spel expression here’"); String msg = exp.getValue(String.class); SpEL …

Read more

Spring EL hello world example

The Spring EL is similar with OGNL and JSF EL, and evaluated or executed during the bean creation time. In addition, all Spring expressions are available via XML or annotation. In this tutorial, we show you how to use Spring Expression Language(SpEL), to inject String, integer and bean into property, both in XML and annotation. …

Read more