spring表达式计算分数(权重)

计算商品权重Demo:

package net.highersoft.solr;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

public class Express {

	public static void main(String[] args) {
		ExpressionParser parser = new SpelExpressionParser();
		
		Expression exp = parser.parseExpression("3*(1+2)");
		Object message = exp.getValue();
		System.out.println(message);
		
		//加参数
		EvaluationContext context = new StandardEvaluationContext();  
        context.setVariable("score", 3);
        context.setVariable("tscore", 1);
        exp = parser.parseExpression("3*(1+2)*#score+#tscore");
		System.out.println(exp.getValue(context));
	}

}

依赖jar包,pom.xml:

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-expression</artifactId>
	<version>5.2.7.RELEASE</version>
</dependency>

文/程忠 浏览次数:0次   2020-07-21 11:43:36

相关阅读


评论: