我正在使用Spring 3.1并使用以下spring配置,我LocalValidatorFactoryBean使用自己的显式创建ValidationMessageSource.我的类路径中有Hibernate Validator 4.1.
<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"/>
Run Code Online (Sandbox Code Playgroud)
但是我注意到,LocalValidatorFactoryBean通过在类afterPropertiesSet方法中调试调试来创建两次.第一次是我在spring配置中定义的explicite bean,但是随后同一个类被类隐式重新实例化DefaultListableBeanFactory- 显然这次没有validationMessageSource.因此,当Spring确实使用LocalValidatorFactoryBean默认Hibernates messagesource而不是我指定的那个时使用它.
好吧,进一步研究它似乎这是由mvc:annotation-driven我在spring配置中引起的.任何指针仍然会有所帮助