diff --git a/spring-beans/src/main/java/org/springframework/beans/PropertyEditorRegistrar.java b/spring-beans/src/main/java/org/springframework/beans/PropertyEditorRegistrar.java index 69e2a68b3e..f96972e812 100644 --- a/spring-beans/src/main/java/org/springframework/beans/PropertyEditorRegistrar.java +++ b/spring-beans/src/main/java/org/springframework/beans/PropertyEditorRegistrar.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,4 +45,18 @@ public interface PropertyEditorRegistrar { */ void registerCustomEditors(PropertyEditorRegistry registry); + /** + * Indicate whether this registrar exclusively overrides default editors + * rather than registering custom editors, intended to be applied lazily. + *
This has an impact on registrar handling in a bean factory: see
+ * {@link org.springframework.beans.factory.config.ConfigurableBeanFactory#addPropertyEditorRegistrar}.
+ * @since 6.2.3
+ * @see PropertyEditorRegistry#registerCustomEditor
+ * @see PropertyEditorRegistrySupport#overrideDefaultEditor
+ * @see PropertyEditorRegistrySupport#setDefaultEditorRegistrar
+ */
+ default boolean overridesDefaultEditors() {
+ return false;
+ }
+
}
diff --git a/spring-beans/src/main/java/org/springframework/beans/PropertyEditorRegistrySupport.java b/spring-beans/src/main/java/org/springframework/beans/PropertyEditorRegistrySupport.java
index 1ec21c582d..dc07ebd707 100644
--- a/spring-beans/src/main/java/org/springframework/beans/PropertyEditorRegistrySupport.java
+++ b/spring-beans/src/main/java/org/springframework/beans/PropertyEditorRegistrySupport.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2023 the original author or authors.
+ * Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -98,6 +98,8 @@ public class PropertyEditorRegistrySupport implements PropertyEditorRegistry {
private boolean configValueEditorsActive = false;
+ private @Nullable PropertyEditorRegistrar defaultEditorRegistrar;
+
@SuppressWarnings("NullAway.Init")
private Map This is expected to be a collaborator with {@link PropertyEditorRegistrySupport},
+ * downcasting the given {@link PropertyEditorRegistry} accordingly and calling
+ * {@link #overrideDefaultEditor} for registering additional default editors on it.
+ * @param registrar the registrar to call when default editors are actually needed
+ * @since 6.2.3
+ * @see #overrideDefaultEditor
+ */
+ public void setDefaultEditorRegistrar(PropertyEditorRegistrar registrar) {
+ this.defaultEditorRegistrar = registrar;
+ }
+
/**
* Override the default editor for the specified type with the given property editor.
* Note that this is different from registering a custom editor in that the editor
@@ -176,6 +191,9 @@ public class PropertyEditorRegistrySupport implements PropertyEditorRegistry {
if (!this.defaultEditorsActive) {
return null;
}
+ if (this.overriddenDefaultEditors == null && this.defaultEditorRegistrar != null) {
+ this.defaultEditorRegistrar.registerCustomEditors(this);
+ }
if (this.overriddenDefaultEditors != null) {
PropertyEditor editor = this.overriddenDefaultEditors.get(requiredType);
if (editor != null) {
diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/ConfigurableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/ConfigurableBeanFactory.java
index cfb1128153..f254ebc49d 100644
--- a/spring-beans/src/main/java/org/springframework/beans/factory/config/ConfigurableBeanFactory.java
+++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/ConfigurableBeanFactory.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2024 the original author or authors.
+ * Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -179,7 +179,11 @@ public interface ConfigurableBeanFactory extends HierarchicalBeanFactory, Single
* on the given registry, fresh for each bean creation attempt. This avoids
* the need for synchronization on custom editors; hence, it is generally
* preferable to use this method instead of {@link #registerCustomEditor}.
+ * If the given registrar implements
+ * {@link PropertyEditorRegistrar#overridesDefaultEditors()} to return {@code true},
+ * it will be applied lazily (only when default editors are actually needed).
* @param registrar the PropertyEditorRegistrar to register
+ * @see PropertyEditorRegistrar#overridesDefaultEditors()
*/
void addPropertyEditorRegistrar(PropertyEditorRegistrar registrar);
diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java
index 64d89ab6f7..692571f5f0 100644
--- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java
+++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2024 the original author or authors.
+ * Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -133,6 +133,9 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
/** Spring ConversionService to use instead of PropertyEditors. */
private @Nullable ConversionService conversionService;
+ /** Default PropertyEditorRegistrars to apply to the beans of this factory. */
+ private final Set