Announcement Announcement Module
Collapse
No announcement yet.
ConversionService load order problem? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ConversionService load order problem?

    Hi,

    I'm having some issues getting my ConversionService to be invoked to convert property values of other beans in my application context configured through XML. Depending on the dependencies between beans, they get loaded in different order and we introduced a dependency between two beans that makes it that the ConversionService is not yet loaded when a property of a bean gets set. Specifically, I'm using Joda's DateTimeZone object as a property type and I'm trying to configure a time zone with a string in a bean property.

    My ConversionService is configured like so:

    Code:
    <mvc:annotation-driven conversion-service="conversionService" />
    
    <bean id="conversionService" class="com.somecompany.spring.context.support.ConversionServiceFactoryBean">
    	<property name="formatterRegistrars">
    		<list>
    			<!-- This guy registers a converters for Joda objects, including DateTimeZone -->
    			<bean class="com.somecompany.commons.spring.format.datetime.joda.JodaTimeFormatterRegistrar" />
    		</list>
    	</property>
    </bean>

    Then I have bean that looks like this:

    Code:
    <bean id="someBean" class="com.somecompany.SomeBean">
    	<property name="someTimeZone" value="America/New_York" />
    </bean>
    This works if someBean is instantiated after the conversionService is registered by Spring MVC. If somehow it gets referred to before, it doesn't and I have the following exception:

    Code:
    java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.joda.time.DateTimeZone] for property 'someTimeZone': no matching editors or conversion strategy found
    After debugging when it works vs when it doesn't, I found the following: when it works, the convertIfNecessary method of a org.springframework.beans.TypeConverterDelegate is invoked. It uses a org.springframework.beans.PropertyEditorRegistrySu pport to get a hold of the ConversionService to use. If one is found, it uses the ConversionService to convert the value, otherwise it reverts to the default property editor strategy. If we mess around with bean dependencies and someBean is instantiated before the conversionService, the conversionService associated with the propertyEditorRegistry of the TypeConverterDelegate is null. I tried adding a "depends-on='conversionService'" somewhere along the away to guarantee that the conversionService is instantiated before someBean, and it is, but even then the conversionService on the propertyEditorRegistry of the TypeConverterDelegate is still null. When we let Spring MVC register the conversionService, its reference on the propertyEditorRegistry of the TypeConverterDelegate is not null.

    Did anyone ever run into this problem?

    Thank you very much for your help.

  • #2
    The ConversionService is configured in the BeanFactoy before singleton instantiations , in method AbstractAppplicationContext.finishBeanFactoryIniti alization().

    So you don't really need the depends-on attribute on beans.

    May be you can break at this point to see why the ConversionService was not configured.

    Regards

    Comment

    Working...
    X