org.springframework.batch.item.file.mapping
Class BeanWrapperFieldSetMapper

java.lang.Object
  extended by org.springframework.batch.support.DefaultPropertyEditorRegistrar
      extended by org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper
All Implemented Interfaces:
FieldSetMapper, BeanFactoryAware, InitializingBean, PropertyEditorRegistrar

public class BeanWrapperFieldSetMapper
extends DefaultPropertyEditorRegistrar
implements FieldSetMapper, BeanFactoryAware, InitializingBean

FieldSetMapper implementation based on bean property paths. The DefaultFieldSet to be mapped should have field name meta data corresponding to bean property paths in a prototype instance of the desired type. The prototype instance is initialized either by referring to to object by bean name in the enclosing BeanFactory, or by providing a class to instantiate reflectively.
Nested property paths, including indexed properties in maps and collections, can be referenced by the DefaultFieldSet names. They will be converted to nested bean properties inside the prototype. The DefaultFieldSet 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.
Property name matching is "fuzzy" in the sense that it tolerates close matches, as long as the match is unique. For instance:

The algorithm used to match a property name is to start with an exact match and then search successively through more distant matches until precisely one match is found. If more than one match is found there will be an error.

Author:
Dave Syer

Constructor Summary
BeanWrapperFieldSetMapper()
           
 
Method Summary
 void afterPropertiesSet()
          Check that precisely one of type or prototype bean name is specified.
protected  DataBinder createBinder(Object target)
          Create a binder for the target object.
protected  void initBinder(DataBinder binder)
          Initialize a new binder instance.
 Object mapLine(FieldSet fs)
          Map the DefaultFieldSet 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(BeanFactory beanFactory)
           
 void setPrototypeBeanName(String name)
          The bean name (id) for an object that can be populated from the field set that will be passed into mapLine(FieldSet).
 void setTargetType(Class type)
          Public setter for the type of bean to create instead of using a prototype bean.
 
Methods inherited from class org.springframework.batch.support.DefaultPropertyEditorRegistrar
registerCustomEditors, setCustomEditors
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BeanWrapperFieldSetMapper

public BeanWrapperFieldSetMapper()
Method Detail

setBeanFactory

public void setBeanFactory(BeanFactory beanFactory)
Specified by:
setBeanFactory in interface BeanFactoryAware

setPrototypeBeanName

public void setPrototypeBeanName(String name)
The bean name (id) for an object that can be populated from the field set that will be passed into mapLine(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.

Parameters:
name - the name of a prototype bean in the enclosing BeanFactory

setTargetType

public void setTargetType(Class type)
Public setter for the type of bean to create instead of using a prototype bean. An object of this type will be created from its default constructor for every call to mapLine(FieldSet).
Either this property or the prototype bean name must be specified, but not both.

Parameters:
type - the type to set

afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Check that precisely one of type or prototype bean name is specified.

Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
IllegalStateException - if neither is set or both properties are set.
Exception
See Also:
InitializingBean.afterPropertiesSet()

mapLine

public Object mapLine(FieldSet fs)
Map the DefaultFieldSet to an object retrieved from the enclosing Spring context, or to a new instance of the required type if no prototype is available.

Specified by:
mapLine in interface FieldSetMapper
Parameters:
fs - the FieldSet to map
Throws:
NotWritablePropertyException - if the DefaultFieldSet contains a field that cannot be mapped to a bean property.
BindingException - if there is a type conversion or other error (if the DataBinder from createBinder(Object) has errors after binding).
See Also:
FieldSetMapper.mapLine(org.springframework.batch.item.file.mapping.FieldSet)

createBinder

protected DataBinder createBinder(Object target)
Create a binder for the target object. The binder will then be used to bind the properties form a field set into the target object. This implementation creates a new DataBinder and calls out to initBinder(DataBinder) and DefaultPropertyEditorRegistrar.registerCustomEditors(PropertyEditorRegistry).

Parameters:
target -
Returns:
a DataBinder that can be used to bind properties to the target.

initBinder

protected void initBinder(DataBinder binder)
Initialize a new binder instance. This hook allows customization of binder settings such as the direct field access. Called by createBinder(Object).

Note that registration of custom property editors should be done in DefaultPropertyEditorRegistrar.registerCustomEditors(PropertyEditorRegistry), not here! This method will only be called when a new data binder is created.

Parameters:
binder - new binder instance
See Also:
createBinder(Object)


Copyright © 2009 SpringSource. All Rights Reserved.