私は、以下に含まれるDatabaseMessageSourceというクラスを作成しました。私はまだいくつかの種類のキャッシュを実装する必要があるので、私は各呼び出しでデータベースにヒットしません。このlinkも参考になりました。正しい方向に向いてくれてありがとう、スカフマンとプリモスキー。
public class DatabaseMessageSource extends ReloadableResourceBundleMessageSource {
@Autowired
private MyDao myDao;
protected MessageFormat resolveCode(String code, Locale locale) {
MyObj myObj = myDao.findByCode(code);
MessageFormat format;
if (myObj!= null && myObj.getId() != null) {
format = new MessageFormat(myObj.getValue(), locale);
} else {
format = super.resolveCode(code, locale);
}
return format;
}
protected String resolveCodeWithoutArguments(String code, Locale locale) {
MyObj myObj = myDao.findByCode(code);
String format;
if (myObj != null && myObj.getId() != null) {
format = myObj.getValue();
} else {
format = super.resolveCodeWithoutArguments(code, locale);
}
return format;
}
}
新しく作成したクラスを指すようにapplicationContextを更新しました。 私はそれを変更する:私はEhcacheのをお勧めし、キャッシングのために
<bean id="messageSource" class="com.mycompany.mypackage.DatabaseMessageSource">
<property name="basenames">
<list>
<value>classpath:defaultMessages</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8"/>
</bean>`enter code here`
、... [この](http://ehcache.org/documentation/recipes/thunderingherd)を見て、[この](HTTP: //ehcache.org/documentation/recipes/spring-annotations).... – PrimosK
ありがとうございます。私は最初のリンクを実装します。あなたは本当に役に立ちました。再度ありがとう – blong824
1つのフォローアップの質問。コントローラからDatabaseMessageSourceクラスにオブジェクトを渡すにはどうしたらいいですか?オブジェクトは、spring:messageタグを含む画面がロードされるまでにセッション中です。 – blong824