spEL表达式使用

spEL表达式

SpEL(Spring Expression Language)是一种表达式语言,是一种可以与一个基于spring的应用程序中的运行时对象交互的东西;一种简化开发的表达式,通过使用表达式来简化开发,减少一些逻辑、配置的编写。

  • 用法

SpEL有三种用法,一种是在注解@Value中;一种是XML配置;最后一种是在代码块中使用Expression。

1、注解

@Value("#{表达式}")

public String name;

Spring容器中按照SpEL表达式筛选修改数据后,赋值给所修饰的变量

2、XML配置

<bean id="user" class="cn.spy.spel.method.User">

    <property name="username" value="#{user.getUsername()}"></property>

    <property name="password" value="#{user.getPassword()}"></property>

</bean>

与注解类似

3、代码中Expression

1、静态方法

    public static void main(String[] args) {

        //创建ExpressionParser解析表达式

        ExpressionParser parser = new SpelExpressionParser();

        //表达式放置

        Expression exp = parser.parseExpression("1 > 2");

        //执行表达式,默认容器是spring本身的容器:ApplicationContext

        Object value = exp.getValue();

        //输出false

        System.out.println(value);

    }

2、变量的表达式使用

    public static void main(String[] args) {

        ExpressionParser parser = new SpelExpressionParser();

        StandardEvaluationContext context =new StandardEvaluationContext();

        context.setVariable("message", " 1 > 2 ");

        String value =parser.parseExpression("#message").getValue(context, String.class);

        //输出 1 > 2

        System.out.println(value);

    }

3、root表达式的使用

    public static void main(String[] args) {

        ExpressionParser parser = new SpelExpressionParser();

        StandardEvaluationContext context =new StandardEvaluationContext();

        context.setRootObject("message");

        Object value1 =parser.parseExpression("#root.substring(0,1)").getValue(context);

        //输出m

        System.out.println(value1);

}

4、SpEL表达式中的运算符

类型

运算符

举例

关系

<,>,<=,>=,==,!=,lt,gt,le,ge,eq,ne

1>2

条件

?: (ternary),?: (elvis)

name?:"other"

逻辑

&&,||,!,and,or,not,between,instanceof

3 between {2,5}

算术

+,- ,* ,/,%,^

1+2

正则表达

matches

“123′ matches ‘\\d{3}”

其他类型

?.,?[…],![…],^[…],$[…]

list?.length

 

5、集合操作

 

A、定义:List<Integer> result1 = parser.parseExpression("{1,2,3}").getValue(List.class);
B、访问:parser.parseExpression("#collection[1]").getValue(context);  --list
        parser.parseExpression("#map['a']").getValue(context);	--map
C、修改:parser.parseExpression("#array[2]").setValue(context, 4);
D、赋值:parser.parseExpression("#array[1] = 3").getValue(context, int.class);
E、集合选择:语法:“(list|map).?[选择表达式]”
    eg:parser.parseExpression("#collection.?[#this>2]").getValue(context, Collection.class);

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring中,可以使用SpEL表达式绑定方法参数。具体步骤如下: 1. 在方法参数上添加@org.springframework.web.bind.annotation.RequestParam注解(或其他适当的注解),并指定SpEL表达式作为其属性值。 2. 在方法上添加@org.springframework.web.bind.annotation.GetMapping(或其他适当的注解),并设置其value属性来指定请求的URL。 3. 在应用程序上下文中配置一个DefaultSpELExpressionParser bean。 4. 在方法参数上添加@org.springframework.beans.factory.annotation.Value注解,并使用SpEL表达式引用应用程序上下文中的bean。 例如,以下代码展示了如何在Spring MVC中使用SpEL表达式绑定方法参数: ```java @RestController public class MyController { @GetMapping("/user/{id}") public User getUserById(@PathVariable("id") int id, @RequestParam("#{systemProperties['user.language']}") String language, @Value("#{myService.someProperty}") String someProperty) { // ... } } ``` 在上面的代码中,getUserById方法使用@PathVariable注解将URL路径中的{id}绑定到方法参数id上。然后,它使用@RequestParam注解将请求参数language绑定到方法参数language上,这里的SpEL表达式#{systemProperties['user.language']}将系统属性user.language的值作为参数传递给方法。最后,它使用@Value注解将应用程序上下文中名为myService的bean的someProperty属性绑定到方法参数someProperty上,这里的SpEL表达式#{myService.someProperty}将该属性的值作为参数传递给方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值