25
25
import org .springframework .beans .BeansException ;
26
26
import org .springframework .beans .FatalBeanException ;
27
27
import org .springframework .beans .PropertyEditorRegistrar ;
28
+ import org .springframework .beans .PropertyEditorRegistry ;
29
+ import org .springframework .beans .PropertyEditorRegistrySupport ;
28
30
import org .springframework .beans .factory .BeanClassLoaderAware ;
29
31
import org .springframework .core .Ordered ;
30
32
import org .springframework .util .Assert ;
@@ -90,7 +92,7 @@ public class CustomEditorConfigurer implements BeanFactoryPostProcessor, BeanCla
90
92
91
93
private PropertyEditorRegistrar [] propertyEditorRegistrars ;
92
94
93
- private Map <String , String > customEditors ;
95
+ private Map <String , ? > customEditors ;
94
96
95
97
private boolean ignoreUnresolvableEditors = false ;
96
98
@@ -123,10 +125,11 @@ public void setPropertyEditorRegistrars(PropertyEditorRegistrar[] propertyEditor
123
125
* Specify the custom editors to register via a {@link Map}, using the
124
126
* class name of the required type as the key and the class name of the
125
127
* associated {@link PropertyEditor} as value.
126
- * @param customEditors said <code>Map</code> of editors (can be <code>null</code>)
128
+ * <p>Also supports {@link PropertyEditor} instances as values; however,
129
+ * this is deprecated since Spring 2.0.7!
127
130
* @see ConfigurableListableBeanFactory#registerCustomEditor
128
131
*/
129
- public void setCustomEditors (Map <String , String > customEditors ) {
132
+ public void setCustomEditors (Map <String , ? > customEditors ) {
130
133
this .customEditors = customEditors ;
131
134
}
132
135
@@ -156,16 +159,35 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
156
159
}
157
160
158
161
if (this .customEditors != null ) {
159
- for (Map .Entry <String , String > entry : this .customEditors .entrySet ()) {
162
+ for (Map .Entry <String , ? > entry : this .customEditors .entrySet ()) {
160
163
String key = entry .getKey ();
161
- String value = entry .getValue ();
164
+ Object value = entry .getValue ();
162
165
Class requiredType = null ;
163
166
164
167
try {
165
168
requiredType = ClassUtils .forName (key , this .beanClassLoader );
166
- Class editorClass = ClassUtils .forName (value , this .beanClassLoader );
167
- Assert .isAssignable (PropertyEditor .class , editorClass );
168
- beanFactory .registerCustomEditor (requiredType , (Class <? extends PropertyEditor >) editorClass );
169
+ if (value instanceof PropertyEditor ) {
170
+ if (logger .isWarnEnabled ()) {
171
+ logger .warn ("Passing PropertyEditor instances into CustomEditorConfigurer is deprecated: " +
172
+ "use PropertyEditorRegistrars or PropertyEditor class names instead. " +
173
+ "Offending key [" + key + "; offending editor instance: " + value );
174
+ }
175
+ beanFactory .addPropertyEditorRegistrar (
176
+ new SharedPropertyEditorRegistrar (requiredType , (PropertyEditor ) value ));
177
+ }
178
+ else if (value instanceof Class ) {
179
+ beanFactory .registerCustomEditor (requiredType , (Class ) value );
180
+ }
181
+ else if (value instanceof String ) {
182
+ Class editorClass = ClassUtils .forName ((String ) value , this .beanClassLoader );
183
+ Assert .isAssignable (PropertyEditor .class , editorClass );
184
+ beanFactory .registerCustomEditor (requiredType , (Class <? extends PropertyEditor >) editorClass );
185
+ }
186
+ else {
187
+ throw new IllegalArgumentException ("Mapped value [" + value + "] for custom editor key [" +
188
+ key + "] is not of required type [" + PropertyEditor .class .getName () +
189
+ "] or a corresponding Class or String value indicating a PropertyEditor implementation" );
190
+ }
169
191
}
170
192
catch (ClassNotFoundException ex ) {
171
193
if (this .ignoreUnresolvableEditors ) {
@@ -181,4 +203,29 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
181
203
}
182
204
}
183
205
206
+
207
+ /**
208
+ * PropertyEditorRegistrar that registers a (deprecated) shared editor.
209
+ */
210
+ private static class SharedPropertyEditorRegistrar implements PropertyEditorRegistrar {
211
+
212
+ private final Class requiredType ;
213
+
214
+ private final PropertyEditor sharedEditor ;
215
+
216
+ public SharedPropertyEditorRegistrar (Class requiredType , PropertyEditor sharedEditor ) {
217
+ this .requiredType = requiredType ;
218
+ this .sharedEditor = sharedEditor ;
219
+ }
220
+
221
+ public void registerCustomEditors (PropertyEditorRegistry registry ) {
222
+ if (!(registry instanceof PropertyEditorRegistrySupport )) {
223
+ throw new IllegalArgumentException ("Cannot registered shared editor " +
224
+ "on non-PropertyEditorRegistrySupport registry: " + registry );
225
+ }
226
+ ((PropertyEditorRegistrySupport ) registry ).registerSharedEditor (this .requiredType , this .sharedEditor );
227
+ }
228
+ }
229
+
230
+
184
231
}
0 commit comments