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]
Advertisements