0

アプリケーション メッセージをプロパティ ファイルに外部化したい。Spring を使用してプロパティ ファイルを読み込んでいます。

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 <property name="locations">
  <list>      
     <value>classpath:applicationmessage.properties</value>      
  </list>
 </property>
 <property name="ignoreResourceNotFound" value="true" />

私はパラとのメッセージを持っています

message.myMessage = Couldn't find resource for customer id {0} and business unit {1}

このメッセージを Java ファイルからパラメータで読み取る最良の方法は何ですか? メッセージを外部化する他のアプローチはありますか。

4

2 に答える 2

1

こんにちは、Spring からプロパティ メッセージを取得する方法はいくつかあります。

方法 1:

<util:properties id="Properties" location="classpath:config/taobaoConfig.properties" />

これを spring.xml に追加します

あなたのJavaファイルで。次のプロパティを作成します。

 @Resource(name = "Properties")
private Properties serverProperties;

プロパティ ファイルのキー値は、serverProperties プロパティになります。

方法 2:

プロパティ コンテナ Bean を作成する

<bean id="propertyUtil" class="com.PropertiesUtil">
    <property name="locations">
        <list>
            <value>/WEB-INF/classes/datasource.properties</value>
            <value>/WEB-INF/classes/fileDef.properties</value>
        </list>
    </property>
</bean>

com.PropertiesUtil のコード

    public class PropertiesUtil extends PropertyPlaceholderConfigurer {
private Properties properties;

@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) {
    super.processProperties(beanFactory, props);
    this.properties = props;
}

/**
 * Get property from properties file.
 * @param name property name
 * @return property value
 */
public String getProperty(final String name) {
    return properties.getProperty(name);
}
}

このコンテナ Bean を使用して、プロパティ ファイルのキー値を取得できます。

于 2013-11-07T05:34:11.603 に答える