English 中文(简体)
Spring SpEL - XML Configuration
  • 时间:2024-09-17

Spring SpEL - XML Based Configuration


Previous Page Next Page  

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