Details
-
Improvement
-
Status: Resolved
-
Minor
-
Resolution: Won't Fix
-
2.21.1
-
None
-
None
-
Unknown
Description
Currently, only simple Spring beans lookup is supported, like #myBean. Deeper lookup is currently not supported. As a workaround, you could specify several Spring beans which then maps to the properties of the original bean.
Following code snipped would, for instance, make possible more complex queries:
public class MyApplicationContextRegistry extends ApplicationContextRegistry { private final BeanResolver beanResolver; private final PropertyAccessor propertyAccessor; private final BeanExpressionContext beanExpressionContext; MyApplicationContextRegistry(ApplicationContext applicationContext) { super(applicationContext); beanResolver = new BeanFactoryResolver(applicationContext); propertyAccessor = new BeanExpressionContextAccessor(); beanExpressionContext = createExpressionContext(applicationContext); } @Override public Object lookupByName(final String name) { assert name != null : "Unspecified name"; final ExpressionParser parser = new SpelExpressionParser(); Expression expression = parser.parseExpression(name); Object expressionValue = expression.getValue(createEvaluationContext(), beanExpressionContext); return expressionValue; } private static BeanExpressionContext createExpressionContext(ApplicationContext applicationContext) { assert applicationContext instanceof ConfigurableApplicationContext : String.format("MUST be an instance of '%s'.", ConfigurableApplicationContext.class.getName()); ConfigurableBeanFactory configurableBeanFactory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory(); return new BeanExpressionContext(configurableBeanFactory, null); } private EvaluationContext createEvaluationContext() { StandardEvaluationContext context = new StandardEvaluationContext(); context.setBeanResolver(beanResolver); context.addPropertyAccessor(propertyAccessor); return context; } }
And this code uses out-of-the-box Spring functionalities (I think...).