- Spring SpEL - Home
- Spring SpEL - Overview
- Spring SpEL - Environment Setup
- Spring SpEL - Create Project
- Spring SpEL - Literal Expression
- Spring SpEL - Properties
- Spring SpEL - Array
- Spring SpEL - List
- Spring SpEL - Map
- Spring SpEL - Methods
- Spring SpEL - Relational Operators
- Spring SpEL - Logical Operators
- Spring SpEL - Mathematical Operators
- Spring SpEL - Assignment Operator
- Spring SpEL - Constructor
- Spring SpEL - Variables
- Spring SpEL - Functions
- Spring SpEL - Expression Templating
Spring SpEL Expression Evaluation
Spring SpEL Bean Configuration
Spring SpEL Language Reference
Spring SpEL Operators
Spring SpEL Special Operators
Spring SpEL Collections
Spring SpEL Other Features
Spring SpEL - Useful Resources
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 - Usage of SpEL in XML based configuration
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.
RandomNumberGenerator.java
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;
}
}
Employee.java
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 + "]";
}
}
MainApp.java
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);
}
}
applicationcontext.xml
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
Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message −
[52, Mahesh, IN]