org.springframework.beans.factory.config
public class: CustomEditorConfigurer [javadoc |
source]
java.lang.Object
org.springframework.beans.factory.config.CustomEditorConfigurer
All Implemented Interfaces:
Ordered, BeanFactoryPostProcessor, BeanClassLoaderAware
BeanFactoryPostProcessor implementation that allows for convenient
registration of custom
property editors .
As of Spring 2.0, the recommended usage is to use custom
PropertyEditorRegistrar implementations that in turn register
any desired editors on a given
registry .
Each PropertyEditorRegistrar can register any number of custom editors.
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="propertyEditorRegistrars">
<list>
<bean class="mypackage.MyCustomDateEditorRegistrar"/>
<bean class="mypackage.MyObjectEditorRegistrar"/>
</list>
</property>
</bean>
Alternative configuration example with custom editor classes:
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date" value="mypackage.MyCustomDateEditor"/>
<entry key="mypackage.MyObject" value="mypackage.MyObjectEditor"/>
</map>
</property>
</bean>
Also supports "java.lang.String[]"-style array class names and primitive
class names (e.g. "boolean"). Delegates to ClassUtils for actual
class name resolution.
NOTE: Custom property editors registered with this configurer do
not apply to data binding. Custom editors for data binding need to
be registered on the org.springframework.validation.DataBinder :
Use a common base class or delegate to common PropertyEditorRegistrar
implementations to reuse editor registration there.
Also see:
- java.beans.PropertyEditor
- org.springframework.beans.PropertyEditorRegistrar
- ConfigurableBeanFactory#addPropertyEditorRegistrar
- ConfigurableBeanFactory#registerCustomEditor
- org.springframework.validation.DataBinder#registerCustomEditor
- org.springframework.web.servlet.mvc.BaseCommandController#setPropertyEditorRegistrars
- org.springframework.web.servlet.mvc.BaseCommandController#initBinder
- author:
Juergen - Hoeller
- since:
27.02.2004 -
| Field Summary |
|---|
| protected final Log | logger | |
| Method from org.springframework.beans.factory.config.CustomEditorConfigurer Detail: |
public int getOrder() {
return this.order;
}
|
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
if (this.propertyEditorRegistrars != null) {
for (int i = 0; i < this.propertyEditorRegistrars.length; i++) {
beanFactory.addPropertyEditorRegistrar(this.propertyEditorRegistrars[i]);
}
}
if (this.customEditors != null) {
for (Iterator it = this.customEditors.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
Class requiredType = null;
try {
if (key instanceof Class) {
requiredType = (Class) key;
}
else if (key instanceof String) {
requiredType = ClassUtils.forName((String) key, this.beanClassLoader);
}
else {
throw new IllegalArgumentException(
"Invalid key [" + key + "] for custom editor: needs to be Class or String.");
}
if (value instanceof PropertyEditor) {
beanFactory.registerCustomEditor(requiredType, (PropertyEditor) value);
}
else if (value instanceof Class) {
beanFactory.registerCustomEditor(requiredType, (Class) value);
}
else if (value instanceof String) {
Class editorClass = ClassUtils.forName((String) value, this.beanClassLoader);
beanFactory.registerCustomEditor(requiredType, editorClass);
}
else {
throw new IllegalArgumentException("Mapped value [" + value + "] for custom editor key [" +
key + "] is not of required type [" + PropertyEditor.class.getName() +
"] or a corresponding Class or String value indicating a PropertyEditor implementation");
}
}
catch (ClassNotFoundException ex) {
if (this.ignoreUnresolvableEditors) {
logger.info("Skipping editor [" + value + "] for required type [" + key + "]: " +
(requiredType != null ? "editor" : "required type") + " class not found.");
}
else {
throw new FatalBeanException(
(requiredType != null ? "Editor" : "Required type") + " class not found", ex);
}
}
}
}
}
|
public void setBeanClassLoader(ClassLoader beanClassLoader) {
this.beanClassLoader = beanClassLoader;
}
|
public void setCustomEditors(Map customEditors) {
this.customEditors = customEditors;
}
Specify the custom editors to register via a Map , using the
class name of the required type as the key and the class name of the
associated PropertyEditor as value.
Also supports PropertyEditor instances as values; however,
this is deprecated since Spring 2.0.7 and will be removed in Spring 3.0. |
public void setIgnoreUnresolvableEditors(boolean ignoreUnresolvableEditors) {
this.ignoreUnresolvableEditors = ignoreUnresolvableEditors;
}
Set whether unresolvable editors should simply be skipped.
Default is to raise an exception in such a case.
This typically applies to either the editor class or the required type
class not being found in the classpath. If you expect this to happen in
some deployments and prefer to simply ignore the affected editors,
then switch this flag to "true". |
public void setOrder(int order) {
this.order = order;
}
|
public void setPropertyEditorRegistrars(PropertyEditorRegistrar[] propertyEditorRegistrars) {
this.propertyEditorRegistrars = propertyEditorRegistrars;
}
Specify the PropertyEditorRegistrars
to apply to beans defined within the current application context.
This allows for sharing PropertyEditorRegistrars with
DataBinders , etc.
Furthermore, it avoids the need for synchronization on custom editors:
A PropertyEditorRegistrar will always create fresh editor
instances for each bean creation attempt. |