SpEl

Spring Expression Language Example

The Spring Expression Language (SpEL for short) is a powerful expression language that supports querying and manipulating an object graph at runtime. SpEL expressions can be used with XML or annotation based configuration metadata for defining BeanDefinitions. In both cases the syntax to define the expression is of the form #{ <expression string> }.

We can use SpEL to inject a bean or a bean property in another bean, or even to invoke a bean method in another bean. SpEL also supports most of the standard mathematical, logical or relational operators, as also the ternary operator (if-then-else) to perform conditional checking. We can also get the elements of a Map or a List using SpEL just like we do in Java. Regular expressions are also supported in SpEL using the matches operator. In addition, Spring provides its own API for evaluating expressions.

In this example we will demonstrate how to use SpEL with XML and annotation based configuration to implement all the cases mentioned above. We will also show how to use the ExpressionParser interface provided by Spring API for parsing an expression string.

Our preferred development environment is Eclipse. We are using Eclipse Juno (4.2) version, along with Maven Integration plugin version 3.1.0. You can download Eclipse from here and Maven Plugin for Eclipse from here. The installation of Maven plugin for Eclipse is out of the scope of this tutorial and will not be discussed. We are also using Spring version 3.2.3 and the JDK 7_u_21.

Let’s begin.

1. Create a new Maven project

Go to File -> Project ->Maven -> Maven Project.

New-Maven-Project

In the “Select project name and location” page of the wizard, make sure that “Create a simple project (skip archetype selection)” option is checked, hit “Next” to continue with default values.

Maven-Project-Name-Location

In the “Enter an artifact id” page of the wizard, you can define the name and main package of your project. We will set the “Group Id” variable to "com.javacodegeeks.snippets.enterprise" and the “Artifact Id” variable to "springexample". The aforementioned selections compose the main project package as "com.javacodegeeks.snippets.enterprise.springexample" and the project name as "springexample". Hit “Finish” to exit the wizard and to create your project.

Configure-Maven-Project

The Maven project structure is shown below:

Maven-project-structure

    It consists of the following folders:

  • /src/main/java folder, that contains source files for the dynamic content of the application,
  • /src/test/java folder contains all source files for unit tests,
  • /src/main/resources folder contains configurations files,
  • /target folder contains the compiled and packaged deliverables,
  • the pom.xml is the project object model (POM) file. The single file that contains all project related configuration.

2. Add Spring 3.2.3 dependency

  • Locate the “Properties” section at the “Overview” page of the POM editor and perform the following changes:
    Create a new property with name org.springframework.version and value 3.2.3.RELEASE.
  • Navigate to the “Dependencies” page of the POM editor and create the following dependencies (you should fill the “GroupId”, “Artifact Id” and “Version” fields of the “Dependency Details” section at that page):
    Group Id : org.springframework Artifact Id : spring-web Version : ${org.springframework.version}

Alternatively, you can add the Spring dependencies in Maven’s pom.xml file, by directly editing it at the “Pom.xml” page of the POM editor, as shown below:
 
pom.xml:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.javacodegeeks.snippets.enterprise</groupId>
    <artifactId>springexample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
 
    <properties>
        <spring.version>3.2.3.RELEASE</spring.version>
    </properties>
</project>

As you can see Maven manages library dependencies declaratively. A local repository is created (by default under {user_home}/.m2 folder) and all required libraries are downloaded and placed there from public repositories. Furthermore intra – library dependencies are automatically resolved and manipulated.

Note that there is no need for extra dependency in order to include the Spring EL in the pom.xml, since it is included in Spring core.

3. A simple bean reference example with Spring EL

Let’s start with injecting a bean in another bean using SpEL. Book.java class is a bean with two properties. We will inject the bookBean and one of its properties to another bean that belongs to Author.java class. The two classes are presented below:

Book.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.javacodegeeks.snippets.enterprise;
 
 
public class Book {
 
    private long id;
     
    private String title;
 
    public long getId() {
        return id;
    }
 
    public void setId(long id) {
        this.id = id;
    }
 
    public String getTitle() {
        return title;
    }
 
    public void setTitle(String title) {
        this.title = title;
    }
 
    @Override
    public String toString(){
        return title;
    }
}

Author.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.javacodegeeks.snippets.enterprise;
 
 
public class Author {
     
    private String name;
     
    private Book book;
     
    private String bookTitle;
     
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
     
    public Book getBook() {
        return book;
    }
 
    public void setBook(Book book) {
        this.book = book;
    }
 
    public String getBookTitle() {
        return bookTitle;
    }
 
    public void setBookTitle(String bookTitle) {
        this.bookTitle = bookTitle;
    }
     
    @Override
    public String toString(){
        return name + " has writen the book : " + book + ". \n" + bookTitle + " is a wonderful title of a wonderful book.";
    }
}

3.1 XML-based configuration

The beans are defined in applicationContext.xml file. The #{bookBean} expression is used to inject bookBean into book property of authorBean, whereas the #{bookBean.title} is used to inject title property of bookBean into bookTitle property of authorBean.

applicationContext.xml:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
 
    <bean id="bookBean" class="com.javacodegeeks.snippets.enterprise.Book">
        <property name="id" value="12345" />
        <property name="title" value="Le Petit Prince (The Little Prince)" />
    </bean>
  
    <bean id="authorBean" class="com.javacodegeeks.snippets.enterprise.Author">
        <property name="name" value="Antoine de Saint-Exupéry" />
        <property name="book" value="#{bookBean}" />
        <property name="bookTitle" value="#{bookBean.title}" />
    </bean>
     
</beans>

3.2 Annotation-based configuration

The same expressions will be used in annotations. The @Component annotation is used to register the component in Spring and the @Value annotation is used to set the values into beans properties. The two classes with the annotations are presented below:

Book.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.javacodegeeks.snippets.enterprise;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
@Component("bookBean")
public class Book {
 
    @Value("12345")
    private long id;
     
    @Value("Le Petit Prince (The Little Prince)")
    private String title;
 
    public long getId() {
        return id;
    }
 
    public void setId(long id) {
        this.id = id;
    }
 
    public String getTitle() {
        return title;
    }
 
    public void setTitle(String title) {
        this.title = title;
    }
     
    @Override
    public String toString(){
        return title;
    }
}

Author.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.javacodegeeks.snippets.enterprise;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
 
@Component("authorBean")
public class Author {
     
    @Value("Antoine de Saint-Exupéry")
    private String name;
     
    @Value("#{bookBean}")
    private Book book;
     
    @Value("#{bookBean.title}")
    private String bookTitle;
     
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
     
    public Book getBook() {
        return book;
    }
 
    public void setBook(Book book) {
        this.book = book;
    }
 
    public String getBookTitle() {
        return bookTitle;
    }
 
    public void setBookTitle(String bookTitle) {
        this.bookTitle = bookTitle;
    }
     
    @Override
    public String toString(){
        return name + " has writen the book : " + book + ". \n" + bookTitle + " is a wonderful title of a wonderful book.";
    }  
}

The only thing needed in applicationContext.xml is to enable auto component-scan, as shown below:

applicationContext.xml

3.3 Run the application

We can run this application, in both xml and annotation based configuration, using the App.java class.

App.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
package com.javacodegeeks.snippets.enterprise;
 
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class App {
 
    public static void main(String[] args) {
     
            ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            Author author = (Author) context.getBean("authorBean");
            System.out.println(author.toString());
            context.close();
    }
}

The output is the one presented below:

Antoine de Saint-Exupéry has writen the book : Le Petit Prince (The Little Prince). 
Le Petit Prince (The Little Prince) is a wonderful title of a wonderful book.

 

4. Method invocation with Spring EL

Now, we will use Expression Language to execute a method in bookBean and inject the result in authorBean. We are adding a method, String getBookInfo(String authorName) in Book.java class and a new property, fullAuthorInfo in Author.java class, as shown below:

Book.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.javacodegeeks.snippets.enterprise;
 
 
public class Book {
 
    private long id;
     
    private String title;
 
    public long getId() {
        return id;
    }
 
    public void setId(long id) {
        this.id = id;
    }
 
    public String getTitle() {
        return title;
    }
 
    public void setTitle(String title) {
        this.title = title;
    }
     
    public String getBookInfo(String authorName){
        return authorName + " has writen the book " + title + ", with book id " + ""+ id + ".";
    }
 
    @Override
    public String toString(){
        return title;
    }
}

Author.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.javacodegeeks.snippets.enterprise;
 
 
public class Author {
     
    private String name;
     
    private Book book;
     
    private String bookTitle;
     
    private String fullAuthorInfo;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
     
    public Book getBook() {
        return book;
    }
 
    public void setBook(Book book) {
        this.book = book;
    }
 
    public String getBookTitle() {
        return bookTitle;
    }
 
    public void setBookTitle(String bookTitle) {
        this.bookTitle = bookTitle;
    }
     
