【问题标题】:Evaluate a bean expression with Spring EL with application context beans使用带有应用程序上下文 bean 的 Spring EL 评估 bean 表达式
【发布时间】:2015-11-16 16:14:51
【问题描述】:

我已经为此工作了几天,正在寻找解决方案,但我被卡住了。

这与其他已回答的问题类似: Programmatically evaluate a bean expression with Spring Expression Language

在我的 java 应用程序(命令行,而不是 Web)中,我使用 Spring 来连接应用程序类。在我的applicationContext.xml 我有这个

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

这很好用。 在我的应用程序中,我想评估类似的字符串,并且我开发了这种方法

@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; 
} 

但是当我运行这段代码时,会抛出一个错误

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) 

我想我已经接近我的目标了,但我不知道如何将应用程序上下文 bean 放入解析器,或者如何让它工作 有人可以帮忙吗?

【问题讨论】:

  • 您使用的是哪个 Spring 版本?另外,你能不试试TemplateParserContext,比如new SpelExpressionParser().parseExpression(toParse).getValue()吗?
  • 在解析EL表达式时尝试构造目标。
  • 没有TemplateParserContext,#{ } 不起作用。
  • 我的Spring版本是3.2.4,谢谢
  • 解析EL表达式时如何构造目标?

标签: java spring spring-el


【解决方案1】:

这应该可行:

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

如果你真的有systemProperties bean 那里...

对于 XML 定义,它的工作方式有点不同,因为它基于 StandardBeanExpressionResolver。但是在目标应用程序中使用有点复杂。

更新

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);
}

查看AbstractNamedValueMethodArgumentResolver源代码。

【讨论】:

  • @ 语法有效,但是我想在 applicationContext,xml 中使用#{systemProperties['ENV']}like。你知道如何让“StandartBeanExpressionResolver”工作吗?我一直在寻找,但没有找到任何明确的结果。
猜你喜欢
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 2017-04-24
  • 1970-01-01
  • 2018-12-17
  • 1970-01-01
  • 2017-07-18
  • 1970-01-01
相关资源
最近更新 更多