Spring 3 has such a nice feature as type conversion. It provides a converter SPI(Converter<S, T>) to be used to implement differenet conversion logic.
The subclass of Converter type allow to define one-way conversion(only from S to T), so if I want a conversion also to be performed from T to S I need to define another converter class that implement Converter<T, S>. If I have many classes which are subject to conversion, i need to define many converters.
Is there any posibility to define two-way conversion logic(from S to T and from T to S) in one converter? and how it will be used?
PS. now I’m using my converters via ConversionServiceFactoryBean defining/injecting them in configuration file
You are correct, if you want to use the
org.springframework.core.convert.converter.Converterinterface directly, you’ll need to implement two converters, one for each direction.But spring 3 has a couple of other options:
If your conversion is not object-to-object but rather object-to-string (and back), then you can implement a
org.springframework.format.Formatterinstead. Formatters get registered as GenericConverters (see http://static.springsource.org/spring-webflow/docs/2.3.x/reference/html/ch05s07.html#converter-upgrade-to-spring-3)Otherwise you could implement your own
org.springframework.core.convert.converter.GenericConverter, which makes it easy to create TwoWayConverter implementations using reflection.