    public String getFullAuthorInfo() {
        return fullAuthorInfo;
    }
 
    public void setFullAuthorInfo(String fullAuthorInfo) {
        this.fullAuthorInfo = fullAuthorInfo;
    }
     
    @Override
    public String toString(){
        return name + " has writen the book : " + book + ". \n" + bookTitle + " is a wonderful title of a wonderful book.";
    }
}

4.1 XML-based configuration

Let’s define the new property in applicationContext.xml file.

applicationContext.xml:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
 
    <bean id="bookBean" class="com.javacodegeeks.snippets.enterprise.Book">
        <property name="id" value="12345" />
        <property name="title" value="Le Petit Prince (The Little Prince)" />
    </bean>
  
    <bean id="authorBean" class="com.javacodegeeks.snippets.enterprise.Author">
        <property name="name" value="Antoine de Saint-Exupéry" />
        <property name="book" value="#{bookBean}" />
        <property name="bookTitle" value="#{bookBean.title}" />
        <property name="fullAuthorInfo" value="#{bookBean.getBookInfo('Antoine de Saint-Exupéry')}" />
    </bean>
     
</beans>

4.2 Annotation-based configuration

The same steps are followed using annotations:

Book.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.javacodegeeks.snippets.enterprise;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
@Component("bookBean")
public class Book {
 
    @Value("12345")
    private long id;
     
    @Value("Le Petit Prince (The Little Prince)")
    private String title;
 
    public long getId() {
        return id;
    }
 
    public void setId(long id) {
        this.id = id;
    }
 
    public String getTitle() {
        return title;
    }
 
    public void setTitle(String title) {
        this.title = title;
    }
     
    public String getBookInfo(String authorName){
        return authorName + " has writen the book " + title + ", with book id " + ""+ id + ".";
    }
 
    @Override
    public String toString(){
        return title;
    }
}

Author.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.javacodegeeks.snippets.enterprise;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
 
@Component("authorBean")
public class Author {
     
    @Value("Antoine de Saint-Exupéry")
    private String name;
     
    @Value("#{bookBean}")
    private Book book;
     
    @Value("#{bookBean.title}")
    private String bookTitle;
 
    @Value("#{bookBean.getBookInfo('Antoine de Saint-Exupéry')}")
    private String fullAuthorInfo;
     
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
     
    public Book getBook() {
        return book;
    }
 
    public void setBook(Book book) {
        this.book = book;
    }
 
    public String getBookTitle() {
        return bookTitle;
    }
 
    public void setBookTitle(String bookTitle) {
        this.bookTitle = bookTitle;
    }
     
    public String getFullAuthorInfo() {
        return fullAuthorInfo;
    }
 
    public void setFullAuthorInfo(String fullAuthorInfo) {
        this.fullAuthorInfo = fullAuthorInfo;
    }
     
    @Override
    public String toString(){
        return name + " has writen the book : " + book + ". \n" + bookTitle + " is a wonderful title of a wonderful book.";
    }  
}

In this case applicationContext.xml file is the same as the one in the previous annotation-based configuration.

applicationContext.xml

4.3 Run the application

Using the same App.java class as in 3.3, we are getting the output below:

App.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
package com.javacodegeeks.snippets.enterprise;
 
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class App {
 
    public static void main(String[] args) {
     
            ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            Author author = (Author) context.getBean("authorBean");
            System.out.println(author.toString());
            System.out.println(author.getFullAuthorInfo());
            context.close();
    }
}
Antoine de Saint-Exupéry has writen the book : Le Petit Prince (The Little Prince). 
Le Petit Prince (The Little Prince) is a wonderful title of a wonderful book.
Antoine de Saint-Exupéry has writen the book Le Petit Prince (The Little Prince), with book id 12345.

5. Operators with Spring EL

Operators that can be used in Spring EL are relational, logical and mathematical operators. We are using all of them in this example. Numbers.class is a class that contains int properties. Operators.class is a class with properties that will be used to hold results after applying Spring EL operators over the properties of Numbers.class.

Numbers.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.javacodegeeks.snippets.enterprise;
 
 
public class Numbers {
 
    private int a;
     
    private int b;
     
    private int c;
     
    private int d;
     
    private int e;
 
    public int getA() {
        return a;
    }
 
    public void setA(int a) {
        this.a = a;
    }
 
    public int getB() {
        return b;
    }
 
    public void setB(int b) {
        this.b = b;
    }
 
    public int getC() {
        return c;
    }
 
    public void setC(int c) {
        this.c = c;
    }
 
    public int getD() {
        return d;
    }
 
    public void setD(int d) {
        this.d = d;
    }
 
    public int getE() {
        return e;
    }
 
    public void setE(int e) {
        this.e = e;
    }
}

Operators.java

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.javacodegeeks.snippets.enterprise;
 
 
public class Operators {
     
        //Relational operators
      
        private boolean equalTest;
      
        private boolean notEqualTest;
      
        private boolean lessThanTest;
      
        private boolean lessThanOrEqualTest;
      
        private boolean greaterThanTest;
      
        private boolean greaterThanOrEqualTest;
      
        //Logical operators
      
        private boolean andTest;
      
        private boolean orTest;
      
        private boolean notTest;
      
        //Mathematical operators
      
        private double addTest;
      
        private String addStringTest;
      
        private double subtractionTest;
      
        private double multiplicationTest;
      
        private double divisionTest;
      
        private double modulusTest ;
      
        private double exponentialPowerTest;
 
        public boolean isEqualTest() {
            return equalTest;
        }
 
        public void setEqualTest(boolean equalTest) {
            this.equalTest = equalTest;
        }
 
        public boolean isNotEqualTest() {
            return notEqualTest;
        }
 
        public void setNotEqualTest(boolean notEqualTest) {
            this.notEqualTest = notEqualTest;
        }
 
        public boolean isLessThanTest() {
            return lessThanTest;
        }
 
        public void setLessThanTest(boolean lessThanTest) {
            this.lessThanTest = lessThanTest;
        }
 
        public boolean isLessThanOrEqualTest() {
            return lessThanOrEqualTest;
        }
 
        public void setLessThanOrEqualTest(boolean lessThanOrEqualTest) {
            this.lessThanOrEqualTest = lessThanOrEqualTest;
        }
 
        public boolean isGreaterThanTest() {
            return greaterThanTest;
        }
 
        public void setGreaterThanTest(boolean greaterThanTest) {
            this.greaterThanTest = greaterThanTest;
        }
 
        public boolean isGreaterThanOrEqualTest() {
            return greaterThanOrEqualTest;
        }
 
        public void setGreaterThanOrEqualTest(boolean greaterThanOrEqualTest) {
            this.greaterThanOrEqualTest = greaterThanOrEqualTest;
        }
 
        public boolean isAndTest() {
            return andTest;
        }
 
        public void setAndTest(boolean andTest) {
            this.andTest = andTest;
        }
 
        public boolean isOrTest() {
            return orTest;
        }
 
        public void setOrTest(boolean orTest) {
            this.orTest = orTest;
        }
 
        public boolean isNotTest() {
            return notTest;
        }
 
        public void setNotTest(boolean notTest) {
            this.notTest = notTest;
        }
 
        public double getAddTest() {
            return addTest;
        }
 
        public void setAddTest(double addTest) {
            this.addTest = addTest;
        }
 
        public String getAddStringTest() {
            return addStringTest;
        }
 
        public void setAddStringTest(String addStringTest) {
            this.addStringTest = addStringTest;
        }
 
        public double getSubtractionTest() {
            return subtractionTest;
        }
 
        public void setSubtractionTest(double subtractionTest) {
            this.subtractionTest = subtractionTest;
        }
 
        public double getMultiplicationTest() {
            return multiplicationTest;
        }
 
        public void setMultiplicationTest(double multiplicationTest) {
            this.multiplicationTest = multiplicationTest;
        }
 
        public double getDivisionTest() {
            return divisionTest;
        }
 
        public void setDivisionTest(double divisionTest) {
            this.divisionTest = divisionTest;
        }
 
        public double getModulusTest() {
            return modulusTest;
        }
 
        public void setModulusTest(double modulusTest) {
            this.modulusTest = modulusTest;
        }
 
        public double getExponentialPowerTest() {
            return exponentialPowerTest;
        }
 
        public void setExponentialPowerTest(double exponentialPowerTest) {
            this.exponentialPowerTest = exponentialPowerTest;
        }
         
