package spring;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.SpelCompilerMode;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.SimpleEvaluationContext;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import java.util.*;
/**
*
* @Date: 2022/1/19 13:41
*/
@Slf4j
public class Spel {
public static void main(String[] args) {
Spel spel = new Spel();
// spel.cal();
spel.demo1();
}
@Test
public void cal() {
String spelExpression = "#diffDead/#average * #score";
Map<String, Object> variables = new HashMap<>();
variables.put("diffDead", 25);
variables.put("average", new Double(21));
variables.put("score", 100);
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariables(variables);
Expression exp = parser.parseExpression(spelExpression);
Integer value = exp.getValue(context, Integer.class);
System.out.println("value:" + value);
}
@Test
public void demo1() {
ExpressionParser parser = new SpelExpressionParser();
Expression info = parser.parseExpression("'hello'");
Object val = info.getValue();
System.out.println(val);
}
@Test
public void demo3() {
ExpressionParser parser = new SpelExpressionParser();
Expression info = parser.parseExpression("'hello'.concat('1')");
Object val = info.getValue();
System.out.println(val);
}
@Test
public void demo4() {
ExpressionParser parser = new SpelExpressionParser();
Expression info = parser.parseExpression("'hello'.getBytes"); //the same as ‘hello’.bytes
byte[] val = (byte[]) info.getValue();
for (int i = 0; i < val.length; i++) {
System.out.println(val[i]);
}
}
@Test
public void demo5() {
ExpressionParser parser = new SpelExpressionParser();
Expression info = parser.parseExpression("'hello'.getBytes.length");
Object val = info.getValue();
System.out.println(val);
}
@Test
public void demo6() {
// Create and set a calendar
GregorianCalendar c = new GregorianCalendar();
c.set(1856, 7, 9);
// The constructor arguments are name, birthday, and nationality.
Date time = c.getTime();
Inventor tesla = new Inventor("Nikola Tesla", c.getTime(), "Serbian");
Inventor tesla2 = new Inventor("Nikola Tesla2", c.getTime(), "Serbian");
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("name"); // Parse name as an expression
String name = (String) exp.getValue(tesla);
String name2 = (String) exp.getValue(tesla2);
log.info(name);
log.info(name2);
exp = parser.parseExpression("name == 'Nikola Tesla'");
boolean result = exp.getValue(tesla, Boolean.class);
log.info("result:{}", result);
}
@Test
public void demo7(){
Simple simple = new Simple();
simple.booleanList.add(true);
simple.booleanList.add(true);
//这个是做什么用的
EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
//两个参数,设置最大的size数
SpelParserConfiguration config = new SpelParserConfiguration(true, true,4);
ExpressionParser parser = new SpelExpressionParser(config);
// "false" is passed in here as a String. SpEL and the conversion service
// will recognize that it needs to be a Boolean and convert it accordingly.
parser.parseExpression("booleanList[3]").setValue(context, simple, "false");
// b is false
simple.booleanList.forEach(r->{
log.info("result:{}",r);
});
}
@Test
public void demo2() {
SpelParserConfiguration config = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE,
this.getClass().getClassLoader());
SpelExpressionParser parser = new SpelExpressionParser(config);
Expression expr = parser.parseExpression("payload");
MyMessage message = new MyMessage();
Object payload = expr.getValue(message);
}
}
@Data
class MyMessage {
String payload = "123";
}
@Data
class Inventor {
String name;
String location;
Date birth;
public Inventor(String name, Date birth, String location) {
this.name = name;
this.location = location;
this.birth = birth;
}
}
class Simple {
public List<Boolean> booleanList = new ArrayList<Boolean>();
}
spel表达式
最新推荐文章于 2024-06-19 16:31:24 发布