- Spring SpEL - Discussion
- Spring SpEL - Useful Resources
- Spring SpEL - Quick Guide
- Spring SpEL - Expression Templating
- Spring SpEL - Functions
- Spring SpEL - Variables
- Spring SpEL - Constructor
- Spring SpEL - Collection Projection
- Spring SpEL - Collection Selection
- Spring SpEL - Safe Navigation Operator
- Spring SpEL - Elvis Operator
- Spring SpEL - Ternary Operator
- Spring SpEL - Assignment Operator
- Spring SpEL - Mathematical Operators
- Spring SpEL - Logical Operators
- Spring SpEL - Relational Operators
- Spring SpEL - Methods
- Spring SpEL - Map
- Spring SpEL - List
- Spring SpEL - Array
- Spring SpEL - Properties
- Spring SpEL - Literal Expression
- Spring SpEL - Annotation Configuration
- Spring SpEL - XML Configuration
- Spring SpEL - EvaluationContext
- Spring SpEL - Expression Interface
- Spring SpEL - Create Project
- Spring SpEL - Environment Setup
- Spring SpEL - Overview
- Spring SpEL - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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 quapfied. 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
chapter. We re adding/updating following files −RandomNumberGenerator.java − A random number generator class.
Employee.java − An employee class.
MainApp.java − Main apppcation to run and test.
apppcationcontext.xml − beans configuration file.
Here is the content of RandomNumberGenerator.java file −
package com.tutorialspoint; pubpc class RandomNumberGenerator { private int randomNumber; pubpc int getRandomNumber() { return randomNumber; } pubpc void setRandomNumber(int randomNumber) { this.randomNumber = randomNumber; } }
Here is the content of Employee.java file −
package com.tutorialspoint; pubpc class Employee { private int id; private String name; private String country; pubpc int getId() { return id; } pubpc void setId(int id) { this.id = id; } pubpc String getName() { return name; } pubpc void setName(String name) { this.name = name; } pubpc String getCountry() { return country; } pubpc void setCountry(String country) { this.country = country; } @Override pubpc String toString() { return "[" + id + ", " + name + ", " + country + "]"; } }
Here is the content of MainApp.java file −
package com.tutorialspoint; import org.springframework.context.ApppcationContext; import org.springframework.context.support.ClassPathXmlApppcationContext; pubpc class MainApp { pubpc static void main(String[] args) { ApppcationContext apppcationContext = new ClassPathXmlApppcationContext("apppcationcontext.xml"); Employee employee = (Employee) apppcationContext.getBean("employee"); System.out.println(employee); } }
Here is the content of apppcationcontext.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]Advertisements