    @Override
    public String toString() {
        return "equalTest : " + equalTest + " \n"
                + "notEqualTest : " + notEqualTest + " \n"
                + "lessThanTest : " + lessThanTest + " \n"
                + "lessThanOrEqualTest : " + lessThanOrEqualTest + " \n"
                + "greaterThanTest : " + greaterThanTest + " \n"
                + "greaterThanOrEqualTest : " + greaterThanOrEqualTest + " \n"
                + "andTest : " + andTest + " \n"
                + "orTest : " + orTest + " \n"
                + "notTest : " + notTest + " \n"
                + "addTest : " + addTest + " \n"
                + "addStringTest : " + addStringTest + " \n"
                + "subtractionTest : " + subtractionTest + " \n"
                + "multiplicationTest " + multiplicationTest + " \n"
                + "divisionTest : " + divisionTest + " \n"
                + "modulusTest : " + modulusTest + " \n"
                + "exponentialPowerTest : " + exponentialPowerTest;
    }
}

5.1 XML-based configuration

In applicationContext2.xml we define the operators in each property, using the expression language.

applicationContext2.xml

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 
    <bean id="numbersBean" class="com.javacodegeeks.snippets.enterprise.Numbers">
        <property name="a" value="100" />
        <property name="b" value="150" />
        <property name="c" value="200" />
        <property name="d" value="250" />
        <property name="e" value="300" />
    </bean>
     
    <bean id="operatorsBean" class="com.javacodegeeks.snippets.enterprise.Operators">
    <property name="equalTest" value="#{numbersBean.a == 100}" />
    <property name="notEqualTest" value="#{numbersBean.a != numbersBean.b}" />
    <property name="lessThanTest" value="#{numbersBean.b lt numbersBean.a}" />
    <property name="lessThanOrEqualTest" value="#{numbersBean.c le numbersBean.b}" />
    <property name="greaterThanTest" value="#{numbersBean.d > numbersBean.e}" />
    <property name="greaterThanOrEqualTest" value="#{numbersBean.d >= numbersBean.c}" />
    <property name="andTest" value="#{numbersBean.a == 100 and numbersBean.b lt 100}" />
    <property name="orTest" value="#{numbersBean.c == 150 or numbersBean.d lt 250}" />
    <property name="notTest" value="#{!(numbersBean.e == 300)}" />
    <property name="addTest" value="#{numbersBean.a + numbersBean.b}" />
    <property name="addStringTest" value="#{'hello' + '@' + 'world'}" />
    <property name="subtractionTest" value="#{numbersBean.d - numbersBean.c}" />
    <property name="multiplicationTest" value="#{numbersBean.a * numbersBean.e}" />
    <property name="divisionTest" value="#{numbersBean.e / numbersBean.a}" />
    <property name="modulusTest" value="#{numbersBean.e % numbersBean.b}" />
    <property name="exponentialPowerTest" value="#{numbersBean.a ^ 2}" />
    </bean>
     
     
</beans>

5.2 Annotation-based configuration

The expressions are now set in the @Value annotations of Operators.java class.

Numbers.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.javacodegeeks.snippets.enterprise;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
@Component("numbersBean")
public class Numbers {
 
    @Value("100")
    private int a;
     
    @Value("150")
    private int b;
     
    @Value("200")
    private int c;
     
    @Value("250")
    private int d;
     
    @Value("300")
    private int e;
 
    public int getA() {
        return a;
    }
 
    public void setA(int a) {
        this.a = a;
    }
 
    public int getB() {
        return b;
    }
 
    public void setB(int b) {
        this.b = b;
    }
 
    public int getC() {
        return c;
    }
 
    public void setC(int c) {
        this.c = c;
    }
 
    public int getD() {
        return d;
    }
 
    public void setD(int d) {
        this.d = d;
    }
 
    public int getE() {
        return e;
    }
 
    public void setE(int e) {
        this.e = e;
    }
}

Operators.java

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package com.javacodegeeks.snippets.enterprise;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
@Component("operatorsBean")
 
public class Operators {
     
        //Relational operators
      
        @Value("#{numbersBean.a == 100}") //true
        private boolean equalTest;
      
        @Value("#{numbersBean.a != numbersBean.b}") //true
        private boolean notEqualTest;
      
        @Value("#{numbersBean.b < numbersBean.a}") //false
        private boolean lessThanTest;
      
        @Value("#{numbersBean.c <= numbersBean.b}") //false
        private boolean lessThanOrEqualTest;
      
        @Value("#{numbersBean.d > numbersBean.e}") //false
        private boolean greaterThanTest;
      
        @Value("#{numbersBean.d >= numbersBean.c}") //true
        private boolean greaterThanOrEqualTest;
      
        //Logical operators
      
        @Value("#{numbersBean.a == 100 and numbersBean.b < 100}") //false
        private boolean andTest;
      
        @Value("#{numbersBean.c == 150 or numbersBean.d < 250}") //true
        private boolean orTest;
      
        @Value("#{!(numbersBean.e == 300)}") //false
        private boolean notTest;
      
        //Mathematical operators
      
        @Value("#{numbersBean.a + numbersBean.b}") //250
        private double addTest;
      
        @Value("#{'hello' + '@' + 'world'}") //hello@world
        private String addStringTest;
      
        @Value("#{numbersBean.d - numbersBean.c}") //50
        private double subtractionTest;
      
        @Value("#{numbersBean.a * numbersBean.e}") //30000
        private double multiplicationTest;
      
        @Value("#{numbersBean.e / numbersBean.a}") //3
        private double divisionTest;
      
        @Value("#{numbersBean.e % numbersBean.b}") //0.0
        private double modulusTest ;
      
        @Value("#{numbersBean.a ^ 2}") //10000
        private double exponentialPowerTest;
 
        public boolean isEqualTest() {
            return equalTest;
        }
 
        public void setEqualTest(boolean equalTest) {
            this.equalTest = equalTest;
        }
 
        public boolean isNotEqualTest() {
            return notEqualTest;
        }
 
        public void setNotEqualTest(boolean notEqualTest) {
            this.notEqualTest = notEqualTest;
        }
 
        public boolean isLessThanTest() {
            return lessThanTest;
        }
 
        public void setLessThanTest(boolean lessThanTest) {
            this.lessThanTest = lessThanTest;
        }
 
        public boolean isLessThanOrEqualTest() {
            return lessThanOrEqualTest;
        }
 
        public void setLessThanOrEqualTest(boolean lessThanOrEqualTest) {
            this.lessThanOrEqualTest = lessThanOrEqualTest;
        }
 
        public boolean isGreaterThanTest() {
            return greaterThanTest;
        }
 
        public void setGreaterThanTest(boolean greaterThanTest) {
            this.greaterThanTest = greaterThanTest;
        }
 
        public boolean isGreaterThanOrEqualTest() {
            return greaterThanOrEqualTest;
        }
 
        public void setGreaterThanOrEqualTest(boolean greaterThanOrEqualTest) {
            this.greaterThanOrEqualTest = greaterThanOrEqualTest;
        }
 
        public boolean isAndTest() {
            return andTest;
        }
 
        public void setAndTest(boolean andTest) {
            this.andTest = andTest;
        }
 
        public boolean isOrTest() {
            return orTest;
        }
 
        public void setOrTest(boolean orTest) {
            this.orTest = orTest;
        }
 
        public boolean isNotTest() {
            return notTest;
        }
 
        public void setNotTest(boolean notTest) {
            this.notTest = notTest;
        }
 
        public double getAddTest() {
            return addTest;
        }
 
        public void setAddTest(double addTest) {
            this.addTest = addTest;
        }
 
        public String getAddStringTest() {
            return addStringTest;
        }
 
        public void setAddStringTest(String addStringTest) {
            this.addStringTest = addStringTest;
        }
 
        public double getSubtractionTest() {
            return subtractionTest;
        }
 
        public void setSubtractionTest(double subtractionTest) {
            this.subtractionTest = subtractionTest;
        }
 
        public double getMultiplicationTest() {
            return multiplicationTest;
        }
 
        public void setMultiplicationTest(double multiplicationTest) {
            this.multiplicationTest = multiplicationTest;
        }
 
        public double getDivisionTest() {
            return divisionTest;
        }
 
        public void setDivisionTest(double divisionTest) {
            this.divisionTest = divisionTest;
        }
 
        public double getModulusTest() {
            return modulusTest;
        }
 
        public void setModulusTest(double modulusTest) {
            this.modulusTest = modulusTest;
        }
 
        public double getExponentialPowerTest() {
            return exponentialPowerTest;
        }
 
        public void setExponentialPowerTest(double exponentialPowerTest) {
            this.exponentialPowerTest = exponentialPowerTest;
        }
         
    @Override
    public String toString() {
        return "equalTest : " + equalTest + " \n"
                + "notEqualTest : " + notEqualTest + " \n"
                + "lessThanTest : " + lessThanTest + " \n"
                + "lessThanOrEqualTest : " + lessThanOrEqualTest + " \n"
                + "greaterThanTest : " + greaterThanTest + " \n"
                + "greaterThanOrEqualTest : " + greaterThanOrEqualTest + " \n"
                + "andTest : " + andTest + " \n"
                + "orTest : " + orTest + " \n"
                + "notTest : " + notTest + " \n"
                + "addTest : " + addTest + " \n"
                + "addStringTest : " + addStringTest + " \n"
                + "subtractionTest : " + subtractionTest + " \n"
                + "multiplicationTest " + multiplicationTest + " \n"
                + "divisionTest : " + divisionTest + " \n"
                + "modulusTest : " + modulusTest + " \n"
                + "exponentialPowerTest : " + exponentialPowerTest;
    }
}

