1:写在前面
spring在spring3版本后提供了el表达式的功能,表达式的计算,对象的访问,方法的调用等。对应的顶层接口是org.springframework.expression.ExpressionParser
。
参考如下文章列表:
https://www.cnblogs.com/ooo0/p/10987630.html
https://www.cnblogs.com/leiOOlei/p/3543222.html
2:字符串常量作为表达式
2.1:测试代码
public static void stringContantExp() {
// 定义表达式解析器对象
ExpressionParser expressionParser = new SpelExpressionParser();
// 设置字符串常量的表达式,'test spring el'就是我们定义的表达式,只不过这里是一个字符串的常量
Expression expression = expressionParser.parseExpression("'test spring el'");
// 获取表达式的值
String value = (String) expression.getValue();
System.out.println("表达式的值是:" + value);
// 设置调用字符串普通方法的EL表达式
expression = expressionParser.parseExpression("'test spring el'.charAt(0)");
System.out.println("通过表达式访问对象普通方法获取的值是:" + expression.getValue());
// 设置调用字符串get方法的EL表达式
expression = expressionParser.parseExpression("'test spring el'.getClass()");
System.out.println("通过表达式访问对象get方法获取的值是:" + expression.getValue());
// 通过EL访问属性
expression = expressionParser.parseExpression("'test spring el'.bytes.length");
System.out.println("通过表达式访问属性的值是:" + expression.getValue());
}
2.2:运行
表达式的值是:test spring el
通过表达式访问对象普通方法获取的值是:t
通过表达式访问对象get方法获取的值是:class java.lang.String
通过表达式访问属性的值是:14
3:操作对象的属性
3.1:测试代码
public class Role2{
//赋值long型
private Long id;
//字符串赋值
private String roleName;
//字符串赋值
private String note;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public Role2(long l, String role_name, String note) {
this.id = l;
this.roleName = role_name;
this.note = note;
}
@Override
public String toString() {
return JSON.toJSONString(this);
}
}
public static void testObjPropEl() {
/* 从根对象中读取属性开始 */
// 表达式解析器
ExpressionParser parser = new SpelExpressionParser();
// 定义根对象
Role2 role = new Role2(1L, "role_name", "note");
// 定义要访问的属性note的表达式
Expression exp = parser.parseExpression("note");
// 从根对象role中访问属性note
String note = (String) exp.getValue(role);
System.out.println(note);
/* 从根对象中读取属性结束 */
/* 设置根对象属性开始 */
// 使用role作为根对象创建计算上下文对象
EvaluationContext ctx = new StandardEvaluationContext(role);
// 设置根对象的note属性的值为new_note
parser.parseExpression("note").setValue(ctx, "new_note");
// 获取根对象的note属性的值,并指定以String类型返回
note = parser.parseExpression("note").getValue(ctx, String.class);
System.out.println(note);
/* 设置根对象属性结束 */
/* 调用根对象的getRoleName方法开始 */
// 调用getRoleName方法
String roleName = parser.parseExpression("getRoleName()").getValue(ctx, String.class);
System.out.println(roleName);
/* 调用根对象的getRoleName方法结束 */
}
3.2:运行
note
new_note
role_name
4:操作集合
4.1:测试代码
private static void testListEl() {
ExpressionParser parser = new SpelExpressionParser();
List<String> list = new ArrayList<>();
list.add("value-0");
list.add("value-1");
// 创建上下文,并将集合设置到上下文中
EvaluationContext ctx = new StandardEvaluationContext();
ctx.setVariable("list", list);
// 获取索引位置1的值
String pos1El = "#list[1]";
String value = parser.parseExpression(pos1El).getValue(ctx, String.class);
System.out.println("索引位置1的值是:" + value);
// 设置索引位置0的值为新值
String pos0El = "#list[0]";
parser.parseExpression(pos0El).setValue(ctx, "new-value-0");
value = parser.parseExpression(pos0El).getValue(ctx, String.class);
System.out.println("索引位置0的新值是:" + value);
}
4.2:运行
索引位置1的值是:value-1
索引位置0的新值是:new-value-0
5:基于EL表达式创建bean(xml)
5.1:类
public class Item {
private String name;
private int total;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
@Override
public String toString() {
return JSON.toJSONString(this);
}
}
public class Customer {
private Item item;
private String itemName;
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
@Override
public String toString() {
return JSON.toJSONString(this);
}
}
5.2:配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="itemBean" class="yudaosourcecode.speltest.Item">
<property name="name" value="itemA" />
<property name="total" value="10" />
</bean>
<bean id="customerBean" class="yudaosourcecode.speltest.Customer">
<property name="item" value="#{itemBean}" />
<property name="itemName" value="#{itemBean.name}" />
</bean>
</beans>
5.3:测试代码
@Test
publicvoid createbeanwitheltest() {
ClassPathXmlApplicationContext ac
= new ClassPathXmlApplicationContext("speltest/createbeanwitheltest.xml");
System.out.println(ac.getBean("customerBean"));
}
5.4:运行
{"item":{"name":"itemA","total":10},"itemName":"itemA"}
6:基于EL表达式创建bean(annotation)
6.1:类
@Component("itemBean")
public class Item {
@Value("itemA_byannotation")//直接注入String
private String name;
@Value("102")//直接注入integer
private int total;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
@Override
public String toString() {
return JSON.toJSONString(this);
}
}
@Component("customerBean")
public class Customer {
@Value("#{itemBean}")
private Item item;
@Value("#{itemBean.name}")
private String itemName;
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
@Override
public String toString() {
return JSON.toJSONString(this);
}
}
6.2:配置文件
<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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="yudaosourcecode.speltest.byannotation" />
</beans>
6.3:测试类
@Test
public void createbeanwithelannotationtest() {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("speltest/createbeanwithelannotationtest.xml");
System.out.println(ac.getBean("customerBean"));
}
6.4:运行
{"item":{"name":"itemA_byannotation","total":102},"itemName":"itemA_byannotation"}
7:基于方法调用创建bean(annotation)
7.1:类
@Component("customerBean")
public class Customer {
// 调用toUpperCase方法将lei转大写设置为值
@Value("#{'lei'.toUpperCase()}")
private String name;
// 调用名称为priceBean的getSpecialPrice方法作为amount的值
@Value("#{priceBean.getSpecialPrice()}")
private double amount;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
@Override
public String toString() {
return JSON.toJSONString(this);
}
}
@Component("priceBean")
public class Price {
public double getSpecialPrice() {
return new Double(99.99);
}
}
7.2:配置文件
<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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="yudaosourcecode.speltest.bymethodinvovationannotation" />
</beans>
7.3:测试代码
@Test
public void bymethodinvovationannotation() {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("speltest/bymethodinvovationannotation.xml");
System.out.println(ac.getBean("customerBean"));
}
7.4:运行
{"amount":99.99,"name":"LEI"}
8:基于方法调用创建bean(xml)
8.1:类
public class Customer {
private String name;
private double amount;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
@Override
public String toString() {
return JSON.toJSONString(this);
}
}
public class Price {
public double getSpecialPrice() {
return new Double(66.99);
}
}
8.2:配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="customerBean" class="yudaosourcecode.speltest.bymethodinvovationxml.Customer
">
<property name="name" value="#{'dongshidaddy'.toUpperCase()}" />
<property name="amount" value="#{priceBean.getSpecialPrice()}" />
</bean>
<bean id="priceBean" class="yudaosourcecode.speltest.bymethodinvovationxml.Price" />
</beans>
8.3:测试代码
@Test
public void bymethodinvovationxml() {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("speltest/bymethodinvovationxml.xml");
System.out.println(ac.getBean("customerBean"));
}
8.4:运行
{"amount":66.99,"name":"DONGSHIDADDY"}
9:运算符
9.1:类
@Component("customerBean")
public class MyOperatorBean {
@Value("#{1 == 1}") //true
private boolean testEqual;
@Value("#{1 != 1}") //false
private boolean testNotEqual;
@Value("#{numberBean.no == 999 and numberBean.no < 900}") //false
private boolean testAnd;
@Value("#{'1' + '@' + '1'}") //1@1
private String testAddString;
@Value("#{1 - 1}") //0.0
private double testSubtraction;
@Value("#{1 * 1}") //1.0
private double testMultiplication;
@Value("#{10 / 2}") //5.0
private double testDivision;
@Value("#{2 ^ 2}") //4.0
private double testExponentialPower;
// 三目
@Value("#{20 < 100 ? true : false}")
private boolean warning;
...snip getter and setter...
@Override
public String toString() {
return JSON.toJSONString(this);
}
}
@Component("numberBean")
public class Number {
@Value("999")
private int no;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
}
9.2:配置文件
<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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="yudaosourcecode.speltest.eloperatortest" />
</beans>
9.3:测试类
@Test
public void eloperatortest() {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("speltest/eloperatortest.xml");
System.out.println(ac.getBean("customerBean"));
}
9.4:运行
{"testAddString":"1@1","testAnd":false,"testDivision":5.0,"testEqual":true,"testExponentialPower":4.0,"testMultiplication":1.0,"testNotEqual":false,"testSubtraction":0.0,"warning":true}