Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

hibernate validator - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...