The only thing needed in applicationContext2.xml is to enable auto component-scan, as shown below:

applicationContext2.xml

5.3 Run the application

We are using App2.class to load the operatorsBean, as shown below:

App2.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
package com.javacodegeeks.snippets.enterprise;
 
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class App2 {
 
    public static void main(String[] args) {
     
            ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
            Operators op = (Operators) context.getBean("operatorsBean");
            System.out.println(op.toString());
            context.close();
    }
}

After running the application the output is the one presented below:

equalTest : true 
notEqualTest : true 
lessThanTest : false 
lessThanOrEqualTest : false 
greaterThanTest : false 
greaterThanOrEqualTest : true 
andTest : false 
orTest : false 
notTest : false 
addTest : 250.0 
addStringTest : hello@world 
subtractionTest : 50.0 
multiplicationTest 30000.0 
divisionTest : 3.0 
modulusTest : 0.0 
exponentialPowerTest : 10000.0

6. Ternary operator (if-then-else) with Spring EL

Using the ternary operator in EL is the same as using the operators above. We are adding a new class, TernaryOperator.java that has one field that will hold the result after applying a Spring EL conditional expression over the fields of Numbers.java class.

TernaryOperator.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
package com.javacodegeeks.snippets.enterprise;
 
public class TernaryOperator {
 
    private boolean check;
 
    public boolean isCheck() {
        return check;
    }
 
    public void setCheck(boolean check) {
        this.check = check;
    }
     
    @Override
    public String toString(){
        return "TernaryOperator, checking if numbersBean.a is less than numbersBean.d : " + check;
    }
}

6.1 XML-based configuration

The new bean is defined in applicationContext2.xml.

applicationContext2.xml

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 
    <bean id="numbersBean" class="com.javacodegeeks.snippets.enterprise.Numbers">
        <property name="a" value="100" />
        <property name="b" value="150" />
        <property name="c" value="200" />
        <property name="d" value="250" />
        <property name="e" value="300" />
    </bean>
     
    <bean id="operatorsBean" class="com.javacodegeeks.snippets.enterprise.Operators">
    <property name="equalTest" value="#{numbersBean.a == 100}" />
    <property name="notEqualTest" value="#{numbersBean.a != numbersBean.b}" />
    <property name="lessThanTest" value="#{numbersBean.b lt numbersBean.a}" />
    <property name="lessThanOrEqualTest" value="#{numbersBean.c le numbersBean.b}" />
    <property name="greaterThanTest" value="#{numbersBean.d > numbersBean.e}" />
    <property name="greaterThanOrEqualTest" value="#{numbersBean.d >= numbersBean.c}" />
    <property name="andTest" value="#{numbersBean.a == 100 and numbersBean.b lt 100}" />
    <property name="orTest" value="#{numbersBean.c == 150 or numbersBean.d lt 250}" />
    <property name="notTest" value="#{!(numbersBean.e == 300)}" />
    <property name="addTest" value="#{numbersBean.a + numbersBean.b}" />
    <property name="addStringTest" value="#{'hello' + '@' + 'world'}" />
    <property name="subtractionTest" value="#{numbersBean.d - numbersBean.c}" />
    <property name="multiplicationTest" value="#{numbersBean.a * numbersBean.e}" />
    <property name="divisionTest" value="#{numbersBean.e / numbersBean.a}" />
    <property name="modulusTest" value="#{numbersBean.e % numbersBean.b}" />
    <property name="exponentialPowerTest" value="#{numbersBean.a ^ 2}" />
    </bean>
     
    <bean id="ternaryOperatorBean" class="com.javacodegeeks.snippets.enterprise.TernaryOperator">
        <property name="check" value="#{numbersBean.a lt numbersBean.d ? true : false}" />
    </bean>
     
</beans>

6.2 Annotation-based configuration

Using annotations, TernaryOperator.java class becomes as shown below:

Want to master Spring Framework ?
Subscribe to our newsletter and download the Spring Framework Cookbook right now!
In order to help you master the leading and innovative Java framework, we have compiled a kick-ass guide with all its major features and use cases! Besides studying them online you may download the eBook in PDF format!

TernaryOperator.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.javacodegeeks.snippets.enterprise;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
@Component("ternaryOperatorBean")
public class TernaryOperator {
 
    @Value("#{numbersBean.a < numbersBean.d ? true : false}")
    private boolean check;
 
    public boolean isCheck() {
        return check;
    }
 
    public void setCheck(boolean check) {
        this.check = check;
    }
     
    @Override
    public String toString(){
        return "TernaryOperator, checking if numbersBean.a is less than numbersBean.d : " + check;
    }
}

Now, the only thing needed in applicationContext2.xml is to enable the auto component scan, as described in all other annotation-based cases above.

applicationContext2.xml

6.3 Run the application

We load the ternaryOperatorBean in App2.java class.

App2.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
package com.javacodegeeks.snippets.enterprise;
 
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class App2 {
 
    public static void main(String[] args) {
     
            ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
            Operators op = (Operators) context.getBean("operatorsBean");
            System.out.println(op.toString());
            TernaryOperator tern = (TernaryOperator) context.getBean("ternaryOperatorBean");
            System.out.println(tern.toString());
            context.close();
    }
}

The result is like the one shown below:

equalTest : true 
notEqualTest : true 
lessThanTest : false 
lessThanOrEqualTest : false 
greaterThanTest : false 
greaterThanOrEqualTest : true 
andTest : false 
orTest : false 
notTest : false 
addTest : 250.0 
addStringTest : hello@world 
subtractionTest : 50.0 
multiplicationTest 30000.0 
divisionTest : 3.0 
modulusTest : 0.0 
exponentialPowerTest : 10000.0
TernaryOperator, checking if numbersBean.a is less than numbersBean.d : true

7. Lists and Maps with Spring EL

Now, lets use Expression Language to get the values of a map and a list. Test.java class has two properties, a map and a list. TestMapList.java class has two fields, that will be used to hold specific elements from the list and the map of Test.java class selected using Sring EL expressions. The two classes are shown below:

Test.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.javacodegeeks.snippets.enterprise;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class Test {
 
    private Map<String, String> map;
    private List<String> list;
  
    public Test() {
        map = new HashMap<String, String>();
        map.put("key1", "Value 1");
        map.put("key2", "Value 2");
        map.put("key3", "Value 3");
  
        list = new ArrayList<String>();
        list.add("list0");
        list.add("list1");
        list.add("list2");
  
    }
  
    public Map<String, String> getMap() {
        return map;
    }
  
    public void setMap(Map<String, String> map) {
        this.map = map;
    }
  
    public List<String> getList() {
        return list;
    }
  
    public void setList(List<String> list) {
        this.list = list;
    }
     
}

TestMapList.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.javacodegeeks.snippets.enterprise;
 
 
public class TestMapList {
     
    private String mapElement;
  
    private String listElement;
   
    public String getMapElement() {
        return mapElement;
    }
 
    public void setMapElement(String mapElement) {
        this.mapElement = mapElement;
    }
 
    public String getListElement() {
        return listElement;
    }
 
    public void setListElement(String listElement) {
        this.listElement = listElement;
    }
 
    @Override
    public String toString() {
        return "TestMapList [mapElement=" + mapElement + ", listElement=" + listElement + "]";
    }
     
}

7.1 XML-based configuration

In applicationContext3.xml we define the two beans:

applicationContext3.xml

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
 
    <bean id="testBean" class="com.javacodegeeks.snippets.enterprise.Test" />
     
    <bean id="testMapListBean" class="com.javacodegeeks.snippets.enterprise.TestMapList">
        <property name="mapElement" value="#{testBean.map['key1']}" />
        <property name="listElement" value="#{testBean.list[0]}" />
    </bean>
 
     
</beans>

7.2 Annotation-based configuration

Using annotations, the classes become as shown below:

Test.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.javacodegeeks.snippets.enterprise;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.springframework.stereotype.Component;
 
@Component("testBean")
public class Test {
 
    private Map<String, String> map;
    private List<String> list;
  
    public Test() {
        map = new HashMap<String, String>();
        map.put("key1", "Value 1");
        map.put("key2", "Value 2");
        map.put("key3", "Value 3");
  
        list = new ArrayList<String>();
        list.add("list0");
        list.add("list1");
        list.add("list2");
  
    }
  
    public Map<String, String> getMap() {
        return map;
    }
  
    public void setMap(Map<String, String> map) {
        this.map = map;
    }
  
    public List<String> getList() {
        return list;
    }
  
