I was working the expressions and trying to play with the contexts (the one we use at creation time and the one we use at evaluation time). Here is some code below that is trying to reproduce my needs and highlight the problem.
ExpressionFactory factory = new ExpressionFactoryImpl();
SimpleContext createContext = new SimpleContext();
createContext.setVariable("myBean", factory.createValueExpression(new MyBean("Laurent"), MyBean.class));
String expression;
expression = "${myBean.foo} ${exchange.host}";
ValueExpression expr = factory.createValueExpression(createContext, expression, String.class);
System.out.println("expr Class : " + expr.getClass());
SimpleContext evalContext = new SimpleContext();
List<String> hosts = asList("www.example.com", "www.foo.com", "www.bar.com");
// I want to evaluate the same expression, but with different values for the variable exchange.
for (String host : hosts) {
evalContext.setVariable("exchange", factory.createValueExpression(new MyExchange(host), MyExchange.class));
System.out.println(expression + " = " + expr.getValue(evalContext));
}
I setup a basic Maven project on https://github.com/laurentvaills/test-juel-expression to reproduce it .
Can you tell me why I got the following error : javax.el.PropertyNotFoundException: Cannot find property exchange ?