5

I use Spring 3.1.1 and Hibernate-validator 4.3.0.Final and have a problem with changing default MessageInterpolator, which takes validation messages from ValidationMessages (in classpath).

I wanna use ResourceBundleMessageInterpolator which will take messages from my spring messageSource

I did something like this in my application-context.xml:

<bean id="resourceBundleMessageInterpolator"
      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>
<bean id="validator"
      class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <property name="messageInterpolator" ref="resourceBundleMessageInterpolator"/>
</bean>

And when I start my web application in logs I see:

11:04:07,402 DEBUG [org.hibernate.validator.internal.engine.ConfigurationImpl] - 
Setting custom MessageInterpolator of type
 org.springframework.validation.beanvalidation.LocaleContextMessageInterpolator 
11:04:07,402 DEBUG [org.hibernate.validator.internal.engine.ConfigurationImpl] - 
Setting custom ConstraintValidatorFactory of type org.springframework .validation.beanvalidation.SpringConstraintValidatorFactory

So as you can see, it's not ResourceBundleMessageInterpolator which i want. It's LocaleContextMessageInterpolator

Later when I try to validate something I just get the messages from ValidationMessages.properties, not from spring message source:

11:08:09,397 DEBUG [org.hibernate.validator.resourceloading.PlatformResourceBundleLocator] - 
ValidationMessages not found.
11:08:09,413 DEBUG [org.hibernate.validator.resourceloading.PlatformResourceBundleLocator] - 
org.hibernate.validator.ValidationMessages found.

As you can see from application-context.xml I wanna use MessageSourceResourceBundleLocator, but it's used, I don't know why PlatformResourceBundleLocator

Any ideas?

1 Answer 1

15

You don't have to declare ResourceBundleMessageInterpolator and MessageSourceResourceBundleLocator beans by yourself (unless you have to), they are created by LocalValidatorFactoryBean when you supply messageSource:

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <property name="validationMessageSource" ref="messageSource"/>
 </bean>

(It does this under the hood:)

public void setValidationMessageSource(MessageSource messageSource) {
    this.messageInterpolator = HibernateValidatorDelegate.buildMessageInterpolator(messageSource);
}

// (...)


private static class HibernateValidatorDelegate {

    public static MessageInterpolator buildMessageInterpolator(MessageSource messageSource) {
        return new ResourceBundleMessageInterpolator(new MessageSourceResourceBundleLocator(messageSource));
    }
}

So with simplified bean definition, do you get same debug output? Where do you use "validator" ref? You may have to use <mvc:annotation-driven validator="validator"> for example.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot for the answer. Earlier I used, simpler version with setValidationMessageSource, as you wrote. The behavior was the same as I described at first. The matter is that I missed an attribute validator="validator" in my <mvc:annotation-driven/>. I added the attribute and it works.
what to do if I use spring-web module only, not spring-webmvc?
where to use validator ref in annotation based configuration
@karthi Ask new question with your specific problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.