我正在尝试将Spring的ReloadableResourceBundleMessageSource用于LocalValidatorFactoryBean,这样当我更新错误消息时,它应该反映出来而不需要重新启动服务器。我使用的是Spring 4.1.4,hibernate-validator 4.3.2.Final。 以下是代码详细信息 -
context.xml -
<mvc:annotation-driven validator="validator" />
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>file:../conf/fileapplication</value> <!-- Messages here will override the below properties file-->
<value>/WEB-INF/application</value>
</list>
</property>
<property name="cacheSeconds" value="10"></property> <!-- Will check for refresh every 10 seconds -->
</bean>
<bean name="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource">
<ref bean="messageSource"/>
</property>
</bean>
模型 -
import org.hibernate.validator.constraints.NotBlank;
public class InputForm {
@NotBlank ( message = "{required.string.blank}")
String requiredString;
控制器 -
@RequestMapping(value = "/check/string", method = RequestMethod.POST)
public String checkString(
@ModelAttribute("formModel") @Valid InputForm inputForm ,
BindingResult result, Model model, HttpServletResponse response,
HttpServletRequest request) {
if (result.hasErrors()) {
model.addAttribute("formModel", inputForm);
return "userInput";
}
// Do some backend validation with String
result.reject("string.not.valid",
"String is Invalid");
model.addAttribute("formModel", inputForm);
return "userInput";
}
application.properties(在/ WEB_INF /文件夹中)
required.string.blank=Please enter the required string.
string.not.valid=Please enter a valid string.
fileapplication.properties(在/ conf /文件夹中。将覆盖上面的文件)
required.string.blank=You did not enter the required string. #Does not reflect when I change here
string.not.valid=You did not enter a valid string. #Reflects when I change here
现在我遇到的问题是,当我更新&#34; string.not.valid&#34;在fileapplication.properties中它反映在运行时,我看到更新的消息。但是当我更新&#34; required.string.blank&#34;在fileapplication.properties中,它不会在运行时反映出来。 请注意,在应用程序启动时,覆盖部分对两个消息都正常工作。但是&#34;重装&#34;部分不适用于&#34; required.string.blank&#34;。
答案 0 :(得分:1)
这是我根据我的研究得出的结果 - 我们需要创建自己的MessageInterpolator并将其作为依赖项添加到验证器而不是消息源。因为当我们添加一个messageSource作为依赖项时,它默认由验证程序缓存,并且任何消息reloads spring都不会在验证程序的缓存的messageSource实例中生效。
以下是详细信息:
在context.xml中,将自定义MessageInterpolator作为依赖项添加到LocalValidatorFactoryBean而不是messageSource:
<mvc:annotation-driven validator="validator" />
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>file:../conf/fileapplication</value> <!-- Messages here will override the below properties file-->
<value>/WEB-INF/application</value>
</list>
</property>
<property name="cacheSeconds" value="10"></property> <!-- Will check for refresh every 10 seconds -->
</bean>
<bean name="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="messageInterpolator">
<ref bean="messageInterpolator"/>
</property>
</bean>
<bean name="messageInterpolator"
class="com.my.org.support.MyCustomResourceBundleMessageInterpolator">
<constructor-arg ref="messageSource" />
</bean>
通过扩展Hibernate的org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator来创建自定义MessageInterpolator。
public class MyCustomResourceBundleMessageInterpolator extends
ResourceBundleMessageInterpolator {
public MyCustomResourceBundleMessageInterpolator(MessageSource messageSource)
{
// Passing false for the second argument
// in the super() constructor avoids the messages being cached.
super(new MessageSourceResourceBundleLocator(messageSource), false);
}
}
模型,控制器和属性文件可以与问题中的相同。