
Spring SpEL Examples
Spring Expression Language (SpEL) is a powerful expression language that is widely used in Spring applications for configuring beans, accessing bean properties, and performing various operations. SpEL provides a concise and consistent way to work with Spring beans and is used in many places within the Spring framework.
Here are some examples of how to use SpEL in Spring:
1. Accessing Bean Properties:
You can use SpEL to access properties of Spring beans. For example, if you have a bean named person with a property called name, you can access it like this:
<bean id="person" class="com.example.Person">
<property name="name" value="John" />
</bean>
<bean id="messageBean" class="com.example.MessageBean">
<property name="message" value="#{person.name} says hello!" />
</bean>
In this example, the message property of the messageBean is set using SpEL to access the name property of the person bean.
2. Arithmetic Expressions:
You can perform arithmetic operations using SpEL. For example:
<bean id="calculator" class="com.example.Calculator">
<property name="result" value="#{10 + 5}" />
</bean>
In this example, the result property of the calculator bean is set to the result of the arithmetic expression 10 + 5.
3. Conditional Expressions:
SpEL allows you to create conditional expressions. For example:
<bean id="person" class="com.example.Person">
<property name="adult" value="#{person.age ge 18}" />
</bean>
In this example, the adult property of the person bean is set to true if the age property is greater than or equal to 18.
4. Ternary Operator:
SpEL supports the ternary operator (condition ? trueValue : falseValue) for conditional assignments. For example:
<bean id="messageBean" class="com.example.MessageBean">
<property name="message" value="#{person.adult ? 'Adult' : 'Minor'}" />
</bean>
In this example, the message property of the messageBean is set based on whether the person is an adult or a minor.
5. Collection Operations:
You can use SpEL to manipulate collections. For instance, you can filter a list:
<util:list id="numbers" list-class="java.util.ArrayList">
<value>1</value>
<value>2</value>
<value>3</value>
<value>4</value>
<value>5</value>
</util:list>
<bean id="filteredNumbers" class="java.util.ArrayList">
<constructor-arg value="#{numbers.?[#this % 2 == 0]}" />
</bean>
In this example, the filteredNumbers bean is created by filtering the numbers list to include only even numbers using SpEL.
6. Method Invocations:
You can invoke methods using SpEL. For instance, invoking a method on a bean:
<bean id="calculator" class="com.example.Calculator">
<property name="result" value="#{calculator.add(10, 5)}" />
</bean>
In this example, the result property of the calculator bean is set by invoking the add method with arguments 10 and 5.
These are just a few examples of how you can use SpEL in Spring applications. SpEL is a versatile and expressive language that allows you to configure your Spring beans and perform various operations in a concise and powerful way.