本文整理汇总了Java中org.springframework.expression.ExpressionParser类的典型用法代码示例。如果您正苦于以下问题:Java ExpressionParser类的具体用法?Java ExpressionParser怎么用?Java ExpressionParser使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ExpressionParser类属于org.springframework.expression包,在下文中一共展示了ExpressionParser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getUserInfos
import org.springframework.expression.ExpressionParser; //导入依赖的package包/类
@Override
public Map<String, String> getUserInfos(User user, HttpServletRequest request, final Map<String, String> userInfosInComputing) {
Map<String, String> userInfos = new HashMap<String, String>();
for(String name: sgcParam2spelExp.keySet()) {
String expression = sgcParam2spelExp.get(name);
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression(expression);
EvaluationContext context = new StandardEvaluationContext();
context.setVariable("user", user);
context.setVariable("request", request);
context.setVariable("userInfosInComputing", userInfosInComputing);
context.setVariable("dateUtils", dateUtils);
String value = (String) exp.getValue(context);
userInfos.put(name, value);
}
return userInfos;
}
示例2: getKeyFilter
import org.springframework.expression.ExpressionParser; //导入依赖的package包/类
/**
* Creates the key filter lambda which is responsible to decide how the rate limit will be performed. The key
* is the unique identifier like an IP address or a username.
*
* @param url is used to generated a unique cache key
* @param rateLimit the {@link RateLimit} configuration which holds the skip condition string
* @param expressionParser is used to evaluate the expression if the filter key type is EXPRESSION.
* @param beanFactory used to get full access to all java beans in the SpEl
* @return should not been null. If no filter key type is matching a plain 1 is returned so that all requests uses the same key.
*/
public KeyFilter getKeyFilter(String url, RateLimit rateLimit, ExpressionParser expressionParser, BeanFactory beanFactory) {
switch(rateLimit.getFilterKeyType()) {
case IP:
return (request) -> url + "-" + request.getRemoteAddr();
case EXPRESSION:
String expression = rateLimit.getExpression();
if(StringUtils.isEmpty(expression)) {
throw new MissingKeyFilterExpressionException();
}
StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(new BeanFactoryResolver(beanFactory));
return (request) -> {
//TODO performance problem - how can the request object reused in the expression without setting it as a rootObject
Expression expr = expressionParser.parseExpression(rateLimit.getExpression());
final String value = expr.getValue(context, request, String.class);
return url + "-" + value;
};
}
return (request) -> url + "-" + "1";
}
示例3: skipCondition
import org.springframework.expression.ExpressionParser; //导入依赖的package包/类
/**
* Creates the lambda for the skip condition which will be evaluated on each request
*
* @param rateLimit the {@link RateLimit} configuration which holds the skip condition string
* @param expressionParser is used to evaluate the skip expression
* @param beanFactory used to get full access to all java beans in the SpEl
* @return the lamdba condition which will be evaluated lazy - null if there is no condition available.
*/
public Condition skipCondition(RateLimit rateLimit, ExpressionParser expressionParser, BeanFactory beanFactory) {
StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(new BeanFactoryResolver(beanFactory));
if(rateLimit.getSkipCondition() != null) {
return (request) -> {
Expression expr = expressionParser.parseExpression(rateLimit.getSkipCondition());
Boolean value = expr.getValue(context, request, Boolean.class);
return value;
};
}
return null;
}
示例4: testRootObject
import org.springframework.expression.ExpressionParser; //导入依赖的package包/类
@Test
public void testRootObject() throws Exception {
GregorianCalendar c = new GregorianCalendar();
c.set(1856, 7, 9);
// The constructor arguments are name, birthday, and nationaltiy.
Inventor tesla = new Inventor("Nikola Tesla", c.getTime(), "Serbian");
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("name");
StandardEvaluationContext context = new StandardEvaluationContext();
context.setRootObject(tesla);
String name = (String) exp.getValue(context);
assertEquals("Nikola Tesla",name);
}
示例5: testLiterals
import org.springframework.expression.ExpressionParser; //导入依赖的package包/类
@Test
public void testLiterals() throws Exception {
ExpressionParser parser = new SpelExpressionParser();
String helloWorld = (String) parser.parseExpression("'Hello World'").getValue(); // evals to "Hello World"
assertEquals("Hello World",helloWorld);
double avogadrosNumber = (Double) parser.parseExpression("6.0221415E+23").getValue();
assertEquals(6.0221415E+23, avogadrosNumber, 0);
int maxValue = (Integer) parser.parseExpression("0x7FFFFFFF").getValue(); // evals to 2147483647
assertEquals(Integer.MAX_VALUE,maxValue);
boolean trueValue = (Boolean) parser.parseExpression("true").getValue();
assertTrue(trueValue);
Object nullValue = parser.parseExpression("null").getValue();
assertNull(nullValue);
}
示例6: testSpecialVariables
import org.springframework.expression.ExpressionParser; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Test
public void testSpecialVariables() throws Exception {
// create an array of integers
List<Integer> primes = new ArrayList<Integer>();
primes.addAll(Arrays.asList(2,3,5,7,11,13,17));
// create parser and set variable 'primes' as the array of integers
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("primes",primes);
// all prime numbers > 10 from the list (using selection ?{...})
List<Integer> primesGreaterThanTen = (List<Integer>) parser.parseExpression("#primes.?[#this>10]").getValue(context);
assertEquals("[11, 13, 17]",primesGreaterThanTen.toString());
}
示例7: testCreateListsOnAttemptToIndexNull01
import org.springframework.expression.ExpressionParser; //导入依赖的package包/类
@Test
public void testCreateListsOnAttemptToIndexNull01() throws EvaluationException, ParseException {
ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
Expression expression = parser.parseExpression("list[0]");
TestClass testClass = new TestClass();
Object o = null;
o = expression.getValue(new StandardEvaluationContext(testClass));
assertEquals("", o);
o = parser.parseExpression("list[3]").getValue(new StandardEvaluationContext(testClass));
assertEquals("", o);
assertEquals(4, testClass.list.size());
try {
o = parser.parseExpression("list2[3]").getValue(new StandardEvaluationContext(testClass));
fail();
} catch (EvaluationException ee) {
ee.printStackTrace();
// success!
}
o = parser.parseExpression("foo[3]").getValue(new StandardEvaluationContext(testClass));
assertEquals("", o);
assertEquals(4, testClass.getFoo().size());
}
示例8: initializingCollectionElementsOnWrite
import org.springframework.expression.ExpressionParser; //导入依赖的package包/类
/**
* SPR-6984: attempting to index a collection on write using an index that
* doesn't currently exist in the collection (address.crossStreets[0] below)
*/
@Test
public void initializingCollectionElementsOnWrite() throws Exception {
TestPerson person = new TestPerson();
EvaluationContext context = new StandardEvaluationContext(person);
SpelParserConfiguration config = new SpelParserConfiguration(true, true);
ExpressionParser parser = new SpelExpressionParser(config);
Expression expression = parser.parseExpression("name");
expression.setValue(context, "Oleg");
assertEquals("Oleg", person.getName());
expression = parser.parseExpression("address.street");
expression.setValue(context, "123 High St");
assertEquals("123 High St", person.getAddress().getStreet());
expression = parser.parseExpression("address.crossStreets[0]");
expression.setValue(context, "Blah");
assertEquals("Blah", person.getAddress().getCrossStreets().get(0));
expression = parser.parseExpression("address.crossStreets[3]");
expression.setValue(context, "Wibble");
assertEquals("Blah", person.getAddress().getCrossStreets().get(0));
assertEquals("Wibble", person.getAddress().getCrossStreets().get(3));
}
示例9: incdecTogether
import org.springframework.expression.ExpressionParser; //导入依赖的package包/类
@Test
public void incdecTogether() {
Spr9751 helper = new Spr9751();
StandardEvaluationContext ctx = new StandardEvaluationContext(helper);
ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
Expression e = null;
// index1 is 2 at the start - the 'intArray[#root.index1++]' should not be evaluated twice!
// intArray[2] is 3
e = parser.parseExpression("intArray[#root.index1++]++");
e.getValue(ctx,Integer.class);
assertEquals(3,helper.index1);
assertEquals(4,helper.intArray[2]);
// index1 is 3 intArray[3] is 4
e = parser.parseExpression("intArray[#root.index1++]--");
assertEquals(4,e.getValue(ctx,Integer.class).intValue());
assertEquals(4,helper.index1);
assertEquals(3,helper.intArray[3]);
// index1 is 4, intArray[3] is 3
e = parser.parseExpression("intArray[--#root.index1]++");
assertEquals(3,e.getValue(ctx,Integer.class).intValue());
assertEquals(3,helper.index1);
assertEquals(4,helper.intArray[3]);
}
示例10: testGetValuePerformance
import org.springframework.expression.ExpressionParser; //导入依赖的package包/类
@Test
public void testGetValuePerformance() throws Exception {
Assume.group(TestGroup.PERFORMANCE);
Map<String, String> map = new HashMap<String, String>();
map.put("key", "value");
EvaluationContext context = new StandardEvaluationContext(map);
ExpressionParser spelExpressionParser = new SpelExpressionParser();
Expression expr = spelExpressionParser.parseExpression("#root['key']");
StopWatch s = new StopWatch();
s.start();
for (int i = 0; i < 10000; i++) {
expr.getValue(context);
}
s.stop();
assertThat(s.getTotalTimeMillis(), lessThan(200L));
}
示例11: propertyAccessorOrder_8211
import org.springframework.expression.ExpressionParser; //导入依赖的package包/类
/**
* We add property accessors in the order:
* First, Second, Third, Fourth.
* They are not utilized in this order; preventing a priority or order of operations
* in evaluation of SPEL expressions for a given context.
*/
@Test
public void propertyAccessorOrder_8211() {
ExpressionParser expressionParser = new SpelExpressionParser();
StandardEvaluationContext evaluationContext = new StandardEvaluationContext(new ContextObject());
evaluationContext.addPropertyAccessor(new TestPropertyAccessor("firstContext"));
evaluationContext.addPropertyAccessor(new TestPropertyAccessor("secondContext"));
evaluationContext.addPropertyAccessor(new TestPropertyAccessor("thirdContext"));
evaluationContext.addPropertyAccessor(new TestPropertyAccessor("fourthContext"));
assertEquals("first", expressionParser.parseExpression("shouldBeFirst").getValue(evaluationContext));
assertEquals("second", expressionParser.parseExpression("shouldBeSecond").getValue(evaluationContext));
assertEquals("third", expressionParser.parseExpression("shouldBeThird").getValue(evaluationContext));
assertEquals("fourth", expressionParser.parseExpression("shouldBeFourth").getValue(evaluationContext));
}
示例12: customStaticFunctions_SPR9038
import org.springframework.expression.ExpressionParser; //导入依赖的package包/类
/**
* Test the ability to subclass the ReflectiveMethodResolver and change how it
* determines the set of methods for a type.
*/
@Test
public void customStaticFunctions_SPR9038() {
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
List<MethodResolver> methodResolvers = new ArrayList<MethodResolver>();
methodResolvers.add(new ReflectiveMethodResolver() {
@Override
protected Method[] getMethods(Class<?> type) {
try {
return new Method[] {
Integer.class.getDeclaredMethod("parseInt", new Class<?>[] {String.class, Integer.TYPE})};
}
catch (NoSuchMethodException ex) {
return new Method[0];
}
}
});
context.setMethodResolvers(methodResolvers);
Expression expression = parser.parseExpression("parseInt('-FF', 16)");
Integer result = expression.getValue(context, "", Integer.class);
assertEquals(-255, result.intValue());
}
示例13: SPR10452
import org.springframework.expression.ExpressionParser; //导入依赖的package包/类
@Test
public void SPR10452() throws Exception {
SpelParserConfiguration configuration = new SpelParserConfiguration(false, false);
ExpressionParser parser = new SpelExpressionParser(configuration);
StandardEvaluationContext context = new StandardEvaluationContext();
Expression spel = parser.parseExpression("#enumType.values()");
context.setVariable("enumType", ABC.class);
Object result = spel.getValue(context);
assertNotNull(result);
assertTrue(result.getClass().isArray());
assertEquals(ABC.A, Array.get(result, 0));
assertEquals(ABC.B, Array.get(result, 1));
assertEquals(ABC.C, Array.get(result, 2));
context.setVariable("enumType", XYZ.class);
result = spel.getValue(context);
assertNotNull(result);
assertTrue(result.getClass().isArray());
assertEquals(XYZ.X, Array.get(result, 0));
assertEquals(XYZ.Y, Array.get(result, 1));
assertEquals(XYZ.Z, Array.get(result, 2));
}
示例14: SPR9735
import org.springframework.expression.ExpressionParser; //导入依赖的package包/类
@Test
public void SPR9735() {
Item item = new Item();
item.setName("parent");
Item item1 = new Item();
item1.setName("child1");
Item item2 = new Item();
item2.setName("child2");
item.add(item1);
item.add(item2);
ExpressionParser parser = new SpelExpressionParser();
EvaluationContext context = new StandardEvaluationContext();
Expression exp = parser.parseExpression("#item[0].name");
context.setVariable("item", item);
assertEquals("child1", exp.getValue(context));
}
示例15: parseKeyByParam
import org.springframework.expression.ExpressionParser; //导入依赖的package包/类
/**
* 通过spring spel解析参数获取redis缓存key
*
* @param keys
* 缓存keys
* @param paramNames
* 参数名
* @param args
* 参数列表
* @return
*/
public static String parseKeyByParam(String keys, String[] paramNames, Object[] args) {
if (StringUtils.isBlank(keys)) {
return "";
}
ExpressionParser parser = getParser();
StandardEvaluationContext context = new StandardEvaluationContext();
// 把方法参数放入SPEL上下文中
for (int i = 0; i < paramNames.length; i++) {
context.setVariable(paramNames[i], args[i]);
}
// 获取参数key
StringBuffer sb = new StringBuffer();
// for (int i = 0; i < keys.length; i++) {
sb.append(parser.parseExpression(keys).getValue(context, String.class));
// }
return sb.toString();
}