Commit 865b7ae4 authored by Phillip Webb's avatar Phillip Webb

Reduce object creation when binding

Use static finals and private instances to reduce the number of objects
created when binding from the `ConfigFileApplicationListener`.

Closes gh-13436
parent 624cd405
...@@ -111,6 +111,8 @@ public class ConfigFileApplicationListener ...@@ -111,6 +111,8 @@ public class ConfigFileApplicationListener
private static final Set<String> NO_SEARCH_NAMES = Collections.singleton(null); private static final Set<String> NO_SEARCH_NAMES = Collections.singleton(null);
private static final Bindable<String[]> STRING_ARRAY = Bindable.of(String[].class);
/** /**
* The "active profiles" property name. * The "active profiles" property name.
*/ */
...@@ -290,6 +292,8 @@ public class ConfigFileApplicationListener ...@@ -290,6 +292,8 @@ public class ConfigFileApplicationListener
private final ConfigurableEnvironment environment; private final ConfigurableEnvironment environment;
private final PropertySourcesPlaceholdersResolver placeholdersResolver;
private final ResourceLoader resourceLoader; private final ResourceLoader resourceLoader;
private final List<PropertySourceLoader> propertySourceLoaders; private final List<PropertySourceLoader> propertySourceLoaders;
...@@ -306,6 +310,8 @@ public class ConfigFileApplicationListener ...@@ -306,6 +310,8 @@ public class ConfigFileApplicationListener
Loader(ConfigurableEnvironment environment, ResourceLoader resourceLoader) { Loader(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {
this.environment = environment; this.environment = environment;
this.placeholdersResolver = new PropertySourcesPlaceholdersResolver(
this.environment);
this.resourceLoader = (resourceLoader != null) ? resourceLoader this.resourceLoader = (resourceLoader != null) ? resourceLoader
: new DefaultResourceLoader(); : new DefaultResourceLoader();
this.propertySourceLoaders = SpringFactoriesLoader.loadFactories( this.propertySourceLoaders = SpringFactoriesLoader.loadFactories(
...@@ -577,10 +583,9 @@ public class ConfigFileApplicationListener ...@@ -577,10 +583,9 @@ public class ConfigFileApplicationListener
return loaded.stream().map((propertySource) -> { return loaded.stream().map((propertySource) -> {
Binder binder = new Binder( Binder binder = new Binder(
ConfigurationPropertySources.from(propertySource), ConfigurationPropertySources.from(propertySource),
new PropertySourcesPlaceholdersResolver(this.environment)); this.placeholdersResolver);
return new Document(propertySource, return new Document(propertySource,
binder.bind("spring.profiles", Bindable.of(String[].class)) binder.bind("spring.profiles", STRING_ARRAY).orElse(null),
.orElse(null),
getProfiles(binder, ACTIVE_PROFILES_PROPERTY), getProfiles(binder, ACTIVE_PROFILES_PROPERTY),
getProfiles(binder, INCLUDE_PROFILES_PROPERTY)); getProfiles(binder, INCLUDE_PROFILES_PROPERTY));
}).collect(Collectors.toList()); }).collect(Collectors.toList());
...@@ -610,7 +615,7 @@ public class ConfigFileApplicationListener ...@@ -610,7 +615,7 @@ public class ConfigFileApplicationListener
} }
private Set<Profile> getProfiles(Binder binder, String name) { private Set<Profile> getProfiles(Binder binder, String name) {
return binder.bind(name, String[].class).map(this::asProfileSet) return binder.bind(name, STRING_ARRAY).map(this::asProfileSet)
.orElse(Collections.emptySet()); .orElse(Collections.emptySet());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment