Results 1 to 3 of 3

Thread: How to stop ListableBeanFactory from creating LocalValidatorFactoryBean instance

  1. #1
    Join Date
    Aug 2009
    Posts
    4

    Post How to stop ListableBeanFactory from creating LocalValidatorFactoryBean instance

    Hi, I have a following spring config where I explicitly create LocalValidatorFactoryBean using my own ValidationMessageSource. I have Hibernate Validator 4.1 in my class path.

    Code:
     <bean id="messageSource"
              class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basenames">
                <list>
                    <value>ValidatorMsgID</value>
                </list>
            </property>
        </bean>
    
        <bean id="validator"
              class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
            <property name="validationMessageSource" ref="messageSource"/>
        </bean>
    
        <bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor"/>
    However I noticed that the LocalValidatorFactoryBean is being created twice by hitting a debug in classes "afterPropertiesSet" method. The first time is for the explicite bean I created, however following that the same class is instantiated implicitly again by DefaultListableBeanFactory class - obviously this time with no validationMessageSource. Therefore it seems that when Spring does make use of the LocalValidatorFactoryBean its using the one with the default Hibernates messagesource rather than the one I have specified.

    Ok, looking into this a bit further its seems that this is caused by "mvc:annotation-driven" I have in the spring config! Any pointers would still help
    Any pointers would really help

    thanks
    Last edited by nishant; Feb 19th, 2012 at 05:51 AM.

  2. #2
    Join Date
    Aug 2009
    Posts
    4

    Default

    Ok, I got it sorted eventually by adding the validator attribute to "mvc:annotation-driven". This is how my final spring config looks

    Code:
    <bean id="messageSource"
          class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>com.mycompany.msgs.ValidatorMsgID</value>
            </list>
        </property>
    </bean>
    
    <bean id="validator"
          class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="messageInterpolator">
            <bean class="org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator">
                <constructor-arg index="0">
                    <bean class="org.springframework.validation.beanvalidation.MessageSourceResourceBundleLocator">
                        <constructor-arg index="0" ref="messageSource"/>
                    </bean>
                </constructor-arg>
            </bean>
        </property>
    </bean>
    
    <bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor">
        <property name="validator" ref="validator"/>
    </bean>
    
    <mvc:annotation-driven validator="validator"/>

  3. #3

    Default

    Many thanks nishant, this is the exact solution to a problem I had. Cheers!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •