
- Spring SpEL 教程
- Spring SpEL - 首頁
- Spring SpEL - 概述
- Spring SpEL - 環境設定
- Spring SpEL - 建立專案
- 表示式求值
- Spring SpEL - Expression 介面
- Spring SpEL - EvaluationContext
- Bean 配置
- Spring SpEL - XML 配置
- Spring SpEL - 註解配置
- 語言參考
- Spring SpEL - 字面量表達式
- Spring SpEL - 屬性
- Spring SpEL - 陣列
- Spring SpEL - 列表
- Spring SpEL - Map
- Spring SpEL - 方法
- 運算子
- Spring SpEL - 關係運算符
- Spring SpEL - 邏輯運算子
- Spring SpEL - 數學運算子
- Spring SpEL - 賦值運算子
- 特殊運算子
- Spring SpEL - 三元運算子
- Spring SpEL - Elvis 運算子
- Spring SpEL - 安全導航運算子
- 集合
- Spring SpEL - 集合選擇
- Spring SpEL - 集合投影
- 其他特性
- Spring SpEL - 建構函式
- Spring SpEL - 變數
- Spring SpEL - 函式
- Spring SpEL - 表示式模板
- Spring SpEL - 有用資源
- Spring SpEL - 快速指南
- Spring SpEL - 有用資源
- Spring SpEL - 討論
Spring SpEL - 基於XML的配置
SpEL 表示式可以用於基於 XML 的 Bean 配置
語法
以下是在 xml 配置中使用表示式的示例。
<bean id="randomNumberGenerator" class="com.tutorialspoint.RandomNumberGenerator"> <property name="randomNumber" value="#{ T(java.lang.Math).random() * 100.0 }"/> </bean>
這裡我們指定了一個屬性,使用 `Math.random()` 方法填充。對於類,應該使用其全限定名。我們也可以使用系統變數,使用 `systemProperties`。它是一個內建變數。
<property name="country" value="#{ systemProperties['user.country'] }"/>
我們也可以使用另一個 Bean,以及一個 SpEL 表示式,如下所示
<property name="id" value="#{ randomNumberGenerator.randomNumber }"/>
下面的例子展示了各種用例。
示例
讓我們更新在 Spring SpEL - 建立專案章節中建立的專案。我們新增/更新以下檔案:
RandomNumberGenerator.java - 一個隨機數生成器類。
Employee.java - 一個員工類。
MainApp.java - 用於執行和測試的主應用程式。
applicationcontext.xml - bean 配置檔案。
以下是 RandomNumberGenerator.java 檔案的內容:
package com.tutorialspoint; public class RandomNumberGenerator { private int randomNumber; public int getRandomNumber() { return randomNumber; } public void setRandomNumber(int randomNumber) { this.randomNumber = randomNumber; } }
以下是 Employee.java 檔案的內容:
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 檔案的內容:
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 檔案的內容:
<?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>
輸出
[84, Mahesh, IN]
廣告