- Spring SpEL Tutorial
- Spring SpEL - Home
- Spring SpEL - Overview
- Spring SpEL - Environment Setup
- Spring SpEL - Create Project
- Expression Evaluation
- Spring SpEL - Expression Interface
- Spring SpEL - EvaluationContext
- Bean Configuration
- Spring SpEL - XML Configuration
- Spring SpEL - Annotation Configuration
- Language Reference
- Spring SpEL - Literal Expression
- Spring SpEL - Properties
- Spring SpEL - Array
- Spring SpEL - List
- Spring SpEL - Map
- Spring SpEL - Methods
- Operators
- Spring SpEL - Relational Operators
- Spring SpEL - Logical Operators
- Spring SpEL - Mathematical Operators
- Spring SpEL - Assignment Operator
- Special Operators
- Spring SpEL - Ternary Operator
- Spring SpEL - Elvis Operator
- Spring SpEL - Safe Navigation Operator
- Collections
- Spring SpEL - Collection Selection
- Spring SpEL - Collection Projection
- Other Features
- Spring SpEL - Constructor
- Spring SpEL - Variables
- Spring SpEL - Functions
- Spring SpEL - Expression Templating
- Spring SpEL - Useful Resources
- Spring SpEL - Quick Guide
- Spring SpEL - Useful Resources
- Spring SpEL - Discussion
Spring SpEL - XML Based Configuration
SpEL expression can be used in XML based beans configuration
Syntax
Following is an example of using an expression in xml configuration.
<bean id="randomNumberGenerator" class="com.tutorialspoint.RandomNumberGenerator"> <property name="randomNumber" value="#{ T(java.lang.Math).random() * 100.0 }"/> </bean>
Here we have specified a property to be filled in using Math.random() method. In case of classes, its name should be fully qualified. We can use system variables as well using systemProperties. It is a built-in variable.
<property name="country" value="#{ systemProperties['user.country'] }"/>
We can use another bean as well with a SpEL expression as shown below:
<property name="id" value="#{ randomNumberGenerator.randomNumber }"/>
Following example shows the various use cases.
Example
Let's update the project created in Spring SpEL - Create Project chapter. We're adding/updating following files −
RandomNumberGenerator.java − A random number generator class.
Employee.java − An employee class.
MainApp.java − Main application to run and test.
applicationcontext.xml − beans configuration file.
Here is the content of RandomNumberGenerator.java file −
package com.tutorialspoint; public class RandomNumberGenerator { private int randomNumber; public int getRandomNumber() { return randomNumber; } public void setRandomNumber(int randomNumber) { this.randomNumber = randomNumber; } }
Here is the content of Employee.java file −
package com.tutorialspoint; public class Employee { private int id; private String name; private String country; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } @Override public String toString() { return "[" + id + ", " + name + ", " + country + "]"; } }
Here is the content of MainApp.java file −
package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext.xml"); Employee employee = (Employee) applicationContext.getBean("employee"); System.out.println(employee); } }
Here is the content of applicationcontext.xml file −
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="randomNumberGenerator" class="com.tutorialspoint.RandomNumberGenerator"> <property name="randomNumber" value="#{ T(java.lang.Math).random() * 100.0 }"/> </bean> <bean id="employee" class="com.tutorialspoint.Employee"> <property name="id" value="#{ randomNumberGenerator.randomNumber }"/> <property name="country" value="#{ systemProperties['user.country'] }"/> <property name="name" value="Mahesh"/> </bean> </beans>
Output
[84, Mahesh, IN]