Proper ty Editors Provided by Spring


JavaBeans property editors, as mentioned in the last chapter when explaining dependency resolution, are the basis for Spring's ability to automatically convert string values to complex object types in bean factory and application context definition. Additionally, they are heavily used for form binding in the Spring MVC web UI layer. Knowing which PropertyEditors are used automatically, as well as which others are also available for use, is valuable in using Spring effectively.

A class called BeanWrapperImpl, implementing the BeanWrapper interface, is the basic engine in Spring for performing PropertyEditor-based conversions in the containers and web MVC layer. BeanWrapper automatically registers and uses a number of property editors:

  • ByteArrayPropertyEditor: Two-way conversion between a string and its corresponding byte representation.

  • ClassEditor: Two-way conversion of a string class name to an instance of java.lang.Class.

  • CustomBooleanEditor: Customizable two-way conversion between a string and a Boolean value. While this is registered by default, it can be overridden to be appropriate for a particular locale by registering a custom instance of it as a custom editor.

  • CustomNumberEditor: Customizable two-way conversion of a string to any of the subclasses of Number, including Integer, Long, Float, and Double. While this is registered by default, it can be overridden by registering a custom instance of it as a custom editor.

  • FileEditor: Two-way conversion of a string to a File object.

  • InputStreamEditor: Conversion of a string to an InputStream object, via an intermediate Resource and ResourceEditor. Note that the user must close the stream.

  • LocaleEditor: Two-way conversion of a string to a Locale object. See the JavaDoc for exact string format.

  • PropertiesEditor: Two-way conversion of a string to a Properties object. See JavaDoc for exact string format.

  • ResourceEditor: Convert location strings to Resource objects. This is actually not manually registered but is found automatically by the JavaBeans support code, as it is in the same package as the Resource class.

  • ResourceArrayPropertyEditor: Convert wildcard location strings (for example, file: c:/my*.txt or classpath*:myfile.txt) to Resource arrays. This is not registered by BeanWrapper itself, but only by application contexts.

  • StringArrayPropertyEditor: Two-way conversion of a string containing comma-delimited items to a String[].

  • URLEditor: Two-way conversion of a string to a URL object.

Application contexts, but not bean factories, override the normal instances of InputStreamEditor, ResourceEditor, and URLEditor with customized versions, which behave appropriately for the context type in terms of resource resolution. With a ClasspathApplicationContext, for example, location strings would default to resolving as classpath resources.

Two additional property editors are available to be registered (as described in Chapter 2 in the section "Creating a Custom PropertyEditor") and used as needed by users:

  • CustomDateEditor: Customizable two-way conversion of a string to a Date object. May be registered as a custom editor as needed. An example for this class was shown in the last chapter.

  • StringTrimmedEditor: PropertyEditor that trims Strings. Optionally allows transforming an empty string into a null value.



Professional Java Development with the Spring Framework
Professional Java Development with the Spring Framework
ISBN: 0764574833
EAN: 2147483647
Year: 2003
Pages: 188

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net