3.spring的EL表达式

来源:互联网 发布:mrp软件下载 编辑:程序博客网 时间:2024/05/17 22:56
一、简介

Spring Expression Language,Spring表达式语言,简称SpEL。支持运行时查询并可以操作对象图。
和JSP页面上的EL表达式、Struts2中用到的OGNL表达式一样,SpEL根据JavaBean风格的getXxx()、setXxx()方法定义的属性访问对象图,完全符合我们熟悉的操作习惯。

二、  基本语法

SpEL使用#{…}作为定界符,所有在大框号中的字符都将被认为是SpEL表达式。

三、使用字面量

●整数:
●小数:
●科学计数法:
●String类型的字面量可以使用单引号或者双引号作为字符串的定界符号


●Boolean:

四、代码演示

package com.xiaofeng.bean;public class Employees {      private Integer id;      private Dept dept;      private String deptname;      private Double salary;      private String empname;      private String empname1;      private String methodvalue;      public String getMethodvalue() {        return methodvalue;    }    public void setMethodvalue(String methodvalue) {        this.methodvalue = methodvalue;    }    public String getEmpname1() {        return empname1;    }    public void setEmpname1(String empname1) {        this.empname1 = empname1;    }    private Double circle;    public Employees() {        super();        // TODO Auto-generated constructor stub    }    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public Dept getDept() {        return dept;    }    public void setDept(Dept dept) {        this.dept = dept;    }    public String getDeptname() {        return deptname;    }    public void setDeptname(String deptname) {        this.deptname = deptname;    }    public Double getSalary() {        return salary;    }    public void setSalary(Double salary) {        this.salary = salary;    }    public String getEmpname() {        return empname;    }    public void setEmpname(String empname) {        this.empname = empname;    }    public Double getCircle() {        return circle;    }    public void setCircle(Double circle) {        this.circle = circle;    }    @Override    public String toString() {        return "Employees [id=" + id + ", dept=" + dept + ", deptname="                + deptname + ", salary=" + salary + ", empname=" + empname                + ", empname1=" + empname1 + ", methodvalue=" + methodvalue                + ", circle=" + circle + "]";    }}
package com.xiaofeng.bean;public class Dept {      private String deptname;    public String getDeptname() {        return deptname;    }    public void setDeptname(String deptname) {        this.deptname = deptname;    }    public String getMessage(String str){        return "被调用非静态方法传值:=======》》" +str;    }    public Dept() {        super();        // TODO Auto-generated constructor stub    }    public String toString() {        return "Dept [deptname=" + deptname + "]";    }}
<?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"    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.0.xsd">      <!--spring EL表达式  -->      <bean id="dept" class="com.xiaofeng.bean.Dept">              <property name="deptname" value="研发部"></property>      </bean>      <bean id="emp" class="com.xiaofeng.bean.Employees">            <property name="id" value="#{1}"/>  <!--直接赋值integer类型,boolean,double等等也是一样  -->            <property name="dept" value="#{dept}"/><!--引用其他类型,ref是一个效果,id需要一样 -->            <property name="deptname" value="#{dept.deptname}"/> <!--引用其他bean的某个属性值  -->            <property name="empname" value='#{"张三"}'/>  <!--传入String类型,单引号,双引号的运用  -->             <property name="empname1" value="#{'张三'}"/>             <property name="salary" value="#{120.343*10}"/><!--加入普通运算  -->              <!--property name="salary" value="#{120.343}"/>-->             <!--调用静态方法  -->             <property name="circle" value="#{T(java.lang.Math).PI * 5}"/>             <!--调用非静态方法  -->             <!-- <property name="methodvalue" value="#{dept.getMessage('xml传值')}"/>-->             <property name="methodvalue" value="#{dept.getMessage(dept.deptname)}"/>      </bean></beans>
    ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");     @Test     public void test(){         Employees emp = (Employees) ioc.getBean("emp");         System.out.println(emp);     }
原创粉丝点击