public class HelloWorld {
public static void main(String[] args) {
//构造上下文:准备比如变量定义等等表达式运行需要的上下文数据
EvaluationContext context = new StandardEvaluationContext();
//创建解析器:提供SpelExpressionParser默认实现
ExpressionParser parser = new SpelExpressionParser();
//解析表达式:使用ExpressionParser来解析表达式为相应的Expression对象
Expression expression =
parser.parseExpression("('Hello' + ' World').concat(#end)");
//设置上下文中的变量的值
context.setVariable("end", "!SpringEL");
//执行表达式,获取运行结果
String str = (String)expression.getValue(context);
System.out.println("the str="+str);
}
}
public static void main(String[] args) {
ExpressionParser parser = new SpelExpressionParser();
//自定义一个解析模板的规则
ParserContext parserContext = new ParserContext() {
public boolean isTemplate() { return true; }
public String getExpressionPrefix() { return "#{"; }
public String getExpressionSuffix() { return "}"; }
};
String template = "#{'Hello '}#{'World!'}";
Expression expression = parser.parseExpression(template, parserContext);
System.out.println( expression.getValue());
}
1:String str1 = parser.parseExpression("'Hello World!'").getValue(String.class);
2:String str2 = parser.parseExpression("\"Hello World!\"").getValue(String.class);
3:int int1 = parser.parseExpression("1").getValue(Integer.class);
4:float float1 = parser.parseExpression("1.1").getValue(Float.class);
5:boolean true1 = parser.parseExpression("true").getValue(boolean.class);
6:Object null1 = parser.parseExpression("null").getValue(Object.class);
ExpressionParser parser = new SpelExpressionParser();
EvaluationContext context = new StandardEvaluationContext();
context.setVariable("variable", "hello1");
context.setVariable("variable", "hello2");
String result1 = parser.parseExpression("#variable").getValue(context, String.class);
System.out.println("r1=="+result1);
context = new StandardEvaluationContext(12);
String result2 = parser.parseExpression("#root-1").getValue(context, String.class);
System.out.println("r2=="+result2);
String result3 = parser.parseExpression("#this").getValue(context, String.class);
System.out.println("r3=="+result3);
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
Method parseInt =
Integer.class.getDeclaredMethod("parseInt", String.class);
context.registerFunction("regParseInt", parseInt);
context.setVariable("parseInt2", parseInt);
String expression1 = "#regParseInt('3') == #parseInt2('3')";
boolean result =
parser.parseExpression(expression1).getValue(context, boolean.class);
System.out.println("result="+result);
1:parser.parseExpression("#root=‘Hi'").getValue(context, String.class);
2:parser.parseExpression("#this=‘Hi'").getValue(context, String.class);
3:context.setVariable("#variable", "variable");
UserModel um = new UserModel();
um.setUuid("User1");
um.setName("UserName1");
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("um",um);
//取值
Expression expression = parser.parseExpression("'uuid='+#um.uuid + ',name='+#um.name");
String v = expression.getValue(context,String.class);
System.out.println("v=="+v);
//赋值
expression = parser.parseExpression("'uuid='+(#um.uuid='newUser') + ',name='+#um.name");
v = expression.getValue(context,String.class);
System.out.println("v2=="+v);
输出结果:
v==uuid=User1,name=UserName1
v2==uuid=newUser,name=UserName1
ApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[] {"applicationContext.xml"});
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(new BeanFactoryResolver(ctx));
String result1 =
parser.parseExpression("@myBean.test()").getValue(context, String.class);
UserModel um1 = new UserModel();
um1.setUuid("u1");
um1.setName("u1Name");
UserModel um2 = new UserModel();
um2.setUuid("u2");
um2.setName("u2Name");
Map map = new HashMap();
map.put(um1.getUuid(),um1);
map.put(um2.getUuid(),um2);
//2.测试Map投影
EvaluationContext context1 = new StandardEvaluationContext();
ExpressionParser parser = new SpelExpressionParser();
context1.setVariable("map", map);
Collection result1 =
parser.parseExpression("#map.![#this.value.name]").getValue(context1, Collection.class);
EvaluationContext context1 = new StandardEvaluationContext();
ExpressionParser parser = new SpelExpressionParser();
context1.setVariable("collection", collection);
Collection result1 =
parser.parseExpression("#collection.?[#this.uuid.equals('u1')]").getValue(context1, Collection.class);
EvaluationContext context1 = new StandardEvaluationContext();
ExpressionParser parser = new SpelExpressionParser();
context1.setVariable("map", map);
Map result1 =
parser.parseExpression("#map.?[#this.key=='u1']").getValue(context1, Map.class);
Xml风格的配置示例----通过SpEL表达式参照其他的Bean
java代码:
public class SpELBean {
@Value("#{ T(java.lang.Math).random() * 100.0 }")
private String value;
//setter和getter由于篇幅省略,自己写上
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26660100/viewspace-717243/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/26660100/viewspace-717243/