SSH笔记-Spring表达式语言:SpEL

本文介绍了Spring表达式语言(SpEL)的使用,它是一个强大的运行时查询和对象图操作语言。SpEL使用#{...}作为表达式定界符,并能方便地对bean属性进行动态赋值。它支持通过bean ID引用、方法调用、属性访问以及计算表达式等操作。文中还提及了注意事项和相关测试代码,包括TestSpel.java、SpelBean系列模型类以及spelContext.xml配置文件。
摘要由CSDN通过智能技术生成

1、Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言

2、语法类似于 EL:SpEL 使用 #{…} 作为定界符,所有在大框号中的字符都将被认为是 SpEL

3、SpEL 为 bean 的属性进行动态赋值提供了便利

4、通过 SpEL 可以实现:
①通过 bean 的 id 对 bean 进行引用
②调用方法以及引用对象中的属性
③计算表达式的值
④正则表达式的匹配

5、注意事项:详细注释或者需要注意的都写到spelContext.xml的注释中

6、文件
①TestSpel.java:测试类
②SpelBean.java:数据模型
③SpelBean2.java:数据模型
④SpelBean3.java:数据模型
⑤spelContext.xml:配置文件

7、TestSpel.java

package com.demo.sshtest.spel;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpel {
   

    public static void main(String[] args) {
        scope();
    }
    public static void scope(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spelContext.xml");
        SpelBean spelBean = (SpelBean)applicationContext.getBean("spelbean");
        System.out.println(spelBean);
    }
}

8、SpelBean.java

package com.demo.sshtest.spel;

public class SpelBean {
   
    private Integer id;
    private String name;
    private double number;
    private boolean judge;
    private SpelBean2 spelBean2;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值