@@ -65,6 +65,8 @@ public class Binder {
65
65
66
66
private final Consumer <PropertyEditorRegistry > propertyEditorInitializer ;
67
67
68
+ private final BindHandler defaultBindHandler ;
69
+
68
70
/**
69
71
* Create a new {@link Binder} instance for the specified sources. A
70
72
* {@link DefaultFormattingConversionService} will be used for all conversion.
@@ -116,12 +118,32 @@ public Binder(Iterable<ConfigurationPropertySource> sources, PlaceholdersResolve
116
118
*/
117
119
public Binder (Iterable <ConfigurationPropertySource > sources , PlaceholdersResolver placeholdersResolver ,
118
120
ConversionService conversionService , Consumer <PropertyEditorRegistry > propertyEditorInitializer ) {
121
+ this (sources , placeholdersResolver , conversionService , propertyEditorInitializer , null );
122
+ }
123
+
124
+ /**
125
+ * Create a new {@link Binder} instance for the specified sources.
126
+ * @param sources the sources used for binding
127
+ * @param placeholdersResolver strategy to resolve any property placeholders
128
+ * @param conversionService the conversion service to convert values (or {@code null}
129
+ * to use {@link ApplicationConversionService})
130
+ * @param propertyEditorInitializer initializer used to configure the property editors
131
+ * that can convert values (or {@code null} if no initialization is required). Often
132
+ * used to call {@link ConfigurableListableBeanFactory#copyRegisteredEditorsTo}.
133
+ * @param defaultBindHandler the default bind handler to use if non is specified when
134
+ * binding
135
+ * @since 2.2.0
136
+ */
137
+ public Binder (Iterable <ConfigurationPropertySource > sources , PlaceholdersResolver placeholdersResolver ,
138
+ ConversionService conversionService , Consumer <PropertyEditorRegistry > propertyEditorInitializer ,
139
+ BindHandler defaultBindHandler ) {
119
140
Assert .notNull (sources , "Sources must not be null" );
120
141
this .sources = sources ;
121
142
this .placeholdersResolver = (placeholdersResolver != null ) ? placeholdersResolver : PlaceholdersResolver .NONE ;
122
143
this .conversionService = (conversionService != null ) ? conversionService
123
144
: ApplicationConversionService .getSharedInstance ();
124
145
this .propertyEditorInitializer = propertyEditorInitializer ;
146
+ this .defaultBindHandler = (defaultBindHandler != null ) ? defaultBindHandler : BindHandler .DEFAULT ;
125
147
}
126
148
127
149
/**
@@ -254,7 +276,7 @@ public <T> T bindOrCreate(ConfigurationPropertyName name, Bindable<T> target, Bi
254
276
private <T > T bind (ConfigurationPropertyName name , Bindable <T > target , BindHandler handler , boolean create ) {
255
277
Assert .notNull (name , "Name must not be null" );
256
278
Assert .notNull (target , "Target must not be null" );
257
- handler = (handler != null ) ? handler : BindHandler . DEFAULT ;
279
+ handler = (handler != null ) ? handler : this . defaultBindHandler ;
258
280
Context context = new Context ();
259
281
return bind (name , target , handler , context , false , create );
260
282
}
@@ -439,8 +461,22 @@ private boolean containsNoDescendantOf(Iterable<ConfigurationPropertySource> sou
439
461
* @return a {@link Binder} instance
440
462
*/
441
463
public static Binder get (Environment environment ) {
442
- return new Binder (ConfigurationPropertySources .get (environment ),
443
- new PropertySourcesPlaceholdersResolver (environment ));
464
+ return get (environment , null );
465
+ }
466
+
467
+ /**
468
+ * Create a new {@link Binder} instance from the specified environment.
469
+ * @param environment the environment source (must have attached
470
+ * {@link ConfigurationPropertySources})
471
+ * @param defaultBindHandler the default bind handler to use if non is specified when
472
+ * binding
473
+ * @return a {@link Binder} instance
474
+ * @since 2.2.0
475
+ */
476
+ public static Binder get (Environment environment , BindHandler defaultBindHandler ) {
477
+ Iterable <ConfigurationPropertySource > sources = ConfigurationPropertySources .get (environment );
478
+ PropertySourcesPlaceholdersResolver placeholdersResolver = new PropertySourcesPlaceholdersResolver (environment );
479
+ return new Binder (sources , placeholdersResolver , null , null , defaultBindHandler );
444
480
}
445
481
446
482
/**
0 commit comments