spring学习笔记(8)——SpEL
来源:互联网 发布:怎么读源码 编辑:程序博客网 时间:2024/05/20 04:28
什么是SpEL
spel就是spring的表达式语言,看下图(该图来自尚硅谷)
SpEL很重要的一个功能就是能够实现 动态赋值
最简单的用法
以上的用法其实没有什么意义,并不能体现出SpEL的强大
SpEL使用
先写三个实体类
package com.zj.spel;public class Address { private String province; private String city; public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } @Override public String toString() { return "Address [province=" + province + ", city=" + city + "]"; }}
package com.zj.spel;public class Car { private String brand; private double price; private double tyrePerimeter;//轮胎周长 public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public double getTyrePerimeter() { return tyrePerimeter; } public void setTyrePerimeter(double tyrePerimeter) { this.tyrePerimeter = tyrePerimeter; } @Override public String toString() { return "Car [brand=" + brand + ", price=" + price + ", tyrePerimeter=" + tyrePerimeter + "]"; }}
package com.zj.spel;public class Person { private String name; private Car car; //引用Address的city属性 private String city; //根据car的价格:价格>300000,有钱人;价格<300000:普通人 private String info; public String getName() { return name; } public void setName(String name) { this.name = name; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } @Override public String toString() { return "Person [name=" + name + ", car=" + car + ", city=" + city + ", info=" + info + "]"; }}
配置文件中使用spel
<?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"> <!-- 使用spel赋字面值 --> <bean id="address" class="com.zj.spel.Address"> <property name="province" value="#{'福建'}"></property> <property name="city" value="#{'福州'}"></property> </bean> <!-- 使用spel引用类的静态属性 --> <bean id="car" class="com.zj.spel.Car"> <property name="brand" value="bmw"></property> <property name="price" value="350000"></property> <!-- 引用Math类的静态属性PI --> <property name="tyrePerimeter" value="#{T(java.lang.Math).PI * 80}"></property> </bean> <bean id="person" class="com.zj.spel.Person"> <property name="name" value="tom"></property> <!-- 使用spel引用其他的bean --> <property name="car" value="#{car}"></property> <!-- 使用spel引用其他bean的属性 --> <property name="city" value="#{address.city}"></property> <!-- 使用spel --> <property name="info" value="#{car.price > 300000 ? '有钱人' : '普通人'}"></property> </bean></beans>
写一个测试方法
public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-spel.xml"); Address address = (Address) ctx.getBean("address"); System.out.println(address); Car car = (Car) ctx.getBean("car"); System.out.println(car); Person person = (Person) ctx.getBean("person"); System.out.println(person); }
结果
用法总结
截图来自尚硅谷
0 0
- spring学习笔记(8)——SpEL
- Spring学习笔记(14)——SpEL
- spring学习笔记4——SpEL表达式
- Beginning Spring学习笔记——第9章 SpEL
- Spring学习笔记之SpEL
- spring SpEL表达式学习笔记
- Spring学习笔记----SpEL表达式
- Spring学习笔记之SpEL
- Spring学习笔记----SpEL表达式
- Spring学习笔记之--SpEL
- Spring学习笔记----SpEL表达式
- 13.笔记JAVA Spring框架学习————Bean使用SpEL
- Spring学习(5)--SpEL常见用法
- Spring(23)——SPEL表达式(一)
- Spring(23)——SPEL表达式(二)
- Spring(23)——SPEL表达式(三)
- Spring(23)——SPEL表达式(四)
- Spring学习笔记(六) Bean的作用域 & Spel 表达式
- 2.2常量与变量
- spring整合quartz
- js方法获取get方式传过来的参数
- string转int
- Android性能优化小集
- spring学习笔记(8)——SpEL
- HelixServer的搭建
- RedHat 6.4 安装WAS 7.0 启动失败com.ibm.websphere.ssl.SSLException
- Hibernate 连接Mycat例子详解
- HDU 1757 A Simple Math Problem (矩阵快速幂)
- QTP初学习---目录分析以及业务过程
- 从jsp上获值添加到数据库时出现乱码问题 怎么解决
- CSP开发基础--开发实例二
- 解决animator退到后台占用CPU问题