Spring表达式语言(SPEL)(01)

文章详细介绍了SpEL(SpringExpressionLanguage)在Java中的各种运算,包括算术、逻辑、比较、字符串操作、方法调用、构造函数、类类型引用、模板表达式、默认值设置以及安全的多级属性访问。测试用例展示了这些功能的实践应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

算术运算

@Test
public void test01() {
   
    // 定义解析器
    ExpressionParser parser = new SpelExpressionParser();
    // 使用解析器解析表达式
    Expression expression = parser.parseExpression("(1+2)*5 + 8-6/2");
    // 获取解析结果
    Assert.assertTrue(expression.getValue().equals(20));//加减乘除
    Assert.assertTrue(parser.parseExpression("8%3").getValue().equals(2));//求余
    Assert.assertTrue(parser.parseExpression("2.0e3").getValue().equals(2000.0));//指数
    Assert.assertTrue(parser.parseExpression("2^3").getValue().equals(8));//指数
}

逻辑运算

@Test
public void test02() {
   
	 ExpressionParser parser = new SpelExpressionParser();
	 Assert.assertTrue(parser.parseExpression("true and true").getValue(Boolean.class));//与
	 Assert.assertTrue(parser.parseExpression("true or false").getValue(Boolean.class));//与
	 Assert.assertTrue(parser.parseExpression("!false").getValue(Boolean.class));//非
}

比较运算

@Test
public void test03() {
   
	 ExpressionParser parser = new SpelExpressionParser();
	 Assert.assertTrue(parser.parseExpression("5>3").getValue(Boolean.class));
	 Assert.assertTrue(parser.parseExpression("5<=8").getValue(Boolean.class));
	 Assert.assertTrue(parser.parseExpression("5==5").getValue(Boolean.class));
	 Assert.assertTrue(parser.parseExpression("5!=6").getValue(Boolean.class));
}

使用字符代替符号

@Test
public void test19() {
   
    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值