Flat Preloader Icon

Spring Expression Language (SPEL)

  • The Spring Expression Language (SPEL) is a powerful and flexible expression language that is primarily used within the Spring Framework for various purposes such as configuration, annotation-based bean definitions, and query operations.
  • SPEL allows you to evaluate expressions at runtime, which makes it a valuable tool for dynamic configuration, data access, and manipulation within Spring applications.
Here are some key features and use cases of the Spring Expression Language (SPEL):

Property Access:

  • SPEL allows you to access bean properties directly using the ${} syntax. This is often used in Spring configuration files (e.g., application.properties) to externalize configuration values.
				
					db.url=jdbc:mysql://localhost:3306/mydb

				
			
				
					<bean id="dataSource"
class="org.springframework.jdbc.
datasource
.DriverManagerDataSource">
    <property name="url"
    value="${db.url}" />
</bean>

				
			

Bean References:

  • You can reference Spring beans by name using the @ symbol. This is useful for injecting beans into other beans.
				
					<bean id="userService" 
class="com.example
.UserService" />
<bean id="orderService"
class="com.example
.OrderService">
    <property name="userService"
    value="#{userService}" />
</bean>

				
			

Method Invocation:

  • SPEL supports method invocation on objects and beans. This can be useful for calling methods with arguments.
				
					<bean id="myService" 
class="com.example.MyService" />
<bean id="message" 
class="java.lang.String" 
factory-bean="myService" 
factory-method="getMessage()" />

				
			
SpEL is an exression language supporting the features of querying and manipulating an object graph at runtime. There are many expression languages available such as JSP EL, OGNL, MVEL and JBoss EL. SpEL provides some additional features such as method invocation and string templating functionality.

Conditional Expressions:

  • SPEL supports conditional expressions, allowing you to make decisions based on conditions.
				
					<bean id="exampleBean"
class="com.example.ExampleBean">
    <property name="enabled" 
    value="#{ systemProperties
    ['my.property'] eq 'true' }" />
</bean>
factory-method="getMessage()" />

				
			

Collection Manipulation:

  • You can manipulate collections, arrays, and maps using SPEL expressions.
				
					<util:list id="myList">
    <value>one</value>
    <value>two</value>
    <value>three</value>
</util:list>

<bean id="elementCount" 
class="java.lang.Integer">
    <constructor-arg 
    value="#{ myList.size() }" />
</bean>

				
			

Regular Expressions:

  • SPEL supports regular expressions for pattern matching.
				
					<bean id="exampleBean" 
class="com.example.ExampleBean">
    <property name="namePattern" 
    value="#{'\\d{3}-
    \\d{2}-\\d{4}'.matches('123-45-6789')}" />
</bean>

				
			

Ternary Operator:

  • SPEL includes the ternary operator (? 🙂 for conditional evaluation.
				
					<bean id="exampleBean" 
class="com.example.ExampleBean">
    <property name="result" 
    value="#{someCondition ?
    'yes' : 'no'}" />
</bean>

				
			

Type Conversion:

  • SPEL performs type conversion automatically when evaluating expressions.
				
					<bean id="exampleBean" 
class="com.example.ExampleBean">
    <property name="intValue" 
    value="#{ '100' }" />
</bean>

				
			

Access to Environment Properties:

  • SPEL allows access to environment properties, system properties, and Spring’s Environment abstraction.
				
					<bean id="exampleBean" 
class="com.example.ExampleBean">
    <property name="dbUrl" 
    value="#{ systemProperties
    ['db.url'] }" />
</bean>

				
			
  • SPEL is used extensively within Spring for tasks such as configuring beans, defining conditions for bean creation, and evaluating expressions in Spring Security for access control. It provides a powerful and flexible way to work with dynamic values and expressions in your Spring applications.