java 使用 Spring 表达式语言以编程方式评估 bean 表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11616316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:47:26  来源:igfitidea点击:

Programmatically evaluate a bean expression with Spring Expression Language

javaspringspring-el

提问by Hyman

I have a simple Spring Bean Expression, which evaluates fine when I define it inside an application context file:

我有一个简单的 Spring Bean 表达式,当我在应用程序上下文文件中定义它时,它的计算结果很好:

<bean id="myConfigBean" class="com.example.myBeanConfigBean">
    <property name="myProperty" value="#{ someOtherBean.getData() }"/>
</bean>

Now, I want to do the same evaluation programmatically. I have used the following code:

现在,我想以编程方式进行相同的评估。我使用了以下代码:

final ExpressionParser parser = new SpelExpressionParser();
final TemplateParserContext templateContext = new TemplateParserContext();
Expression expression = parser.parseExpression("#{ someOtherBean.getData() }", templateContext);
final String value = (String) expression.getValue();

This throws an exception:

这会引发异常:

EL1007E:(pos 22): Field or property 'someOtherBean' cannot be found on null

I guess I have to set a root object somehow that allows to the configured beans like a property. But I did not get it to work yet. Anyone, who has done this already and could give a hint?

我想我必须以某种方式设置一个根对象,以允许像属性一样配置 bean。但我还没有让它工作。任何人,谁已经这样做了并且可以给出提示?

采纳答案by Gary Russell

implement BeanFactoryAware to get a reference to the bean factory; then...

实现 BeanFactoryAware 以获取对 bean 工厂的引用;然后...

StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(new BeanFactoryResolver(this.beanFactory));
Expression expression = parser.parseExpression("@someOtherBean.getData()"); 
// or "@someOtherBean.data"
final String value = expression.getValue(context, String.class);

EDIT

编辑

To answer the comment below. The @triggers the use of the bean factory resolver to access a bean; an alternative is to add a BeanExpressionContextAccessorto the evaluation context and use a BeanExpressionContextas the root object for the evaluation...

回答下面的评论。该@触发器使用bean工厂解析器来访问一个bean; 另一种方法是将 a 添加BeanExpressionContextAccessor到评估上下文并使用 aBeanExpressionContext作为评估的根对象...

final ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(new BeanFactoryResolver(beanFactory));
context.addPropertyAccessor(new BeanExpressionContextAccessor());
Expression expression = parser.parseExpression("someOtherBean.getData()");
BeanExpressionContext rootObject = new BeanExpressionContext(beanFactory, null);

...

String value = expression.getValue(context, rootObject, String.class);

回答by myset

Please take a look @ https://www.mkyong.com/spring3/test-spring-el-with-expressionparser/

请看看@https ://www.mkyong.com/spring3/test-spring-el-with-expressionparser/

Sample java code

示例 Java 代码

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 App {
    public static void main(String[] args) {

        ExpressionParser parser = new SpelExpressionParser();

        //literal expressions
        Expression exp = parser.parseExpression("'Hello World'");
        String msg1 = exp.getValue(String.class);
        System.out.println(msg1);

        //method invocation
        Expression exp2 = parser.parseExpression("'Hello World'.length()");
        int msg2 = (Integer) exp2.getValue();
        System.out.println(msg2);

        //Mathematical operators
        Expression exp3 = parser.parseExpression("100 * 2");
        int msg3 = (Integer) exp3.getValue();
        System.out.println(msg3);

        //create an item object
        Item item = new Item("mkyong", 100);
        //test EL with item object
        StandardEvaluationContext itemContext = new StandardEvaluationContext(item);

        //display the value of item.name property
        Expression exp4 = parser.parseExpression("name");
        String msg4 = exp4.getValue(itemContext, String.class);
        System.out.println(msg4);

        //test if item.name == 'mkyong'
        Expression exp5 = parser.parseExpression("name == 'mkyong'");
        boolean msg5 = exp5.getValue(itemContext, Boolean.class);
        System.out.println(msg5);

    }
}