Skip to content

Commit

Permalink
Avoid early ConversionService determination in StandardBeanExpression…
Browse files Browse the repository at this point in the history
…Resolver

Closes spring-projectsgh-27446
  • Loading branch information
jhoeller committed Sep 21, 2021
1 parent 49d0038 commit 0dc5d27
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@
import org.springframework.beans.factory.config.BeanExpressionContext;
import org.springframework.beans.factory.config.BeanExpressionResolver;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.ParserContext;
Expand Down Expand Up @@ -156,10 +157,10 @@ public Object evaluate(@Nullable String value, BeanExpressionContext evalContext
sec.addPropertyAccessor(new EnvironmentAccessor());
sec.setBeanResolver(new BeanFactoryResolver(evalContext.getBeanFactory()));
sec.setTypeLocator(new StandardTypeLocator(evalContext.getBeanFactory().getBeanClassLoader()));
ConversionService conversionService = evalContext.getBeanFactory().getConversionService();
if (conversionService != null) {
sec.setTypeConverter(new StandardTypeConverter(conversionService));
}
sec.setTypeConverter(new StandardTypeConverter(() -> {
ConversionService cs = evalContext.getBeanFactory().getConversionService();
return (cs != null ? cs : DefaultConversionService.getSharedInstance());
}));
customizeEvaluationContext(sec);
this.evaluationCache.put(evalContext, sec);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,8 @@

package org.springframework.expression.spel.support;

import java.util.function.Supplier;

import org.springframework.core.convert.ConversionException;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
Expand All @@ -37,15 +39,15 @@
*/
public class StandardTypeConverter implements TypeConverter {

private final ConversionService conversionService;
private final Supplier<ConversionService> conversionService;


/**
* Create a StandardTypeConverter for the default ConversionService.
* @see DefaultConversionService#getSharedInstance()
*/
public StandardTypeConverter() {
this.conversionService = DefaultConversionService.getSharedInstance();
this.conversionService = DefaultConversionService::getSharedInstance;
}

/**
Expand All @@ -54,20 +56,30 @@ public StandardTypeConverter() {
*/
public StandardTypeConverter(ConversionService conversionService) {
Assert.notNull(conversionService, "ConversionService must not be null");
this.conversionService = () -> conversionService;
}

/**
* Create a StandardTypeConverter for the given ConversionService.
* @param conversionService a Supplier for the ConversionService to delegate to
* @since 5.3.11
*/
public StandardTypeConverter(Supplier<ConversionService> conversionService) {
Assert.notNull(conversionService, "Supplier must not be null");
this.conversionService = conversionService;
}


@Override
public boolean canConvert(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.conversionService.canConvert(sourceType, targetType);
return this.conversionService.get().canConvert(sourceType, targetType);
}

@Override
@Nullable
public Object convertValue(@Nullable Object value, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType) {
try {
return this.conversionService.convert(value, sourceType, targetType);
return this.conversionService.get().convert(value, sourceType, targetType);
}
catch (ConversionException ex) {
throw new SpelEvaluationException(ex, SpelMessage.TYPE_CONVERSION_ERROR,
Expand Down

0 comments on commit 0dc5d27

Please sign in to comment.