SpringBoot基础之Spring Expression L
2021-12-06 本文已影响0人
汤太咸啊
今天整理了下SpEL表达式的一些内容,挺有意思,大家可以看看,其实我这段时间发的这些都内容,都是我从公司Pluralsight中整理下来的相关知识,很多还是很不错的,如果时间充足的可以多去看看,很多知识非常不错,自己也在复习一遍。
SpEL表达式,直接通过表达式调用一些方法
SpelExpressionParser parser = new SpelExpressionParser();
Expression exp1 = parser.parseExpression("'Hello World'");
String message = (String)exp1.getValue();
System.out.println(message);
exp1 = parser.parseExpression("'Hello World'.length()");
System.out.println(exp1.getValue());
exp1 = parser.parseExpression("'Hello World'.length()*10");
System.out.println(exp1.getValue());
exp1 = parser.parseExpression("'Hello World'.length()<10");
System.out.println(exp1.getValue());
exp1 = parser.parseExpression("'Hello World'.length()>10");
System.out.println(exp1.getValue());
输出
Hello World
11
110
false
true
通过StandardEvaluationContext调用方法
StandardEvaluationContext ec1 = new StandardEvaluationContext();
ec1.setVariable("greeting","Hello China");
String msg = (String) parser.parseExpression("#greeting.substring(6)").getValue(ec1);
System.out.println(msg);
输出
China
通过StandardEvaluationContext设置Bean的值
@Component("User")
public class User {
private String name;
private int age;
private String country;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
User user = new User();
StandardEvaluationContext userContext = new StandardEvaluationContext(user);
parser.parseExpression("country").setValue(userContext, "USA");
System.out.println(user.getCountry());
parser.parseExpression("age").setValue(userContext, 19);
System.out.println(user.getAge());
输出
USA
19
设置system的properties属性值
StandardEvaluationContext propsContext = new StandardEvaluationContext();
propsContext.setVariable("systemProperties",System.getProperties());
Expression expCountry = parser.parseExpression("#systemProperties['user.country']");
parser.parseExpression("country").setValue(userContext, expCountry.getValue(propsContext));
System.out.println(user.getCountry());
输出
US
通过注解@Value使用SpEL,以及可用使用计算或者比较方法,甚至直接设置类
@Value("#{'Jhon Doe'}")
private String name;
@Value("#{30}")
private int age;
@Value("#{ systemProperties['user.timezone']}")
public void setTimeZone(String timeZone){
this.timeZone = timeZone;
}
public User(@Value("#{systemProperties['user.language']}") String language, @Value("#{systemProperties['user.country']}")String country) {
this.language = language;
this.country = country;
}
//输出User值
Jhon Doe
30
Asia/Shanghai
@Value("#{100.5+500.75+400.66}")
private double amount;
@Value("#{order.amount>=1000?order.amount*5/100:0}")
private double discount;
@Value("#{user.country =='US' and user.timeZone =='America/New_Yourk' ? 3:14}")
private int daysToDeliver;
@Value("#{T(java.text.NumberFormat).getCurrencyInstance(T(java.util.Locale).getDefault()).format(order.amount)}")
private String formattedAmount;
@Value("#{user.name} your order total is #{order.formattedAmount} and discount is #{order.amount - order.discount}")
private String orderSumary;
//输出以上五个值
1001.9100000000001
50.0955
14
$1,001.91
Jhon Doe your order total is $1,001.91 and discount is 951.8145000000001
List和Map的情况
@Component("city")
public class City {
private String name;
private double shipping;
private boolean isCapital;
//构造方法,setget方法
}
@Component("shipping")
public class Shipping {
private Map<String, List<City>> locationsByCountry;
private Map<String,List<City>> chargesByLocation;
//设置两个map数据
}
@Component("order")
public Class Order{
//通过user.country直接获取到某一个List
@Value("#{(shipping.locationsByCountry[user.country])}")
private List<City> shippingLocation;
@Value("#{order.shippingLocation.?[isCapital != true]}")
private List<City> nonCapitalShippingLocations;
@Value("#{(shipping.locationsByCountry.?[key=='UK' or key=='US'])}")
private Map<String,List<City>> westernShippingLocations;
@Value("#{order.shippingLocation.?[shipping<10].size()}")
private Integer noOfCheapShippingLocations;
}