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

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

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

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

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

猜你喜欢

转载自blog.csdn.net/qq_22778717/article/details/80641734