From e141f77801854fe51c919b86fa42f58535bc6cd8 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 21 Dec 2017 15:23:35 -0800 Subject: [PATCH] Share BinderConversionService with a static Use a single shared static `BinderConversionService` instance for all created binders to save memory and improve performance. Fixes gh-11352 --- .../boot/context/properties/bind/Binder.java | 4 ++-- .../bind/convert/BinderConversionService.java | 15 ++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java index e6ece53ae0..e44bee4121 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java @@ -351,8 +351,8 @@ public class Binder { * @return a {@link Binder} instance */ public static Binder get(Environment environment) { - return new Binder(ConfigurationPropertySources.get(environment), - new PropertySourcesPlaceholdersResolver(environment)); + return new Binder(ConfigurationPropertySources + .get(environment), new PropertySourcesPlaceholdersResolver(environment)); } /** diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/convert/BinderConversionService.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/convert/BinderConversionService.java index d1a86c613d..dec6824076 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/convert/BinderConversionService.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/convert/BinderConversionService.java @@ -38,9 +38,11 @@ import org.springframework.format.support.DefaultFormattingConversionService; */ public class BinderConversionService implements ConversionService { - private final ConversionService conversionService; + private static final ConversionService additionalConversionService = createAdditionalConversionService(); + + private static final ConversionService defaultConversionService = new DefaultFormattingConversionService(); - private final ConversionService additionalConversionService; + private final ConversionService conversionService; /** * Create a new {@link BinderConversionService} instance. @@ -48,22 +50,21 @@ public class BinderConversionService implements ConversionService { */ public BinderConversionService(ConversionService conversionService) { this.conversionService = (conversionService != null ? conversionService - : new DefaultFormattingConversionService()); - this.additionalConversionService = createAdditionalConversionService(); + : defaultConversionService); } @Override public boolean canConvert(Class sourceType, Class targetType) { return (this.conversionService != null && this.conversionService.canConvert(sourceType, targetType)) - || this.additionalConversionService.canConvert(sourceType, targetType); + || additionalConversionService.canConvert(sourceType, targetType); } @Override public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) { return (this.conversionService != null && this.conversionService.canConvert(sourceType, targetType)) - || this.additionalConversionService.canConvert(sourceType, targetType); + || additionalConversionService.canConvert(sourceType, targetType); } @Override @@ -92,7 +93,7 @@ public class BinderConversionService implements ConversionService { private T callAdditionalConversionService(Function call, RuntimeException cause) { try { - return call.apply(this.additionalConversionService); + return call.apply(additionalConversionService); } catch (ConverterNotFoundException ex) { throw (cause != null ? cause : ex);