public class BeanWrapperFieldSetMapper<T> extends DefaultPropertyEditorRegistrar implements FieldSetMapper<T>, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.InitializingBean
FieldSetMapper
implementation based on bean property paths. The
FieldSet
to be mapped should have field name meta data corresponding
to bean property paths in an instance of the desired type. The instance is
created and initialized either by referring to a prototype object by bean
name in the enclosing BeanFactory, or by providing a class to instantiate
reflectively.FieldSet
names. They will be converted to
nested bean properties inside the prototype. The FieldSet
and the
prototype are thus tightly coupled by the fields that are available and those
that can be initialized. If some of the nested properties are optional (e.g.
collection members) they need to be removed by a post processor.FieldSet
values are converted to the
desired type for injecting into the prototype there are several choices. You
can inject PropertyEditor
instances directly through the
customEditors
property, or you can override
the createBinder(Object)
and initBinder(DataBinder)
methods, or you can provide a custom FieldSet
implementation.
You can also use a ConversionService
to convert to the desired type
through the conversionService
property.
Constructor and Description |
---|
BeanWrapperFieldSetMapper() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Check that precisely one of type or prototype bean name is specified.
|
protected org.springframework.validation.DataBinder |
createBinder(java.lang.Object target)
Create a binder for the target object.
|
protected void |
initBinder(org.springframework.validation.DataBinder binder)
Initialize a new binder instance.
|
T |
mapFieldSet(FieldSet fs)
Map the
FieldSet to an object retrieved from the enclosing Spring
context, or to a new instance of the required type if no prototype is
available. |
void |
setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory) |
void |
setConversionService(org.springframework.core.convert.ConversionService conversionService)
Public setter for the 'conversionService' property.
|
void |
setCustomEditors(java.util.Map<? extends java.lang.Object,? extends java.beans.PropertyEditor> customEditors)
Specify the
custom editors to register. |
void |
setDistanceLimit(int distanceLimit)
The maximum difference that can be tolerated in spelling between input
key names and bean property names.
|
void |
setPrototypeBeanName(java.lang.String name)
The bean name (id) for an object that can be populated from the field set
that will be passed into
mapFieldSet(FieldSet) . |
void |
setStrict(boolean strict)
Public setter for the 'strict' property.
|
void |
setTargetType(java.lang.Class<? extends T> type)
Public setter for the type of bean to create instead of using a prototype
bean.
|
registerCustomEditors
public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory)
setBeanFactory
in interface org.springframework.beans.factory.BeanFactoryAware
public void setDistanceLimit(int distanceLimit)
distanceLimit
- the distance limit to setpublic void setPrototypeBeanName(java.lang.String name)
mapFieldSet(FieldSet)
. Typically a
prototype scoped bean so that a new instance is returned for each field
set mapped.
Either this property or the type property must be specified, but not
both.name
- the name of a prototype bean in the enclosing BeanFactorypublic void setTargetType(java.lang.Class<? extends T> type)
mapFieldSet(FieldSet)
.type
- the type to setpublic void afterPropertiesSet() throws java.lang.Exception
afterPropertiesSet
in interface org.springframework.beans.factory.InitializingBean
java.lang.IllegalStateException
- if neither is set or both properties are
set.java.lang.Exception
InitializingBean.afterPropertiesSet()
public T mapFieldSet(FieldSet fs) throws org.springframework.validation.BindException
FieldSet
to an object retrieved from the enclosing Spring
context, or to a new instance of the required type if no prototype is
available.mapFieldSet
in interface FieldSetMapper<T>
fs
- the FieldSet
to maporg.springframework.validation.BindException
- if there is a type conversion or other error (if
the DataBinder
from createBinder(Object)
has errors
after binding).org.springframework.beans.NotWritablePropertyException
- if the FieldSet
contains a
field that cannot be mapped to a bean property.FieldSetMapper.mapFieldSet(FieldSet)
protected org.springframework.validation.DataBinder createBinder(java.lang.Object target)
DataBinder
and calls out to
initBinder(DataBinder)
and
DefaultPropertyEditorRegistrar.registerCustomEditors(PropertyEditorRegistry)
.target
- Object to bind toDataBinder
that can be used to bind properties to the
target.protected void initBinder(org.springframework.validation.DataBinder binder)
direct field access
. Called by createBinder(Object)
.
Note that registration of custom property editors can be done in
DefaultPropertyEditorRegistrar.registerCustomEditors(PropertyEditorRegistry)
.
binder
- new binder instancecreateBinder(Object)
public void setStrict(boolean strict)
mapFieldSet(FieldSet)
will fail of the FieldSet contains fields
that cannot be mapped to the bean.strict
- indicatorpublic void setConversionService(org.springframework.core.convert.ConversionService conversionService)
createBinder(Object)
will use it if not null.conversionService
- ConversionService
to be used for type conversionspublic void setCustomEditors(java.util.Map<? extends java.lang.Object,? extends java.beans.PropertyEditor> customEditors)
custom editors
to register.setCustomEditors
in class DefaultPropertyEditorRegistrar
customEditors
- a map of Class to PropertyEditor (or class name to
PropertyEditor).CustomEditorConfigurer.setCustomEditors(Map)