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
广告
© . All rights reserved.