我正在使用spEL 4.0.0.RELEASE将http参数绑定到java对象.
我多次使用表达式解析器时会注意到一些奇怪的问题:每次调用都需要更多的时间(常量).这只发生在java 8上.在java 7上都可以.
这是示例代码:
package ru.tersys.test;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
public class Test {
public static void main(String[] args) {
Test instance = new Test();
Table t = new Table();
Column c = new Column();
t.getColumns().add(c);
int counter = 10;
while (counter-- > 0)
{
instance.doTest("t",t);
}
}
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
public void doTest(String prefix,Object obj)
{
Date d = Calendar.getInstance().getTime();
int counter = 0;
String el = "t.columns[0].name";
context.setVariable(prefix,obj);
String prefixWithDot = prefix + ".";
int count = 400;
while (count -- > 0)
{
if (el.startsWith(prefixWithDot))
{
parser.parseExpression("#"+el).setValue(context,"testColumnName");
counter++;
}
}
System.out.println("bind duration sec = " + (Calendar.getInstance().getTime().getTime() - d.getTime())/1000 + " param count = " + counter);
}
static class Table
{
public Table()
{
columns = new ArrayList<>();
}
public ArrayList
jdk 8输出:
bind duration sec = 1 param count = 400
bind duration sec = 3 param count = 400
bind duration sec = 5 param count = 400
bind duration sec = 7 param count = 400
bind duration sec = 9 param count = 400
bind duration sec = 12 param count = 400
bind duration sec = 14 param count = 400
bind duration sec = 16 param count = 400
bind duration sec = 19 param count = 400
bind duration sec = 22 param count = 400
java 7输出:
bind duration sec = 0 param count = 400
bind duration sec = 0 param count = 400
bind duration sec = 0 param count = 400
bind duration sec = 0 param count = 400
bind duration sec = 0 param count = 400
bind duration sec = 0 param count = 400
bind duration sec = 0 param count = 400
bind duration sec = 0 param count = 400
bind duration sec = 0 param count = 400
bind duration sec = 0 param count = 400
最佳答案
原文链接:https://www.f2er.com/spring/431518.html