6

「データベース駆動型リソース バンドル」を機能させるのに問題があります。以下の例でTextDAOは、アプリケーションの起動時に適切に挿入されますが、messageSourceアクセスされると新しいMessagesオブジェクトが作成されます。これがポイントです。これを機能させる方法は?

<!-- message source -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="someapp.bundle.Messages" />
</bean>

Messages.java

@Component
public class Messages extends ListResourceBundle {

    @Autowired
    private TextDAO textDAO;

    public Messages() {
        log.debug("CONSTRUCTOR");
    }

    @Override
    protected Object[][] getContents() {
        // loading messages from DB
        List<Text> texts = textDAO.findAll();  // textDAO is null
        ...
    }
}

再オープン

提案どおりBozho、以下のようにリソース バンドルを作成しましたが、動的にリロードするのに問題があります。ReloadableResourceBundleMessageSource はプロパティ ファイル用だと思いますが、これも機能する可能性があります。

public class DatabaseDrivenMessageSource extends ReloadableResourceBundleMessageSource {

    private Logger log = LoggerFactory.getLogger(getClass());

    private final Map<String, Map<String, String>> properties = new HashMap<String, Map<String, String>>();

    private TextDAO textDAO;

    @Autowired
    public DatabaseDrivenMessageSource(TextDAO textDAO) {
        this.textDAO = textDAO;
        reload();
    }

    @Override
    protected MessageFormat resolveCode(String code, Locale locale) {
        String msg = getText(code, locale);
        MessageFormat result = createMessageFormat(msg, locale);
        return result;
    }

    @Override
    protected String resolveCodeWithoutArguments(String code, Locale locale) {
        return getText(code, locale);
    }

    private String getText(String code, Locale locale) {
        Map<String, String> localized = properties.get(code);
        String textForCurrentLanguage = null;
        if (localized != null) {
            textForCurrentLanguage = localized.get(locale.getLanguage());
            if (textForCurrentLanguage == null) {
                textForCurrentLanguage = localized.get(Locale.ENGLISH.getLanguage());
            }
        }
        return textForCurrentLanguage != null ? textForCurrentLanguage : code;
    }

    public void reload() {
        properties.clear();
        properties.putAll(loadTexts());
    }

    protected Map<String, Map<String, String>> loadTexts() {
        log.debug("loadTexts");
        Map<String, Map<String, String>> m = new HashMap<String, Map<String, String>>();
        List<Text> texts = textDAO.findAll();
        for(Text text: texts) {
            Map<String, String> v = new HashMap<String, String>();
            v.put("en", text.getEn());
            v.put("de", text.getDe());
            m.put(text.getKey(), v);
        }
        return m;
    }
}
4

3 に答える 3

3

basenameはひもなので、春の豆ではないので、そこに注入はありません。

あなたがしようとすることができるのはReloadableResourceBundleMessageSource、そこでいくつかのメソッドをサブクラス化してオーバーライドすることです(たとえば- getMessage(..))。DAOはサブクラスに注入する必要があります。

于 2011-03-31T14:28:26.580 に答える
2

ReloadableResourceBundleMessageSourceプロパティで指定されたクラスのインスタンスを作成しますbasename。このインスタンスには、Spring によって注入された依存関係はありません。そのため、オブジェクトのtextDAOフィールドは null です。Messages

この Spring 号には、添付ファイルとして、JDBC でサポートされた MessageSource の例のソース コードが含まれていますReloadableResourceBundleMessageSource

于 2011-03-31T15:07:29.167 に答える
0

少し古いですが、まだ関連しています... i18n のリソースバンドルとして JDBC バックエンドを備えた Spring Cloud Config サーバーを使用しています。ゼロコード。素晴らしい作品!

于 2019-04-29T12:36:58.143 に答える