简体   繁体   中英

Evaluate a bean expression with Spring EL with application context beans

I've been working on this a few days, searching for a solution but I'm stuck.

It's similar to this other answered question: Programmatically evaluate a bean expression with Spring Expression Language

In my java application (command line, not web) I'm using Spring to wire the application classes. In my applicationContext.xml I have this

<bean id="appProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
    <property name="location" value="classpath:config/#{systemProperties['ENV']?:'LOCAL'}.properties"/> 
</bean> 

This works fine. Inside my app I want to evaluate similar strings, and I've developed this method

@Component("myAppAware ") 
public class MyAppAware implements BeanFactoryAware { 

@Autowired 
private ApplicationContext applicationContext; 


private BeanFactory beanFactory; 

@Override 
public void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.beanFactory = beanFactory; } 

public String process(){ 
    String toParse = "classpath:config/#{systemProperties['ENV']?:'LOCAL'}.properties"; 
    BeanFactoryResolver beanFactoryResolver = new BeanFactoryResolver(beanFactory); 
    StandardEvaluationContext context = new StandardEvaluationContext(); 
    context.setBeanResolver(beanFactoryResolver); 
    SpelExpressionParser parser = new SpelExpressionParser(); 
    TemplateParserContext templateContext = new TemplateParserContext(); 
    Expression expression = parser.parseExpression(toParse,templateContext); 
    Object ovalue = expression.getValue(context); // EXCEPTION THROWN HERE !!! 
    String value = ovalue.toString(); 
    return value; 
} 

But when I run this code, a error is thrown

org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Field or property 'systemProperties' cannot be found on null 
        at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:243) 
        at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:112) 
        at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:107) 

I think I'm near my objective, but I do not know how to get application context beans into he parser, or how to get it working anyone could help ?

This should work:

String toParse = "classpath:config/#{@systemProperties['ENV']?:'LOCAL'}.properties"; 

If you really have systemProperties bean there...

With an XML definition it works a bit different because it is based on the StandardBeanExpressionResolver . But is a bit complex to use from the target application.

UPDATE

this.expressionContext = (beanFactory != null ? new BeanExpressionContext(beanFactory, null) : null);
 ...............
private Object resolveDefaultValue(String defaultValue) {
    if (this.configurableBeanFactory == null) {
        return defaultValue;
    }
    String placeholdersResolved = this.configurableBeanFactory.resolveEmbeddedValue(defaultValue);
    BeanExpressionResolver exprResolver = this.configurableBeanFactory.getBeanExpressionResolver();
    if (exprResolver == null) {
        return defaultValue;
    }
    return exprResolver.evaluate(placeholdersResolved, this.expressionContext);
}

See AbstractNamedValueMethodArgumentResolver source code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM