2017-11-18 12 views
2

JSON形式のローカライズされたテキストを含むmessages.propertiesに私のWebサービスが必要です。私はそれを行うために私自身のパーサを書くことができますが、Springフレームワークでこのロジックをどこに挿入すべきですか?あるいは、すでにこれを行うことができるSpringインフラストラクチャ機能がありますか?プロパティをJSONに変換する方法は、Spring MVCの方法ですか?

答えて

1

クラスに@PropertySourceアノテーションを使用すると、プロパティファイルをメモリに読み込むことができます。

@Configuration 
class MessagesConfiguration { 

    @Bean(name = "messageProperties") 
    public static PropertiesFactoryBean mapper() { 
     PropertiesFactoryBean bean = new PropertiesFactoryBean(); 
     bean.setLocation(new ClassPathResource("messages.properties")); 
     return bean; 
    } 

    @Resource(name="messageProperties") 
    private Properties messages = new Properties(); 

    public Properties getMessages() { 
     return messages; 
    } 

} 

Properties.classあなたはJSONに変換することができますので、Map<String, String>ための単なるラッパーです。

+0

Springはすべてのプロパティをmessagesプロパティに注入しますか? – Xegara

+0

はい、すべてのプロパティをこのPropertyインスタンスに注入します。 1つの注意:すべてのプロパティをキー 'messages'で開始する必要があります。それは –

+0

私はしようとしましたが、それは何もメッセージプロパティに注入しませんでした。 – Xegara

関連する問題