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

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;
    }
    public double getNumber() {
        return number;
    }
    public void setNumber(double number) {
        this.number = number;
    }
    public boolean isJudge() {
        return judge;
    }
    public void setJudge(boolean judge) {
        this.judge = judge;
    }
    public SpelBean2 getSpelBean2() {
        return spelBean2;
    }
    public void setSpelBean2(SpelBean2 spelBean2) {
        this.spelBean2 = spelBean2;
    }
    @Override
    public String toString() {
        return "SpelBean [id=" + id + ", name=" + name + ", number=" + number + ", judge=" + judge + ", spelBean2="
                + spelBean2 + "]";
    }
}

9、SpelBean2.java

package com.demo.sshtest.spel;

public class SpelBean2 {
    private Integer id;
    private String name;
    //用于测试引用SpelBean3中的info属性
    private String in;
    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;
    }
    public String getIn() {
        return in;
    }
    public void setIn(String in) {
        this.in = in;
    }
    @Override
    public String toString() {
        return "SpelBean2 [id=" + id + ", name=" + name + ", in=" + in + "]";
    }
}

10、SpelBean3.java

package com.demo.sshtest.spel;

public class SpelBean3 {

    private String info;

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "SpelBean3 [info=" + info + "]";
    }

}

11、spelContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

    <!--
    SpEL表达式
    1、SpEL 使用 #{…} 作为定界符,所有在大框号中的字符都将被认为是 SpEL
    2、SpEL 为 bean 的属性进行动态赋值提供了便利
    3、表示方法:
        整数                           :#{1}
        小数                           :#{1.1}
        科学计数法               :#{1e4}
        字符串                       :#{'aaaa'}
        布尔型                       :#{false}  #{true}
        引用对象                   :#{对象bean}
        引用对象的属性       :#{对象bean.属性名}
        调用对象的方法       :#{对象bean.该类的方法}
        运算符                       :#{number+1} #{T(java.lang.Math).PI*8}
        字符串拼接               :#{'aaaa'+'bbbb'}
        比较运算符               :#{number==1}
        逻辑运算符               :#{number lt 10 and number==5}
        if-else运算符  :#{number==0?true:false}
        正则表达式               :#{result.match '[0-9]{1,2}'}
    -->

    <bean id="spelbean" class="com.demo.sshtest.spel.SpelBean">
        <!-- 使用SpEL为整型属性赋值 -->
        <property name="id" value="#{1}"></property>
        <!-- 使用SpEL为字符串属性赋值 要用单引号包着字符串内容-->
        <property name="name" value="#{'nameA'}"></property>
        <!-- 使用SpEL使用if-else方法判断,并为double型属性赋值-->
        <property name="number" value="#{((12.25==0)?(12.23):(13.33))}"></property>
        <!-- 使用SpEL为boolean型属性赋值 -->
        <property name="judge" value="#{true}"></property>
        <!-- 使用SpEL引用其他bean -->
        <property name="spelBean2" value="#{spelbean2}"></property>
    </bean>
    <bean id="spelbean2" class="com.demo.sshtest.spel.SpelBean2">
        <property name="id" value="#{2}"></property>
        <property name="name" value="#{'nameB'}"></property>
        <!-- 使用SpEL引用其他bean的属性 -->
        <property name="in" value="#{spelbean3.info}"></property>
    </bean>
    <bean id="spelbean3" class="com.demo.sshtest.spel.SpelBean3">
        <!-- 使用SpEL引用类的静态属性,通过T()调用一个类的静态方法 -->
        <property name="info" value="#{T(java.lang.Math).PI*8}"></property>
    </bean>
</beans>

12、项目目录
项目目录

13、demo
https://download.csdn.net/download/qq_22778717/10470362

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring表达式语言SpEL)是一种强大的表达式语言,用于在运行时计算表达式的值。SpEL可以用于配置文件、注解和代码中,支持对Bean的属性、方法和构造函数进行访问和操作,提供了很多常用的运算符和函数,是Spring框架中非常重要的一部分。 SpEL支持以下特性: 1. 引用Bean的属性和方法。可以使用“#”符号引用Bean的属性和方法,如“#user.name”表示引用名为user的Bean的name属性。 2. 调用静态方法和常量。可以使用“T()”关键字调用静态方法和常量,如“T(java.lang.Math).PI”表示引用Math类的PI常量。 3. 访问数组和集合。可以使用“[]”符号访问数组和集合,如“list[0]”表示访问名为list的集合的第一个元素。 4. 进行算术运算和比较运算。SpEL支持常见的算术运算和比较运算符,如“+”、“-”、“*”、“/”、“%”、“==”、“!=”、“<”、“>”等。 5. 定义变量和使用占位符。可以使用“#{}”定义变量和使用占位符,如“#{T(System).currentTimeMillis()}”表示定义一个名为currentTimeMillis的变量并赋值为当前时间的毫秒数。 6. 调用Bean的构造函数。可以使用“new”关键字调用Bean的构造函数,如“new java.util.Date()”表示调用java.util.Date类的无参构造函数。 SpEL可以在Spring的XML配置文件、@Value注解以及SpEL表达式注解中使用,可以方便地实现复杂的条件判断和动态计算。例如,可以使用SpEL表达式注解来实现@Scheduled注解的cron表达式的动态计算,或者在XML配置文件中使用SpEL表达式来实现Bean之间的依赖注入和条件配置等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值