Skip to content

Commit e141f77

Browse files
committed
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
1 parent 6cb331e commit e141f77

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,8 @@ private boolean containsNoDescendantOf(Stream<ConfigurationPropertySource> sourc
351351
* @return a {@link Binder} instance
352352
*/
353353
public static Binder get(Environment environment) {
354-
return new Binder(ConfigurationPropertySources.get(environment),
355-
new PropertySourcesPlaceholdersResolver(environment));
354+
return new Binder(ConfigurationPropertySources
355+
.get(environment), new PropertySourcesPlaceholdersResolver(environment));
356356
}
357357

358358
/**

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/convert/BinderConversionService.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,33 @@
3838
*/
3939
public class BinderConversionService implements ConversionService {
4040

41-
private final ConversionService conversionService;
41+
private static final ConversionService additionalConversionService = createAdditionalConversionService();
42+
43+
private static final ConversionService defaultConversionService = new DefaultFormattingConversionService();
4244

43-
private final ConversionService additionalConversionService;
45+
private final ConversionService conversionService;
4446

4547
/**
4648
* Create a new {@link BinderConversionService} instance.
4749
* @param conversionService and option root conversion service
4850
*/
4951
public BinderConversionService(ConversionService conversionService) {
5052
this.conversionService = (conversionService != null ? conversionService
51-
: new DefaultFormattingConversionService());
52-
this.additionalConversionService = createAdditionalConversionService();
53+
: defaultConversionService);
5354
}
5455

5556
@Override
5657
public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
5758
return (this.conversionService != null
5859
&& this.conversionService.canConvert(sourceType, targetType))
59-
|| this.additionalConversionService.canConvert(sourceType, targetType);
60+
|| additionalConversionService.canConvert(sourceType, targetType);
6061
}
6162

6263
@Override
6364
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
6465
return (this.conversionService != null
6566
&& this.conversionService.canConvert(sourceType, targetType))
66-
|| this.additionalConversionService.canConvert(sourceType, targetType);
67+
|| additionalConversionService.canConvert(sourceType, targetType);
6768
}
6869

6970
@Override
@@ -92,7 +93,7 @@ private <T> T callConversionService(Function<ConversionService, T> call) {
9293
private <T> T callAdditionalConversionService(Function<ConversionService, T> call,
9394
RuntimeException cause) {
9495
try {
95-
return call.apply(this.additionalConversionService);
96+
return call.apply(additionalConversionService);
9697
}
9798
catch (ConverterNotFoundException ex) {
9899
throw (cause != null ? cause : ex);

0 commit comments

Comments
 (0)