Spring SpEL - 文字表達式



SpEL 表示式支援以下型別的文字 −

  • 字串 − 單引號分隔字串。使用單引號,在其周圍再加一個單引號。

  • 數字 − 支援 int、實際和十六進位制表示式。

  • 布林值

  • null

以下示例顯示各種用例。

示例

讓我們更新 Spring SpEL - 建立專案 章節中建立的專案。我們在新增/更新以下檔案 −

  • MainApp.java − 執行和測試的主應用程式。

以下是 MainApp.java 檔案的內容 −

package com.tutorialspoint;

import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;

public class MainApp {
   public static void main(String[] args) {
      ExpressionParser parser = new SpelExpressionParser();
      
      // parse a simple text
      String message = (String) parser.parseExpression("'Tutorialspoint'").getValue();
      System.out.println(message);

      // parse a double from exponential expression
      double avogadros  = (Double) parser.parseExpression("6.0221415E+23").getValue();
      System.out.println(avogadros);	

      // parse an int value from Hexadecimal expression
      int intValue = (Integer) parser.parseExpression("0x7FFFFFFF").getValue();
      System.out.println(intValue);

      // parse a boolean 
      boolean booleanValue = (Boolean) parser.parseExpression("true").getValue();
      System.out.println(booleanValue);

      // parse a null object
      Object nullValue = parser.parseExpression("null").getValue();
      System.out.println(nullValue);
   }
}

輸出

Tutorialspoint
6.0221415E23
2147483647
true
null
廣告