    public void setList(List<String> list) {
        this.list = list;
    }
     
}

TestMapList.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.javacodegeeks.snippets.enterprise;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
@Component("testMapListBean")
public class TestMapList {
     
    @Value("#{testBean.map['key1']}")
    private String mapElement;
  
    @Value("#{testBean.list[0]}")
    private String listElement;
   
    public String getMapElement() {
        return mapElement;
    }
 
    public void setMapElement(String mapElement) {
        this.mapElement = mapElement;
    }
 
    public String getListElement() {
        return listElement;
    }
 
    public void setListElement(String listElement) {
        this.listElement = listElement;
    }
 
    @Override
    public String toString() {
        return "TestMapList [mapElement=" + mapElement + ", listElement=" + listElement + "]";
    }
     
}

Whereas, applicationContext3.xml is now:

applicationContext3.xm

7.3 Run the application

We load the beans in App3.java class.

App3.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
package com.javacodegeeks.snippets.enterprise;
 
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class App3 {
 
    public static void main(String[] args) {
     
            ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("applicationContext3.xml");
 
            TestMapList testMapList = (TestMapList) context.getBean("testMapListBean");
            System.out.println(testMapList.toString());
            context.close();
    }
}

The result is the one shown below:

TestMapList [mapElement=Value 1, listElement=list0]

8. Regular Expressions with Spring EL

In order to check how regular expressions work with SpEL, we are going to enrich our latest example with email check functionality. We are adding a new property email to Test.java class that will hold the email to be checked for validity. Additionaly we are creating a new class, TestRegEx.java that will hold the result of Spring EL regular expressions check results in its fields. For this example we are going to check the email property value of Test.java with a regular expression used for emails so as to examine whether it is a valid email address. We will also try to match a number to a regular expression used for digits to check if it is a digit.

Test.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.javacodegeeks.snippets.enterprise;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class Test {
 
    private Map<String, String> map;
    private List<String> list;
    private String email;
  
    public Test() {
        map = new HashMap<String, String>();
        map.put("key1", "Value 1");
        map.put("key2", "Value 2");
        map.put("key3", "Value 3");
  
        list = new ArrayList<String>();
        list.add("list0");
        list.add("list1");
        list.add("list2");
         
        email = "hello@javacodegeeks.com";
  
    }
 
    public Map<String, String> getMap() {
        return map;
    }
  
    public void setMap(Map<String, String> map) {
        this.map = map;
    }
  
    public List<String> getList() {
        return list;
    }
  
    public void setList(List<String> list) {
        this.list = list;
    }
 
    public String getEmail() {
        return email;
    }
 
    public void setEmail(String email) {
        this.email = email;
    }  
}

TestRegEx.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.javacodegeeks.snippets.enterprise;
 
 
public class TestRegEx {
     
    private String regEx;
     
 
    private String regExResult;
 
    private String numberResult;
  
    public String getRegEx() {
        return regEx;
    }
 
    public void setRegEx(String regEx) {
        this.regEx = regEx;
    }
     
    public String getRegExResult() {
        return regExResult;
    }
 
    public void setRegExResult(String regExResult) {
        this.regExResult = regExResult;
    }
 
    public String getNumberResult() {
        return numberResult;
    }
 
    public void setNumberResult(String numberResult) {
        this.numberResult = numberResult;
    }
 
    @Override
    public String toString() {
        return "TestRegex :  \n Does testBean.email match the ^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$ "
    + regExResult + "\n Is 100 a valid number ? " + numberResult;
                 
    }
 
}

8.1 XML-based configuration

In applicationContext3.xml we add the new bean.

applicationContext3.xml

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
 
    <bean id="testBean" class="com.javacodegeeks.snippets.enterprise.Test" />
     
    <bean id="testMapListBean" class="com.javacodegeeks.snippets.enterprise.TestMapList">
        <property name="mapElement" value="#{testBean.map['key1']}" />
        <property name="listElement" value="#{testBean.list[0]}" />
    </bean>
 
    <bean id="testRegExBean" class="com.javacodegeeks.snippets.enterprise.TestRegEx">
        <property name="regEx" value="^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})
 
<pre wp-pre-tag-41=""></pre><p>quot; /><br>
<property name="regExResult" value="#{(testBean.email matches '^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})</p><h3>8.2 Annotation-based configuration</h3><p>Now, let's see what happens when we use annotations:</p><p><span style="text-decoration: underline;"><em>Test.java</em></span></p><pre class="brush:java">package com.javacodegeeks.snippets.enterprise;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.springframework.stereotype.Component;
 
@Component("testBean")
public class Test {
 
    private Map<String, String> map;
    private List<String> list;
    private String email;
  
    public Test() {
        map = new HashMap<String, String>();
        map.put("key1", "Value 1");
        map.put("key2", "Value 2");
        map.put("key3", "Value 3");
  
        list = new ArrayList<String>();
        list.add("list0");
        list.add("list1");
        list.add("list2");
         
        email = "hello@javacodegeeks.com";
  
    }
 
    public Map<String, String> getMap() {
        return map;
    }
  
    public void setMap(Map<String, String> map) {
        this.map = map;
    }
  
    public List<String> getList() {
        return list;
    }
  
    public void setList(List<String> list) {
        this.list = list;
    }
 
    public String getEmail() {
        return email;
    }
 
    public void setEmail(String email) {
        this.email = email;
    }  
}
</pre><p><span style="text-decoration: underline;"><em>TestRegEx.java</em></span></p><pre class="brush:java">package com.javacodegeeks.snippets.enterprise;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
@Component("testRegExBean")
public class TestRegEx {
     
    @Value("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})
 
<pre wp-pre-tag-43=""></pre><p>quot;)<br>
private String regEx;</p><p>@Value("#{(testBean.email matches testRegExBean.regEx)== true ? '-Yes there is a match.' : '-No there is no match.' }")<br>
private String regExResult;</p><p>@Value("#{ ('100' matches '\\d+') == true ? '-Yes this is digit.' : '-No this is not a digit.' }")<br>
private String numberResult;</p><p>public String getRegEx() {<br>
return regEx;<br>
}</p><p>public void setRegEx(String regEx) {<br>
this.regEx = regEx;<br>
}</p><p>public String getRegExResult() {<br>
return regExResult;<br>
}</p><p>public void setRegExResult(String regExResult) {<br>
this.regExResult = regExResult;<br>
}</p><p>public String getNumberResult() {<br>
return numberResult;<br>
}</p><p>public void setNumberResult(String numberResult) {<br>
this.numberResult = numberResult;<br>
}</p><p>@Override<br>
public String toString() {<br>
return "TestRegex :  \n Does testBean.email match the ^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$ "<br>
+ regExResult + "\n Is 100 a valid number ? " + numberResult;</p><p>}</p><p>}<br>
<span style="text-decoration: underline;"><em>applicationContext3.xml</em></span></p><pre class="brush:xml"><beans xmlns="http://www.springframework.org/schema/beans"
 
     
<context:component-scan base-package="com.javacodegeeks.snippets.enterprise" />
     
