2017-03-01 128 views
0

您是否知道如何使用Spring內部Spel表達式解析器來解析包含Spring應用程序上下文的bean引用的String?使用Spring內部Spel表達式解析器

我已經看到SpelExpressionParser可以用於定義某些顯式用戶變量的StandardEvaluationContext。

我正在尋找一個解決方案,直接使用Spring內部Spel表達式解析器綁定到整個Spring應用程序上下文。這個想法是使用具有與@Value SPEL註釋相同功能的字符串模板。

回答

0

我發現該溶液通過使用:

private Object resolveExpression(String expression) { 
    String placeholdersResolved = applicationContext.getBeanFactory().resolveEmbeddedValue(expression); 
    BeanExpressionResolver expressionResolver = applicationContext.getBeanFactory().getBeanExpressionResolver(); 
    return expressionResolver.evaluate(placeholdersResolved, new BeanExpressionContext(applicationContext.getBeanFactory(), null)); 
} 

resolveEmbeddedValue替換$ {}與屬性佔位符表達。

使用Application Context Bean Factory評估解析#{}表達式