2

リソースフォルダにある多くのプロパティファイルをロードする必要があります。

以下の内容のabc_en.propertiesというリソースがあります。
a = x
b = y
c = z

そして、Javaメソッドでjava.util.Propertiesを歌うプロパティを使用する必要があります。

  java.util.Properties reportProperties = new java.util.Properties();   
   ...
  String a = reportProperties.getProperty("a");

これどうやってするの?

ありがとう

4

2 に答える 2

4

コンテキストファイルでpropertyConfigurerBeanを定義する必要があります。

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

編集:

使用するには、コンテキストファイルでBeanjava.util.Propertiesを定義する必要があります。PropertiesFactoryBean

    <bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
          <property name="location">
               <list>
                 <value>classpath:abc_en.properties</value>
                 <value>classpath:abc_fr.properties</value>
               </list>
          </property>
        </bean>

java.util.Properties次に、クラスで変数を定義し、プロパティBeanをそのクラスにロードする必要があります。

public class MyClass {

     @Autowired
     private java.util.Properties properties;


     public void myMethod() {
         String a = properties.getProperty("a");
         String b = properties.getProperty("b");
         String c = properties.getProperty("c");
     }
}

プロパティBeanをクラスにロードする方法は他にもありますが、@Autowiredアノテーションを使用する場合は、<context:annotation-config />要素をコンテキストファイルに配置する必要があります。

于 2012-07-25T09:58:56.687 に答える
0

xmlファイルでmessagesourceBeanを定義する必要があります。

この方法を試してください

<bean id="messageSource" name="applicationMessageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames">
      <list>
          <value>resources.abc.abc</value>
       </list>
    </property>
</bean>
于 2012-07-25T09:54:20.277 に答える