</beans>
</pre><h3>8.3 Run the application</h3><p>Now, let's use <code>App3.java</code> class again:</p><p><span style="text-decoration: underline;"><em>App3.java</em></span></p><pre class="brush:java">package com.javacodegeeks.snippets.enterprise;
 
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class App3 {
 
    public static void main(String[] args) {
     
            ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("applicationContext3.xml");
 
            TestMapList testMapList = (TestMapList) context.getBean("testMapListBean");
            System.out.println(testMapList.toString());
             
            TestRegEx testRegEx = (TestRegEx) context.getBean("testRegExBean");
            System.out.println(testRegEx.toString());
             
            context.close();
    }
}
</pre><pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code sty="" le="color: black; word-wrap: normal;">TestMapList [mapElement=Value 1, listElement=list0]
TestRegex : 
 Does testBean.email match the ^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$ -Yes there is a match.
 Is 100 a valid number ? -Yes this is digit.</code></pre><h2>9. ExpressionParser with Spring EL</h2><p>In order to use the <code>ExpressionParser</code> provided by the Spring API to evaluate exressions in Spring, we are creating <code>ExpressionParserApp.java</code> class. We are creating a new SpelExpressionParser and with its <code>parseExpression(String arg0)</code> API method we parse the expression string and return an <code>Expression</code> object that will be evaluated. We are using a literal expression, a method invocation, and we are also creating a new <code>Test</code> object and evaluate its email field.</p><p><span style="text-decoration: underline;"><em>ExpressionParserApp.java</em></span></p><pre class="brush:java">package com.javacodegeeks.snippets.enterprise;
 
 
    import org.springframework.expression.Expression;
    import org.springframework.expression.ExpressionParser;
    import org.springframework.expression.spel.standard.SpelExpressionParser;
    import org.springframework.expression.spel.support.StandardEvaluationContext;
      
    public class ExpressionParserApp {
        public static void main(String[] args) {
      
            ExpressionParser parser = new SpelExpressionParser();
      
            //literal expressions
            Expression exp = parser.parseExpression("'Hello World'");
            String msg1 = exp.getValue(String.class);
            System.out.println(msg1);
      
            //method invocation
            Expression exp2 = parser.parseExpression("'Hello World'.length()"); 
            int msg2 = (Integer) exp2.getValue();
            System.out.println(msg2);
      
            //Mathematical operators
            Expression exp3 = parser.parseExpression("100 * 2"); 
            int msg3 = (Integer) exp3.getValue();
            System.out.println(msg3);
      
            //create an test object
            Test test = new Test();
            //test EL with test object
            StandardEvaluationContext testContext = new StandardEvaluationContext(test);
      
            //display the value of test.email property
            Expression exp4 = parser.parseExpression("email");
            String msg4 = exp4.getValue(testContext, String.class);
            System.out.println(msg4);
      
            //test if test.email == 'Hello@javacodegeeks.com'
            Expression exp5 = parser.parseExpression("email == 'Hello@javacodegeeks.com'");
            boolean msg5 = exp5.getValue(testContext, Boolean.class);
            System.out.println(msg5);
    }
}
</pre><h3>9.1 Run the application</h3><p>After running <code>ExpressionParserApp.java</code> the result is like the one below:</p><pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code sty="" le="color: black; word-wrap: normal;">Hello World
11
200
hello@javacodegeeks.com
false
</code></pre><p> <br>
This was an example of Expression Language in Spring 3.<br>
 </p><p>Download the Eclipse project of this tutorial : <a href="https://examples.javacodegeeks.com/wp-content/uploads/2013/08/SpringExpressionLanguageExample.zip">SpringExpressionLanguageExample.zip</a><a title="" href="http://cdn.javacodegeeks.com/wp-content/uploads/2013/08/SpringExpressionLanguageExample.zip" target="_blank" rel="external nofollow"></a></p><p>)== true ? '-Yes there is a match.' : '-No there is no match.' }" /><br>
