Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MessageInterpolator in Spring

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?

like image 570
Roman T Avatar asked Jun 27 '12 11:06

Roman T


1 Answers

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.

like image 110
Xaerxess Avatar answered Oct 13 '22 00:10

Xaerxess