Christian Ribeaud created CAMEL-12683:
-----------------------------------------

             Summary: When lookup the registry, Camel should support more 
complex expressions like 'myBean.property'
                 Key: CAMEL-12683
                 URL: https://issues.apache.org/jira/browse/CAMEL-12683
             Project: Camel
          Issue Type: Improvement
          Components: camel-spring
    Affects Versions: 2.21.1
            Reporter: Christian Ribeaud


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:
{code:java}
public class MyApplicationContextRegistry extends ApplicationContextRegistry {

    private final BeanResolver beanResolver;
    private final PropertyAccessor propertyAccessor;
    private final BeanExpressionContext beanExpressionContext;

    ProdicApplicationContextRegistry(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;
    }
}
{code}
And this code uses _out-of-the-box_ *Spring* functionalities (I think...).




--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to