<property name="numberResult" value="#{ ('100' matches '\d+') == true ? '-Yes this is digit.' : '-No this is not a digit.' }" /><br>
</bean></p><p></beans></p><h3>8.2 Annotation-based configuration</h3><p>Now, let's see what happens when we use annotations:</p><p><span style="text-decoration: underline;"><em>Test.java</em></span></p><pre wp-pre-tag-42=""></pre><p><span style="text-decoration: underline;"><em>TestRegEx.java</em></span></p><pre wp-pre-tag-43=""></pre><p><span style="text-decoration: underline;"><em>applicationContext3.xml</em></span></p><pre wp-pre-tag-44=""></pre><h3>8.3 Run the application</h3><p>Now, let's use <code>App3.java</code> class again:</p><p><span style="text-decoration: underline;"><em>App3.java</em></span></p><pre wp-pre-tag-45=""></pre><pre wp-pre-tag-46=""></pre><h2>9. ExpressionParser with Spring EL</h2><p>In order to use the <code>ExpressionParser</code> provided by the Spring API to evaluate exressions in Spring, we are creating <code>ExpressionParserApp.java</code> class. We are creating a new SpelExpressionParser and with its <code>parseExpression(String arg0)</code> API method we parse the expression string and return an <code>Expression</code> object that will be evaluated. We are using a literal expression, a method invocation, and we are also creating a new <code>Test</code> object and evaluate its email field.</p><p><span style="text-decoration: underline;"><em>ExpressionParserApp.java</em></span></p><pre wp-pre-tag-47=""></pre><h3>9.1 Run the application</h3><p>After running <code>ExpressionParserApp.java</code> the result is like the one below:</p><pre wp-pre-tag-48=""></pre><p> <br>
This was an example of Expression Language in Spring 3.<br>
 </p><p>Download the Eclipse project of this tutorial : <a href="https://examples.javacodegeeks.com/wp-content/uploads/2013/08/SpringExpressionLanguageExample.zip">SpringExpressionLanguageExample.zip</a><a title="" href="http://cdn.javacodegeeks.com/wp-content/uploads/2013/08/SpringExpressionLanguageExample.zip" target="_blank" rel="external nofollow"></a></p><style>.lepopup-progress-149 div.lepopup-progress-t1>div{background-color:#e0e0e0;}.lepopup-progress-149 div.lepopup-progress-t1>div>div{background-color:#bd4070;}.lepopup-progress-149 div.lepopup-progress-t1>div>div{color:#ffffff;}.lepopup-progress-149 div.lepopup-progress-t1>label{color:#444444;}.lepopup-form-149, .lepopup-form-149 *, .lepopup-progress-149 {font-size:15px;color:#444444;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form-149 .lepopup-element div.lepopup-input div.lepopup-signature-box span i{font-family:'Arial','arial';font-size:13px;color:#555555;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form-149 .lepopup-element div.lepopup-input div.lepopup-signature-box,.lepopup-form-149 .lepopup-element div.lepopup-input div.lepopup-multiselect,.lepopup-form-149 .lepopup-element div.lepopup-input input[type='text'],.lepopup-form-149 .lepopup-element div.lepopup-input input[type='email'],.lepopup-form-149 .lepopup-element div.lepopup-input input[type='password'],.lepopup-form-149 .lepopup-element div.lepopup-input select,.lepopup-form-149 .lepopup-element div.lepopup-input select option,.lepopup-form-149 .lepopup-element div.lepopup-input textarea{font-family:'Arial','arial';font-size:13px;color:#555555;font-style:normal;text-decoration:none;text-align:left;background-color:rgba(255, 255, 255, 0.7);background-image:none;border-width:1px;border-style:solid;border-color:#cccccc;border-radius:0px;box-shadow: inset 0px 0px 15px -7px #000000;}.lepopup-form-149 .lepopup-element div.lepopup-input ::placeholder{color:#555555; opacity: 0.9;} .lepopup-form-149 .lepopup-element div.lepopup-input ::-ms-input-placeholder{color:#555555; opacity: 0.9;}.lepopup-form-149 .lepopup-element div.lepopup-input div.lepopup-multiselect::-webkit-scrollbar-thumb{background-color:#cccccc;}.lepopup-form-149 .lepopup-element div.lepopup-input>i.lepopup-icon-left, .lepopup-form-149 .lepopup-element div.lepopup-input>i.lepopup-icon-right{font-size:20px;color:#444444;border-radius:0px;}.lepopup-form-149 .lepopup-element .lepopup-button,.lepopup-form-149 .lepopup-element .lepopup-button:visited{font-family:'Arial','arial';font-size:13px;color:#ffffff;font-weight:700;font-style:normal;text-decoration:none;text-align:center;background-color:#326693;background-image:none;border-width:1px;border-style:solid;border-color:#326693;border-radius:0px;box-shadow:none;}.lepopup-form-149 .lepopup-element div.lepopup-input .lepopup-imageselect+label{border-width:1px;border-style:solid;border-color:#cccccc;border-radius:0px;box-shadow:none;}.lepopup-form-149 .lepopup-element div.lepopup-input .lepopup-imageselect+label span.lepopup-imageselect-label{font-size:15px;color:#444444;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form-149 .lepopup-element div.lepopup-input input[type='checkbox'].lepopup-checkbox-tgl:checked+label:after{background-color:rgba(255, 255, 255, 0.7);}.lepopup-form-149 .lepopup-element div.lepopup-input input[type='checkbox'].lepopup-checkbox-classic+label,.lepopup-form-149 .lepopup-element div.lepopup-input input[type='checkbox'].lepopup-checkbox-fa-check+label,.lepopup-form-149 .lepopup-element div.lepopup-input input[type='checkbox'].lepopup-checkbox-square+label,.lepopup-form-149 .lepopup-element div.lepopup-input input[type='checkbox'].lepopup-checkbox-tgl+label{background-color:rgba(255, 255, 255, 0.7);border-color:#cccccc;color:#555555;}.lepopup-form-149 .lepopup-element div.lepopup-input input[type='checkbox'].lepopup-checkbox-square:checked+label:after{background-color:#555555;}.lepopup-form-149 .lepopup-element div.lepopup-input input[type='checkbox'].lepopup-checkbox-tgl:checked+label,.lepopup-form-149 .lepopup-element div.lepopup-input input[type='checkbox'].lepopup-checkbox-tgl+label:after{background-color:#555555;}.lepopup-form-149 .lepopup-element div.lepopup-input input[type='radio'].lepopup-radio-classic+label,.lepopup-form-149 .lepopup-element div.lepopup-input input[type='radio'].lepopup-radio-fa-check+label,.lepopup-form-149 .lepopup-element div.lepopup-input input[type='radio'].lepopup-radio-dot+label{background-color:rgba(255, 255, 255, 0.7);border-color:#cccccc;color:#555555;}.lepopup-form-149 .lepopup-element div.lepopup-input input[type='radio'].lepopup-radio-dot:checked+label:after{background-color:#555555;}.lepopup-form-149 .lepopup-element div.lepopup-input div.lepopup-multiselect>input[type='checkbox']+label:hover{background-color:#bd4070;color:#ffffff;}.lepopup-form-149 .lepopup-element div.lepopup-input div.lepopup-multiselect>input[type='checkbox']:checked+label{background-color:#a93a65;color:#ffffff;}.lepopup-form-149 .lepopup-element input[type='checkbox'].lepopup-tile+label, .lepopup-form-149 .lepopup-element input[type='radio'].lepopup-tile+label {font-size:15px;color:#444444;font-style:normal;text-decoration:none;text-align:center;background-color:#ffffff;background-image:none;border-width:1px;border-style:solid;border-color:#cccccc;border-radius:0px;box-shadow:none;}.lepopup-form-149 .lepopup-element-error{font-size:15px;color:#ffffff;font-style:normal;text-decoration:none;text-align:left;background-color:#d9534f;background-image:none;}.lepopup-form-149 .lepopup-element-2 {background-color:rgba(226, 236, 250, 1);background-image:none;border-width:1px;border-style:solid;border-color:rgba(216, 216, 216, 1);border-radius:3px;box-shadow: 1px 1px 15px -6px #d7e1eb;}.lepopup-form-149 .lepopup-element-3 * {font-family:'Arial','arial';font-size:26px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form-149 .lepopup-element-3 {font-family:'Arial','arial';font-size:26px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form-149 .lepopup-element-3 .lepopup-element-html-content {min-height:73px;}.lepopup-form-149 .lepopup-element-4 * {font-family:'Arial','arial';font-size:19px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form-149 .lepopup-element-4 {font-family:'Arial','arial';font-size:19px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form-149 .lepopup-element-4 .lepopup-element-html-content {min-height:23px;}.lepopup-form-149 .lepopup-element-5 * {font-family:'Arial','arial';font-size:15px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form-149 .lepopup-element-5 {font-family:'Arial','arial';font-size:15px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form-149 .lepopup-element-5 .lepopup-element-html-content {min-height:24px;}.lepopup-form-149 .lepopup-element-6 * {font-family:'Arial','arial';font-size:15px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form-149 .lepopup-element-6 {font-family:'Arial','arial';font-size:15px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form-149 .lepopup-element-6 .lepopup-element-html-content {min-height:18px;}.lepopup-form-149 .lepopup-element-7 * {font-family:'Arial','arial';font-size:15px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form-149 .lepopup-element-7 {font-family:'Arial','arial';font-size:15px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form-149 .lepopup-element-7 .lepopup-element-html-content {min-height:18px;}.lepopup-form-149 .lepopup-element-8 * {font-family:'Arial','arial';font-size:15px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form-149 .lepopup-element-8 {font-family:'Arial','arial';font-size:15px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form-149 .lepopup-element-8 .lepopup-element-html-content {min-height:18px;}.lepopup-form-149 .lepopup-element-9 * {font-family:'Arial','arial';font-size:15px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form-149 .lepopup-element-9 {font-family:'Arial','arial';font-size:15px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form-149 .lepopup-element-9 .lepopup-element-html-content {min-height:18px;}.lepopup-form-149 .lepopup-element-10 * {font-family:'Arial','arial';font-size:15px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form-149 .lepopup-element-10 {font-family:'Arial','arial';font-size:15px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form-149 .lepopup-element-10 .lepopup-element-html-content {min-height:18px;}.lepopup-form-149 .lepopup-element-11 * {font-family:'Arial','arial';font-size:15px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form-149 .lepopup-element-11 {font-family:'Arial','arial';font-size:15px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form-149 .lepopup-element-11 .lepopup-element-html-content {min-height:18px;}.lepopup-form-149 .lepopup-element-12 * {font-family:'Arial','arial';font-size:15px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form-149 .lepopup-element-12 {font-family:'Arial','arial';font-size:15px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form-149 .lepopup-element-12 .lepopup-element-html-content {min-height:18px;}.lepopup-form-149 .lepopup-element-13 * {font-family:'Arial','arial';font-size:15px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form-149 .lepopup-element-13 {font-family:'Arial','arial';font-size:15px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form-149 .lepopup-element-13 .lepopup-element-html-content {min-height:18px;}.lepopup-form-149 .lepopup-element-14 div.lepopup-input .lepopup-icon-left, .lepopup-form-149 .lepopup-element-14 div.lepopup-input .lepopup-icon-right {line-height:36px;}.lepopup-form-149 .lepopup-element-15 div.lepopup-input{height:auto;line-height:1;}.lepopup-form-149 .lepopup-element-16 * {font-family:'Arial','arial';font-size:14px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form-149 .lepopup-element-16 {font-family:'Arial','arial';font-size:14px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form-149 .lepopup-element-16 .lepopup-element-html-content {min-height:5px;}.lepopup-form-149 .lepopup-element-19 * {font-family:'Arial','arial';font-size:13px;color:#333333;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form-149 .lepopup-element-19 {font-family:'Arial','arial';font-size:13px;color:#333333;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form-149 .lepopup-element-19 .lepopup-element-html-content {min-height:363px;}.lepopup-form-149 .lepopup-element-0 * {font-size:15px;color:#ffffff;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form-149 .lepopup-element-0 {font-size:15px;color:#ffffff;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:#5cb85c;background-image:none;border-width:0px;border-style:solid;border-color:#ccc;border-radius:5px;box-shadow: 1px 1px 15px -6px #000000;padding-top:40px;padding-right:40px;padding-bottom:40px;padding-left:40px;}.lepopup-form-149 .lepopup-element-0 .lepopup-element-html-content {min-height:160px;}</style><div class="lepopup-inline" style="margin: 0 auto;"><div class="lepopup-form lepopup-form-149 lepopup-form-FLB5myXrG6EPB1Vz lepopup-form-icon-inside lepopup-form-position-middle-right" data-session="0" data-id="FLB5myXrG6EPB1Vz" data-form-id="149" data-slug="7lQM6oyWL5bTm5lw" data-title="Under the Post Inline" data-page="1" data-xd="off" data-width="820" data-height="430" data-position="middle-right" data-esc="off" data-enter="on" data-disable-scrollbar="off" style="display:none;width:820px;height:430px;" onclick="event.stopPropagation();"><div class="lepopup-form-inner" style="width:820px;height:430px;"><div class="lepopup-element lepopup-element-2 lepopup-element-rectangle" data-type="rectangle" data-top="0" data-left="0" data-animation-in="fadeIn" data-animation-out="fadeOut" style="animation-duration:0ms;animation-delay:0ms;z-index:501;top:0px;left:0px;width:820px;height:430px;"></div><div class="lepopup-element lepopup-element-3 lepopup-element-html" data-type="html" data-top="7" data-left="10" data-animation-in="bounceInDown" data-animation-out="fadeOut" style="animation-duration:0ms;animation-delay:0ms;z-index:502;top:7px;left:10px;width:797px;height:73px;"><div class="lepopup-element-html-content">Do you want to know how to develop your skillset to become a <span style="color: #CAB43D; text-shadow: 1px 1px #835D5D;">Java Rockstar?</span></div></div><div class="lepopup-element lepopup-element-4 lepopup-element-html" data-type="html" data-top="83" data-left="308" data-animation-in="bounceInDown" data-animation-out="fadeOut" style="animation-duration:0ms;animation-delay:0ms;z-index:503;top:83px;left:308px;width:473px;height:23px;"><div class="lepopup-element-html-content">Subscribe to our newsletter to start Rocking <span style="text-decoration: underline;">right now!</span></div></div><div class="lepopup-element lepopup-element-5 lepopup-element-html" data-type="html" data-top="107" data-left="308" data-animation-in="bounceInDown" data-animation-out="fadeOut" style="animation-duration:0ms;animation-delay:0ms;z-index:504;top:107px;left:308px;width:473px;height:24px;"><div class="lepopup-element-html-content">To get you started we give you our best selling eBooks for <span style="color:#e01404; text-shadow: 1px 1px #C99924; font-size: 15px;">FREE!</span></div></div><div class="lepopup-element lepopup-element-6 lepopup-element-html" data-type="html" data-top="136" data-left="308" data-animation-in="bounceInDown" data-animation-out="fadeOut" style="animation-duration:0ms;animation-delay:0ms;z-index:505;top:136px;left:308px;width:473px;height:18px;"><div class="lepopup-element-html-content"><span style="font-weight: bold;">1.</span> JPA Mini Book</div></div><div class="lepopup-element lepopup-element-7 lepopup-element-html" data-type="html" data-top="156" data-left="308" data-animation-in="bounceInDown" data-animation-out="fadeOut" style="animation-duration:0ms;animation-delay:0ms;z-index:506;top:156px;left:308px;width:473px;height:18px;"><div class="lepopup-element-html-content"><span style="font-weight: bold;">2.</span> JVM Troubleshooting Guide</div></div><div class="lepopup-element lepopup-element-8 lepopup-element-html" data-type="html" data-top="176" data-left="308" data-animation-in="bounceInDown" data-animation-out="fadeOut" style="animation-duration:0ms;animation-delay:0ms;z-index:507;top:176px;left:308px;width:473px;height:18px;"><div class="lepopup-element-html-content"><span style="font-weight: bold;">3.</span> JUnit Tutorial for Unit Testing</div></div><div class="lepopup-element lepopup-element-9 lepopup-element-html" data-type="html" data-top="196" data-left="308" data-animation-in="bounceInDown" data-animation-out="fadeOut" style="animation-duration:0ms;animation-delay:0ms;z-index:508;top:196px;left:308px;width:473px;height:18px;"><div class="lepopup-element-html-content"><span style="font-weight: bold;">4.</span> Java Annotations Tutorial</div></div><div class="lepopup-element lepopup-element-10 lepopup-element-html" data-type="html" data-top="216" data-left="308" data-animation-in="bounceInDown" data-animation-out="fadeOut" style="animation-duration:0ms;animation-delay:0ms;z-index:509;top:216px;left:308px;width:473px;height:18px;"><div class="lepopup-element-html-content"><span style="font-weight: bold;">5.</span> Java Interview Questions</div></div><div class="lepopup-element lepopup-element-11 lepopup-element-html" data-type="html" data-top="236" data-left="308" data-animation-in="bounceInDown" data-animation-out="fadeOut" style="animation-duration:0ms;animation-delay:0ms;z-index:510;top:236px;left:308px;width:473px;height:18px;"><div class="lepopup-element-html-content"><span style="font-weight: bold;">6.</span> Spring Interview Questions</div></div><div class="lepopup-element lepopup-element-12 lepopup-element-html" data-type="html" data-top="256" data-left="308" data-animation-in="bounceInDown" data-animation-out="fadeOut" style="animation-duration:0ms;animation-delay:0ms;z-index:511;top:256px;left:308px;width:473px;height:18px;"><div class="lepopup-element-html-content"><span style="font-weight: bold;">7.</span> Android UI Design</div></div><div class="lepopup-element lepopup-element-13 lepopup-element-html" data-type="html" data-top="282" data-left="308" data-animation-in="bounceInDown" data-animation-out="fadeOut" style="animation-duration:0ms;animation-delay:0ms;z-index:512;top:282px;left:308px;width:473px;height:18px;"><div class="lepopup-element-html-content">and many more ....</div></div><div class="lepopup-element lepopup-element-14" data-type="email" data-deps="" data-id="14" data-top="305" data-left="308" data-animation-in="fadeIn" data-animation-out="fadeOut" style="animation-duration:0ms;animation-delay:0ms;z-index:513;top:305px;left:308px;width:473px;height:36px;"><div class="lepopup-input"><input type="email" name="lepopup-14" class="lepopup-ta-left " placeholder="Enter your e-mail..." autocomplete="email" data-default="" value="" aria-label="Email Field" oninput="lepopup_input_changed(this);" onfocus="lepopup_input_error_hide(this);"></div></div><div class="lepopup-element lepopup-element-15" data-type="checkbox" data-deps="" data-id="15" data-top="344" data-left="308" data-animation-in="fadeIn" data-animation-out="fadeOut" style="animation-duration:0ms;animation-delay:0ms;z-index:514;top:344px;left:308px;width:160px;"><div class="lepopup-input lepopup-cr-layout-1 lepopup-cr-layout-left"><div class="lepopup-cr-container lepopup-cr-container-medium lepopup-cr-container-left"><div class="lepopup-cr-box"><input class="lepopup-checkbox lepopup-checkbox-classic lepopup-checkbox-medium" type="checkbox" name="lepopup-15[]" id="lepopup-checkbox-IttIlnNNgKxaXx6H-14-0" value="on" data-default="off" onchange="lepopup_input_changed(this);"><label for="lepopup-checkbox-IttIlnNNgKxaXx6H-14-0" onclick="lepopup_input_error_hide(this);"></label></div><div class="lepopup-cr-label lepopup-ta-left"><label for="lepopup-checkbox-IttIlnNNgKxaXx6H-14-0" onclick="lepopup_input_error_hide(this);"></label></div></div></div></div><div class="lepopup-element lepopup-element-16 lepopup-element-html" data-type="html" data-top="344" data-left="338" data-animation-in="fadeIn" data-animation-out="fadeOut" style="animation-duration:0ms;animation-delay:0ms;z-index:515;top:344px;left:338px;width:350px;height:5px;"><div class="lepopup-element-html-content">I agree to the <a href="https://www.javacodegeeks.com/about/terms-of-use" target="_blank">Terms </a> and <a href="https://www.javacodegeeks.com/about/privacy-policy" target="_blank">Privacy Policy</a></div></div><div class="lepopup-element lepopup-element-17" data-type="button" data-top="372" data-left="308" data-animation-in="bounceIn" data-animation-out="fadeOut" style="animation-duration:0ms;animation-delay:0ms;z-index:516;top:372px;left:308px;width:85px;height:37px;"><a class="lepopup-button lepopup-button-zoom-out " href="#" onclick="return lepopup_submit(this);" data-label="Sign up" data-loading="Loading..."><span>Sign up</span></a></div><div class="lepopup-element lepopup-element-19 lepopup-element-html" data-type="html" data-top="67" data-left="-15" data-animation-in="fadeIn" data-animation-out="fadeOut" style="animation-duration:0ms;animation-delay:0ms;z-index:518;top:67px;left:-15px;width:320px;height:363px;"><div class="lepopup-element-html-content"><img data-lazyloaded="1" src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIzMjAiIGhlaWdodD0iMzYzIiB2aWV3Qm94PSIwIDAgMzIwIDM2MyI+PHJlY3Qgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgZmlsbD0idHJhbnNwYXJlbnQiLz48L3N2Zz4=" decoding="async" data-src="https://examples.javacodegeeks.com/wp-content/uploads/2015/01/books_promo.png.webp" alt="" width="320" height="363"><noscript><img decoding="async" src="https://examples.javacodegeeks.com/wp-content/uploads/2015/01/books_promo.png.webp" alt="" width="320" height="363"/></noscript></div></div></div></div><div class="lepopup-form lepopup-form-149 lepopup-form-FLB5myXrG6EPB1Vz lepopup-form-icon-inside lepopup-form-position-middle-right" data-session="0" data-id="FLB5myXrG6EPB1Vz" data-form-id="149" data-slug="7lQM6oyWL5bTm5lw" data-title="Under the Post Inline" data-page="confirmation" data-xd="off" data-width="420" data-height="320" data-position="middle-right" data-esc="off" data-enter="on" data-disable-scrollbar="off" style="display:none;width:420px;height:320px;" onclick="event.stopPropagation();"><div class="lepopup-form-inner" style="width:420px;height:320px;"><div class="lepopup-element lepopup-element-0 lepopup-element-html" data-type="html" data-top="80" data-left="70" data-animation-in="bounceInDown" data-animation-out="fadeOutUp" style="animation-duration:1000ms;animation-delay:0ms;z-index:500;top:80px;left:70px;width:280px;height:160px;"><div class="lepopup-element-html-content"><h4 style="text-align: center; font-size: 18px; font-weight: bold;">Thank you!</h4><p style="text-align: center;">We will contact you soon.</p></div></div></div></div><input type="hidden" id="lepopup-logic-FLB5myXrG6EPB1Vz" value="[]"></div><div class="post-bottom-meta post-bottom-tags post-tags-classic"><div class="post-bottom-meta-title"><span class="tie-icon-tags" aria-hidden="true"></span> Tags</div><span class="tagcloud"><a href="https://examples.javacodegeeks.com/tag/regex/" rel="tag">regex</a></span></div></pre>

Theodora Fragkouli

Theodora has graduated from Computer Engineering and Informatics Department in the University of Patras. She also holds a Master degree in Economics from the National and Technical University of Athens. During her studies she has been involved with a large number of projects ranging from programming and software engineering to telecommunications, hardware design and analysis. She works as a junior Software Engineer in the telecommunications sector where she is mainly involved with projects based on Java and Big Data technologies.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
ooxaam
ooxaam
6 years ago

Can we iterate over a list using Spel?

Fateh
Fateh
6 years ago
Reply to  ooxaam

Hi
Yes you can iterate over the List using spEL

public static void main(String[] args) {
List myList = new ArrayList(Arrays.asList(0, 1, 2, 3, 4, 5));

ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext(myList);

context.setVariable(“myList”, myList);

List newList = parser.parseExpression(“#myList”).getValue(context, List.class);

for (int index : newList){
System.out.println(“value is “+ newList.get(index));
}

}

Back to top button