本文整理汇总了Java中org.springframework.expression.spel.support.StandardEvaluationContext.setVariable方法的典型用法代码示例。如果您正苦于以下问题:Java StandardEvaluationContext.setVariable方法的具体用法?Java StandardEvaluationContext.setVariable怎么用?Java StandardEvaluationContext.setVariable使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.expression.spel.support.StandardEvaluationContext
的用法示例。
在下文中一共展示了StandardEvaluationContext.setVariable方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testScenario_DefiningVariablesThatWillBeAccessibleInExpressions
import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
/**
* Scenario: using the standard context but adding your own variables
*/
@Test
public void testScenario_DefiningVariablesThatWillBeAccessibleInExpressions() throws Exception {
// Create a parser
SpelExpressionParser parser = new SpelExpressionParser();
// Use the standard evaluation context
StandardEvaluationContext ctx = new StandardEvaluationContext();
ctx.setVariable("favouriteColour","blue");
List<Integer> primes = new ArrayList<Integer>();
primes.addAll(Arrays.asList(2,3,5,7,11,13,17));
ctx.setVariable("primes",primes);
Expression expr = parser.parseRaw("#favouriteColour");
Object value = expr.getValue(ctx);
assertEquals("blue", value);
expr = parser.parseRaw("#primes.get(1)");
value = expr.getValue(ctx);
assertEquals(3, value);
// all prime numbers > 10 from the list (using selection ?{...})
expr = parser.parseRaw("#primes.?[#this>10]");
value = expr.getValue(ctx);
assertEquals("[11, 13, 17]", value.toString());
}
示例2: testSpecialVariables
import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的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());
}
示例3: testTernary
import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
@Test
public void testTernary() throws Exception {
String falseString = parser.parseExpression("false ? 'trueExp' : 'falseExp'").getValue(String.class);
assertEquals("falseExp",falseString);
StandardEvaluationContext societyContext = new StandardEvaluationContext();
societyContext.setRootObject(new IEEE());
parser.parseExpression("Name").setValue(societyContext, "IEEE");
societyContext.setVariable("queryName", "Nikola Tesla");
String expression = "isMember(#queryName)? #queryName + ' is a member of the ' "
+ "+ Name + ' Society' : #queryName + ' is not a member of the ' + Name + ' Society'";
String queryResultString = parser.parseExpression(expression).getValue(societyContext, String.class);
assertEquals("Nikola Tesla is a member of the IEEE Society",queryResultString);
// queryResultString = "Nikola Tesla is a member of the IEEE Society"
}
示例4: SPR10452
import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的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));
}
示例5: testCallingIllegalFunctions
import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
@Test
public void testCallingIllegalFunctions() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext ctx = new StandardEvaluationContext();
ctx.setVariable("notStatic", this.getClass().getMethod("nonStatic"));
try {
@SuppressWarnings("unused")
Object v = parser.parseRaw("#notStatic()").getValue(ctx);
fail("Should have failed with exception - cannot call non static method that way");
} catch (SpelEvaluationException se) {
if (se.getMessageCode() != SpelMessage.FUNCTION_MUST_BE_STATIC) {
se.printStackTrace();
fail("Should have failed a message about the function needing to be static, not: "
+ se.getMessageCode());
}
}
}
示例6: testScenario03_Arithmetic
import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
@Test
public void testScenario03_Arithmetic() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext ctx = new StandardEvaluationContext();
// Might be better with a as a variable although it would work as a property too...
// Variable references using a '#'
Expression expr = parser.parseRaw("(hasRole('SUPERVISOR') or (#a < 1.042)) and hasIpAddress('10.10.0.0/16')");
Boolean value = null;
ctx.setVariable("a",1.0d); // referenced as #a in the expression
ctx.setRootObject(new Supervisor("Ben")); // so non-qualified references 'hasRole()' 'hasIpAddress()' are invoked against it
value = expr.getValue(ctx,Boolean.class);
assertTrue(value);
ctx.setRootObject(new Manager("Luke"));
ctx.setVariable("a",1.043d);
value = expr.getValue(ctx,Boolean.class);
assertFalse(value);
}
示例7: testScenario04_ControllingWhichMethodsRun
import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
@Test
public void testScenario04_ControllingWhichMethodsRun() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext ctx = new StandardEvaluationContext();
ctx.setRootObject(new Supervisor("Ben")); // so non-qualified references 'hasRole()' 'hasIpAddress()' are invoked against it);
ctx.addMethodResolver(new MyMethodResolver()); // NEEDS TO OVERRIDE THE REFLECTION ONE - SHOW REORDERING MECHANISM
// Might be better with a as a variable although it would work as a property too...
// Variable references using a '#'
// SpelExpression expr = parser.parseExpression("(hasRole('SUPERVISOR') or (#a < 1.042)) and hasIpAddress('10.10.0.0/16')");
Expression expr = parser.parseRaw("(hasRole(3) or (#a < 1.042)) and hasIpAddress('10.10.0.0/16')");
Boolean value = null;
ctx.setVariable("a",1.0d); // referenced as #a in the expression
value = expr.getValue(ctx,Boolean.class);
assertTrue(value);
// ctx.setRootObject(new Manager("Luke"));
// ctx.setVariable("a",1.043d);
// value = (Boolean)expr.getValue(ctx,Boolean.class);
// assertFalse(value);
}
示例8: parseKeyByParam
import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的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();
}
示例9: evaluate
import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
/**
* Evaluate
*
* @return Evaluated Expression
*/
public Object evaluate(final FileContents fileContents,
final FileRecord fileRecord, final String expression) {
/* Create Context */
StandardEvaluationContext standardEvaluationContext = new StandardEvaluationContext(
fileRecord);
standardEvaluationContext.setVariable("aggregations",
fileContents.getAggregations());
/* Evaluate Expressions */
LOGGER.info("Evaluating Expression: {}", expression);
return expressionParser.parseExpression(expression)
.getValue(standardEvaluationContext);
}
示例10: encryptUsingPlatformBean
import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
@Override
public String encryptUsingPlatformBean(String cleartext) {
SpelExpressionParser expressionParser = new SpelExpressionParser();
String encryptExpressionString = ApplicationProperties.get("encryptExpression");
Expression encryptExpression = expressionParser.parseExpression(encryptExpressionString);
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("stringToEncrypt", cleartext);
return encryptExpression.getValue(context, String.class);
}
示例11: decrypt
import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
public String decrypt(String encryptedValue) {
if (encryptedValue==null) {
return null;
}
final ExpressionParser expressionParser = new SpelExpressionParser();
final Expression decryptExpression = expressionParser.parseExpression(decryptorImpl);
final StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("stringToDecrypt", encryptedValue);
return decryptExpression.getValue(context, String.class);
}
示例12: encrypt
import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
public String encrypt(String unencryptedValue) {
if (unencryptedValue==null) {
return null;
}
final ExpressionParser expressionParser = new SpelExpressionParser();
final Expression encryptExpression = expressionParser.parseExpression(encryptorImpl);
final StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("stringToEncrypt", unencryptedValue);
return encryptExpression.getValue(context, String.class);
}
示例13: renderQueryIfExpressionOrReturnQuery
import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
/**
* @param query, the query expression potentially containing a SpEL expression. Must not be {@literal null}.}
* @param metadata the {@link EntityMetadata} for the given entity. Must not be {@literal null}.
* @param parser Must not be {@literal null}.
* @return
*/
private static String renderQueryIfExpressionOrReturnQuery(String query, EntityMetadata<?> metadata,
SpelExpressionParser parser) {
Assert.notNull(query, "query must not be null!");
Assert.notNull(metadata, "metadata must not be null!");
Assert.notNull(parser, "parser must not be null!");
if (!containsExpression(query)) {
return query;
}
StandardEvaluationContext evalContext = new StandardEvaluationContext();
evalContext.setVariable(ENTITY_NAME, metadata.getJavaType().getName());
query = potentiallyQuoteExpressionsParameter(query);
Expression expr = parser.parseExpression(query, ParserContext.TEMPLATE_EXPRESSION);
String result = expr.getValue(evalContext, String.class);
if (result == null) {
return query;
}
return potentiallyUnquoteParameterExpressions(result);
}
示例14: parseKey
import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
/**
* <p>
* 获取缓存的key key 定义在注解上,支持SPEL表达式
* </p>
*
* @param args
* @return key 调用方设置的key
* @author fulei.yang
* @date 2016/11/03
* @version 0.0.1
*/
private String parseKey(String key, Method method, Object[] args) {
// 获取被拦截方法参数名列表(使用Spring支持类库)
LocalVariableTableParameterNameDiscoverer localVariableTableParameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
String[] paraNameArr = localVariableTableParameterNameDiscoverer.getParameterNames(method);
// 使用SPEL进行key的解析
ExpressionParser parser = new SpelExpressionParser();
// SPEL上下文
StandardEvaluationContext context = new StandardEvaluationContext();
// 把方法参数放入SPEL上下文中
for (int i = 0; i < paraNameArr.length; i++) {
context.setVariable(paraNameArr[i], args[i]);
}
return parser.parseExpression(key).getValue(context, String.class);
}
示例15: SPR12803
import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
@Test
public void SPR12803() throws Exception {
StandardEvaluationContext sec = new StandardEvaluationContext();
sec.setVariable("iterable", Collections.emptyList());
SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("T(org.springframework.expression.spel.SpelReproTests.GuavaLists).newArrayList(#iterable)");
assertTrue(expression.getValue(sec) instanceof ArrayList);
}