问题描述
我正在尝试在hibernate验证4.1中覆盖默认的ResourceBundleLocator。到目前为止它的工作完美,但其使用的唯一例子包括用于实例化ValidationFactory的java代码。
I'm trying to override default ResourceBundleLocator in hibernate validation 4.1. So far it works perfectly, but the only examples of its usage include java code to instantiate ValidationFactory.
当使用带有spring hibernate的web应用程序时,会自动配置验证(仅限于合适的hibernate验证* .jar文件应该存在并自动使用)。如何在该场景中替换ResourceBundleLocator?我没有看到在任何属性或applicationContext.xml文件中规划我的自定义ResourceBundleLocator的方法。
When using a web application with spring hibernate validation is automatically configured (only the suitable hibernate validation *.jar file should exist and it is automatically used). How can i substitute ResourceBundleLocator in that scenario? I do not see any way of specyfing my custom ResourceBundleLocator in any properties or applicationContext.xml file.
推荐答案
这种神奇的方法所需的工作是。
The magic method that does the required job is LocalValidatorFactoryBean#setValidationMessageSource(MessageSource messageSource).
首先,方法的合同: -
First of all, contract of the method:-
注意:此功能需要
类路径上的Hibernate
Validator 4.1或更高版本。您可以使用
不同的验证提供程序但
Hibernate Validator的
ResourceBundleMessageInterpolator
类必须在
配置期间可访问。
NOTE: This feature requires Hibernate Validator 4.1 or higher on the classpath. You may nevertheless use a different validation provider but Hibernate Validator's ResourceBundleMessageInterpolator class must be accessible during configuration.
指定此属性或
messageInterpolator,而不是两者。如果
你想构建一个自定义的
MessageInterpolator,可以考虑从Hibernate Validator的
ResourceBundleMessageInterpolator和
传递
传递
MessageSourceResourceBundleLocator
构造插补器时。
Specify either this property or "messageInterpolator", not both. If you would like to build a custom MessageInterpolator, consider deriving from Hibernate Validator's ResourceBundleMessageInterpolator and passing in a Spring MessageSourceResourceBundleLocator when constructing your interpolator.
您可以通过调用此方法指定自定义message.properties(或.xml)...像这样...
You can specify your custom message.properties(or .xml) by invoking this method... like this...
my-beans.xml
<bean name="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource">
<ref bean="resourceBundleLocator"/>
</property>
</bean>
<bean name="resourceBundleLocator" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>META-INF/validation_errors</value>
</list>
</property>
</bean>
validation_errors.properties
javax.validation.constraints.NotNull.message=MyNotNullMessage
Person.java
class Person {
private String firstName;
private String lastName;
@NotNull
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
BeanValidationTest.java
public class BeanValidationTest {
private static ApplicationContext applicationContext;
@BeforeClass
public static void initialize() {
applicationContext = new ClassPathXmlApplicationContext("classpath:META-INF/spring/webmvc-beans.xml");
Assert.assertNotNull(applicationContext);
}
@Test
public void test() {
LocalValidatorFactoryBean factory = applicationContext.getBean("validator", LocalValidatorFactoryBean.class);
Validator validator = factory.getValidator();
Person person = new Person();
person.setLastName("dude");
Set<ConstraintViolation<Person>> violations = validator.validate(person);
for(ConstraintViolation<Person> violation : violations) {
System.out.println("Custom Message:- " + violation.getMessage());
}
}
}
Outupt: 自定义消息: - MyNotNullMessage
这篇关于Hibernate Validator,自定义ResourceBundleLocator和Spring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!