Search in sources :

Example 11 with MapAccessor

use of org.springframework.context.expression.MapAccessor in project spring-integration by spring-projects.

the class IntegrationEvaluationContextFactoryBean method getObject.

@Override
public StandardEvaluationContext getObject() throws Exception {
    StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
    if (this.typeLocator != null) {
        evaluationContext.setTypeLocator(this.typeLocator);
    }
    evaluationContext.setBeanResolver(this.beanResolver);
    evaluationContext.setTypeConverter(getTypeConverter());
    for (PropertyAccessor propertyAccessor : getPropertyAccessors().values()) {
        evaluationContext.addPropertyAccessor(propertyAccessor);
    }
    evaluationContext.addPropertyAccessor(new MapAccessor());
    for (Entry<String, Method> functionEntry : getFunctions().entrySet()) {
        evaluationContext.registerFunction(functionEntry.getKey(), functionEntry.getValue());
    }
    return evaluationContext;
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) PropertyAccessor(org.springframework.expression.PropertyAccessor) MapAccessor(org.springframework.context.expression.MapAccessor) Method(java.lang.reflect.Method)

Example 12 with MapAccessor

use of org.springframework.context.expression.MapAccessor in project spring-integration by spring-projects.

the class ExpressionUtils method createEvaluationContext.

/**
 * Create a {@link StandardEvaluationContext} with a {@link MapAccessor} in its
 * property accessor property and the supplied {@link ConversionService} in its
 * conversionService property.
 * @param conversionService the conversion service.
 * @param beanFactory the bean factory.
 * @param simple true if simple.
 * @return the evaluation context.
 */
private static EvaluationContext createEvaluationContext(ConversionService conversionService, BeanFactory beanFactory, boolean simple) {
    if (simple) {
        Builder ecBuilder = SimpleEvaluationContext.forPropertyAccessors(new MapAccessor(), DataBindingPropertyAccessor.forReadOnlyAccess()).withInstanceMethods();
        if (conversionService != null) {
            ecBuilder.withConversionService(conversionService);
        }
        return ecBuilder.build();
    } else {
        StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
        evaluationContext.addPropertyAccessor(new MapAccessor());
        if (conversionService != null) {
            evaluationContext.setTypeConverter(new StandardTypeConverter(conversionService));
        }
        if (beanFactory != null) {
            evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
        }
        return evaluationContext;
    }
}
Also used : StandardTypeConverter(org.springframework.expression.spel.support.StandardTypeConverter) BeanFactoryResolver(org.springframework.context.expression.BeanFactoryResolver) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Builder(org.springframework.expression.spel.support.SimpleEvaluationContext.Builder) MapAccessor(org.springframework.context.expression.MapAccessor)

Example 13 with MapAccessor

use of org.springframework.context.expression.MapAccessor in project spring-integration by spring-projects.

the class ObjectToJsonTransformerTests method testBoonJsonObjectMapper_toNode.

@Test
public void testBoonJsonObjectMapper_toNode() throws Exception {
    ObjectToJsonTransformer transformer = new ObjectToJsonTransformer(new BoonJsonObjectMapper(), ObjectToJsonTransformer.ResultType.NODE);
    TestPerson person = new TestPerson("John", "Doe", 42);
    person.setAddress(new TestAddress(123, "Main Street"));
    Object payload = transformer.transform(new GenericMessage<TestPerson>(person)).getPayload();
    assertThat(payload, instanceOf(Map.class));
    SpelExpressionParser parser = new SpelExpressionParser();
    Expression expression = parser.parseExpression("firstName + ': ' + address.street");
    StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
    evaluationContext.addPropertyAccessor(new MapAccessor());
    String value = expression.getValue(evaluationContext, payload, String.class);
    assertEquals("John: Main Street", value);
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) MapAccessor(org.springframework.context.expression.MapAccessor) Matchers.containsString(org.hamcrest.Matchers.containsString) BoonJsonObjectMapper(org.springframework.integration.support.json.BoonJsonObjectMapper) Map(java.util.Map) Test(org.junit.Test)

