Programmatically evaluate a bean expression with Spring Expression Language

18,009

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

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 BeanExpressionContextAccessor to the evaluation context and use a BeanExpressionContext as the root object for the evaluation...

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);
Share:
18,009
Jack
Author by

Jack

Updated on June 24, 2022

Comments

  • Jack
    Jack almost 2 years

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

    <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?

  • RiyasAbdulla
    RiyasAbdulla about 7 years
    Can i use the above expression without '@' symbol? @someOtherBean.getData() >> someOtherBean.getData().
  • Gary Russell
    Gary Russell about 7 years
    Not without additional configuration; see the edit to my answer. You should really ask a new question - the admins here don't like new questions in comments.
  • JDev
    JDev about 4 years
    @Gary Russell I have a similar requirement only difference is my method take parameters. How can I pass dynamic parameters? So method signature is someOtherBean.getData(Objec[] args), I am looking for a way to pass the parameters. Tried using Expression expression = parser.parseExpression("someOtherBean.getData(#args)") but its not working for me
  • Gary Russell
    Gary Russell about 4 years
    Don't ask new questions in comments to old answers. This is one is nearly 8 years old!!! Ask a new question and provide much more context. Where do you expect the value of #args to come from? The expression itself can evaluate the value to pass into the method.