Spring EL表达式的用法
用法一:表达式解析器
public class SpringEl {
public static void main(String[] args) {
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("'hello'.charAt(0)");
System.out.println(expression.getValue());
}
}
总结:可以解析一种以字符串形式表示的对象调用,其调用结果可以通过表达式的getValue()来得到
用法二:变量解析
public class SpringEl {
public static void main(String[] args) {
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("'hello'.charAt(0)");
System.out.println(expression.getValue());
Role role = new Role();
role.setRoleName("李龙");
role.setNote("李龙的描述");
EvaluationContext context = new StandardEvaluationContext(role);
parser.parseExpression("note").setValue(context,"new_note");
System.out.println(role.getNote());
}
}
总结:变量的名字要和对象中的属性相同才可以获取到或者进行设置,也可以进行运算