Example 14 with MapAccessor

use of org.springframework.context.expression.MapAccessor in project beetl2.0 by javamonkey.

the class SpELFunction method createEvaluationContext.

/**
 * 创建SpEL执行上下文
 *
 * @param rootObject
 *            SpEL表达式根对象
 * @param context
 *            Beetl上下文对象
 * @return SpEL表达式执行上下文
 */
private EvaluationContext createEvaluationContext(Object rootObject, Context beetlContext) {
    StandardEvaluationContext context = new StandardEvaluationContext(rootObject);
    // 允许使用#context访问Beetl上下文
    context.setVariable("context", beetlContext);
    // 允许使用#global访问Beetl上下文的全局变量
    context.setVariable("global", beetlContext.globalVar);
    // 注册WebRender定义的全局变量
    context.setVariable("ctxPath", beetlContext.getGlobal("ctxPath"));
    context.setVariable("servlet", beetlContext.getGlobal("servlet"));
    context.setVariable("parameter", beetlContext.getGlobal("parameter"));
    context.setVariable("request", beetlContext.getGlobal("request"));
    context.setVariable("session", beetlContext.getGlobal("session"));
    // 允许使用属性格式访问Map
    context.addPropertyAccessor(new MapAccessor());
    // 允许访问Spring容器Bean
    context.setBeanResolver(new BeanFactoryResolver(applicationContext));
    return context;
}
Also used : BeanFactoryResolver(org.springframework.context.expression.BeanFactoryResolver) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) MapAccessor(org.springframework.context.expression.MapAccessor)

Example 15 with MapAccessor

use of org.springframework.context.expression.MapAccessor in project spring-framework by spring-projects.

the class EvalTag method createEvaluationContext.

private EvaluationContext createEvaluationContext(PageContext pageContext) {
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.addPropertyAccessor(new JspPropertyAccessor(pageContext));
    context.addPropertyAccessor(new MapAccessor());
    context.addPropertyAccessor(new EnvironmentAccessor());
    context.setBeanResolver(new BeanFactoryResolver(getRequestContext().getWebApplicationContext()));
    ConversionService conversionService = getConversionService(pageContext);
    if (conversionService != null) {
        context.setTypeConverter(new StandardTypeConverter(conversionService));
    }
    return context;
}
Also used : StandardTypeConverter(org.springframework.expression.spel.support.StandardTypeConverter) BeanFactoryResolver(org.springframework.context.expression.BeanFactoryResolver) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) ConversionService(org.springframework.core.convert.ConversionService) MapAccessor(org.springframework.context.expression.MapAccessor) EnvironmentAccessor(org.springframework.context.expression.EnvironmentAccessor)

Aggregations

MapAccessor (org.springframework.context.expression.MapAccessor)20 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)17 HashMap (java.util.HashMap)11 Nullable (javax.annotation.Nullable)7 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)6 Test (org.junit.Test)4 Expression (org.springframework.expression.Expression)4 Map (java.util.Map)3 BeanFactoryResolver (org.springframework.context.expression.BeanFactoryResolver)3 EnvironmentAccessor (org.springframework.context.expression.EnvironmentAccessor)3 Method (java.lang.reflect.Method)2 SpringELExpressionParser (org.springframework.binding.expression.spel.SpringELExpressionParser)2 BeanExpressionContextAccessor (org.springframework.context.expression.BeanExpressionContextAccessor)2 ExpressionParser (org.springframework.expression.ExpressionParser)2 PropertyAccessor (org.springframework.expression.PropertyAccessor)2 SpelParserConfiguration (org.springframework.expression.spel.SpelParserConfiguration)2 ReflectivePropertyAccessor (org.springframework.expression.spel.support.ReflectivePropertyAccessor)2 StandardTypeConverter (org.springframework.expression.spel.support.StandardTypeConverter)2 ActionPropertyAccessor (org.springframework.webflow.expression.spel.ActionPropertyAccessor)2 BeanFactoryPropertyAccessor (org.springframework.webflow.expression.spel.BeanFactoryPropertyAccessor)2