Search in sources :

Example 6 with StandardTypeConverter

use of org.springframework.expression.spel.support.StandardTypeConverter in project spring-framework by spring-projects.

the class StandardBeanExpressionResolver method evaluate.

@Override
@Nullable
public Object evaluate(@Nullable String value, BeanExpressionContext evalContext) throws BeansException {
    if (!StringUtils.hasLength(value)) {
        return value;
    }
    try {
        Expression expr = this.expressionCache.get(value);
        if (expr == null) {
            expr = this.expressionParser.parseExpression(value, this.beanExpressionParserContext);
            this.expressionCache.put(value, expr);
        }
        StandardEvaluationContext sec = this.evaluationCache.get(evalContext);
        if (sec == null) {
            sec = new StandardEvaluationContext(evalContext);
            sec.addPropertyAccessor(new BeanExpressionContextAccessor());
            sec.addPropertyAccessor(new BeanFactoryAccessor());
            sec.addPropertyAccessor(new MapAccessor());
            sec.addPropertyAccessor(new EnvironmentAccessor());
            sec.setBeanResolver(new BeanFactoryResolver(evalContext.getBeanFactory()));
            sec.setTypeLocator(new StandardTypeLocator(evalContext.getBeanFactory().getBeanClassLoader()));
            sec.setTypeConverter(new StandardTypeConverter(() -> {
                ConversionService cs = evalContext.getBeanFactory().getConversionService();
                return (cs != null ? cs : DefaultConversionService.getSharedInstance());
            }));
            customizeEvaluationContext(sec);
            this.evaluationCache.put(evalContext, sec);
        }
        return expr.getValue(sec);
    } catch (Throwable ex) {
        throw new BeanExpressionException("Expression parsing failed", ex);
    }
}
Also used : StandardTypeConverter(org.springframework.expression.spel.support.StandardTypeConverter) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) BeanExpressionException(org.springframework.beans.factory.BeanExpressionException) StandardTypeLocator(org.springframework.expression.spel.support.StandardTypeLocator) Expression(org.springframework.expression.Expression) ConversionService(org.springframework.core.convert.ConversionService) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) Nullable(org.springframework.lang.Nullable)

Example 7 with StandardTypeConverter

use of org.springframework.expression.spel.support.StandardTypeConverter 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

StandardTypeConverter (org.springframework.expression.spel.support.StandardTypeConverter)7 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)5 ConversionService (org.springframework.core.convert.ConversionService)4 BeanFactoryResolver (org.springframework.context.expression.BeanFactoryResolver)3 Test (org.junit.jupiter.api.Test)2 MapAccessor (org.springframework.context.expression.MapAccessor)2 GenericConversionService (org.springframework.core.convert.support.GenericConversionService)2 StandardTypeLocator (org.springframework.expression.spel.support.StandardTypeLocator)2 Method (java.lang.reflect.Method)1 Time (java.sql.Time)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 PostConstruct (javax.annotation.PostConstruct)1 BeanExpressionException (org.springframework.beans.factory.BeanExpressionException)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 ApplicationContext (org.springframework.context.ApplicationContext)1 BeanFactoryAccessor (org.springframework.context.expression.BeanFactoryAccessor)1 EnvironmentAccessor (org.springframework.context.expression.EnvironmentAccessor)1 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)1 DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)1