MessageInterpolator in Spring

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

I want to use a ResourceBundleMessageInterpolator that will receive messages from my Spring messageSource

I did something similar 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 run my web application in the 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, this is not the ResourceBundleMessageInterpolator that I want. This is LocaleContextMessageInterpolator

Later, when I try to check something, I just get messages from ValidationMessages.properties, and not from the 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.

application-context.xml MessageSourceResourceBundleLocator, , , PlatformResourceBundleLocator

?

+5
1

ResourceBundleMessageInterpolator MessageSourceResourceBundleLocator beans ( ), LocalValidatorFactoryBean messageSource:

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

( :)

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));
    }
}

, bean ? ""? , <mvc:annotation-driven validator="validator">, .

+15

All Articles