Home > Java > Property editors

Property editors

In Java, property editors are seldom used. Originally, they were meant for Swing applications to transform between model objects and their string representations on the GUI.

Spring found them another use: it’s typically what needs to be done when parsing a Spring beans definition file in order to create beans in the factory. Have you Spring users noticed you can set a number to a property and it is taken as such?

It’s because in every bean factory, there’re some editors registered by default. In fact, there are three kinds of editors:

  1. The first level consist of those default editors.
  2. If you need to go further, you can register editors provided by the Spring framework.
  3. Finally, you can also create your own editors.

The list of editors built-in, pre-registeredor not, is available in the Spring documentation.

For example, the LocaleEditor is built-in and to use it, we only have to provide the correct locale string representation like so:

<bean id="dummy" class="ch.frankel.blog.spring.propertyeditor.Dummy">
	<property name="locale" value="fr_FR" />
</bean>

Interestingly enough, some other editors are not well-known, like the PatternEditor, although they are registered by default.

In order to register other property editors, whether built-in or homemade, just configure the customEditors map property of the org.springframework.beans.factory.config.CustomEditorConfigurer bean. Note that the latter can safely remain anonymous.

The following configuration snippet adds a date property editor so that dates can easily be configured in beans definitions files.

<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
  <property name="customEditors">
    <map>
      <entry key="java.util.Date">
        <bean class="org.springframework.beans.propertyeditors.CustomDateEditor">
          <constructor-arg index="0">
            <bean class="java.text.SimpleDateFormat">
              <constructor-arg  value="dd.MM.yyyy" />
            </bean>
          </constructor-arg>
          <constructor-arg index="1" value="false" />
        </bean>
      </entry>
    </map>
  </property>
</bean>

Note that keys in the custom editors map are the wanted object type, meaning there can be only a single editor for each type – a good thing if there’s one.

Sources for this article can be found here in Eclipse/Maven format.

email
Categories: Java Tags: ,
  1. July 17th, 2011 at 23:04 | #1

    Hi Nicolas,

    nice blog post! There’s a few things to mention regarding PropertyEditor usage and Spring. The first thing is: we recommend using Converter implementations (available since Spring 3.0) over PropertyEditors. This is mostly due to the fact that PropertyEditors have a huge problem: they’re stateful. This why we in turn don’t recommend the configuration style (defining a bean instance of a property editor) you showed here. If the Map entry’s value configured for CustomEditorConfigurer’s customEditors is a PropertyEditor instance Spring will have to synchronize every acces to this bean instance. So usually for a very simple case you can simply configure a plain class as Map value and Spring is then able to create PropertyEditor instances without any need for synchronization.

    However, this requires the PropertyEditor implementation to have a no-arg constructor and not requiring any further configuration. In case you need control over PropertyEditor instance creation have a look at PropertyEditorRegistrar which provides some kind of callback mechanism to create PropertyEditor instances. All of the stuff I just mentioned should be explained in the Javadocs of CustomEditorConfigurer as well.

    But as I mentioned right in the beginning: no need to deal with all these nasty details by using the Converter framework ;) .

    Cheers,
    Ollie

  2. July 18th, 2011 at 11:08 | #2

    Hi Oliver,

    Thanks for your continued interest in my articles and for this piece of info.

    I’ll check the Converter framework in the following weeks.

  3. khong07
    January 3rd, 2012 at 13:33 | #3

    Hi oliver, nicolas,

    Could you give me an plain example with converter or propertyeditorregistar callback?

    Because actually, i use stringarraypropertyeditor to convert string property to array with custom delimiter (so need constructor arg in stringarraypropertyeditor)

    Many thanks.
    Khong07

  1. No trackbacks yet.