2017-08-14 2 views
0

私は、エラーメッセージを更新するときに、サーバーを再起動する必要なく反映するように、SpringのReloadableResourceBundleMessageSourceをLocalValidatorFactoryBeanに使用しようとしています。私はSpring 4.1.4、hibernate-validator 4.3.2.Finalを使用しています。org.hibernate.validator.constraintsがリロードされたメッセージを選択しない

の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/folderにあります。私はそれが実行時に反映fileapplication.propertiesで「string.not.valid」を更新し、私は更新されたメッセージを見たときに今、私が直面しています問題は、ある)

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 

ファイルの上に上書きされます。しかし、fileapplication.propertiesの "required.string.blank"を更新すると、実行時に反映されません。 アプリケーションの起動時に、両方のメッセージに対して上書き部分が正常に動作していることに注意してください。しかし、 "reloading"部分は "required.string.blank"に対して正常に動作していません。

答えて

0

私は私の研究に基づいて考えました。私たち自身のMessageInterpolatorを作成し、それをメッセージソースではなくバリデーターに依存として追加する必要があります。 MessageSourceを依存関係として追加すると、バリデータによってデフォルトでキャッシュされるため、メッセージリロードのスプリングリターンは、バリデータのキャッシュされたmessageSourceのインスタンスでは有効になりません。以下は

詳細です:のcontext.xmlで

、LocalValidatorFactoryBean代わりのmessageSourceへの依存関係としてカスタムMessageInterpolatorを追加します。

<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を拡張してカスタムMessageInterpolatorを作成します。 messageinterpolation.ResourceBundleMessageInterpolator。

public class MyCustomResourceBundleMessageInterpolatorextends 
    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);  
    } 
} 

モデル、コントローラおよびプロパティファイルは、質問と同じにすることができます。

関連する問題