Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过Spring使用ReloadableResourceBundleMessageSource从特定属性文件获取属性值?_Java_Spring - Fatal编程技术网

Java 如何通过Spring使用ReloadableResourceBundleMessageSource从特定属性文件获取属性值?

Java 如何通过Spring使用ReloadableResourceBundleMessageSource从特定属性文件获取属性值?,java,spring,Java,Spring,假设我在/WEB-INF/properties下有两个属性文件/ 1.名称.属性 2.城市。房产 这些属性有键值对。我想通过使用ReloadableResourceBundleMessageSource通过Spring传递key来读取这些属性文件中定义的值 我有两个问题: 如果两个属性具有相同的键,如何告诉Spring从特定属性文件获取值 例如: names.properties有一个键值对:key1=James和 cities.properties有一个键值对:key1=Bangalore 我

假设我在/WEB-INF/properties下有两个属性文件/

1.名称.属性

2.城市。房产

这些属性有键值对。我想通过使用ReloadableResourceBundleMessageSource通过Spring传递key来读取这些属性文件中定义的

我有两个问题:

  • 如果两个属性具有相同的键,如何告诉Spring从特定属性文件获取值
  • 例如: names.properties有一个键值对:key1=James和 cities.properties有一个键值对:key1=Bangalore

    我希望通过从“names.properties”传递“key1”而不是从任何其他属性获得值“James”,即使其他属性中存在相同的键。。如何实现

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    String value = applicationContext.getMessage("key1",
            null, "DefaultValue", null); => there is no way to tell Spring from which property file value should be read ! (not locale)
    
  • 我想通过代码刷新属性文件(文件更改时不会自动刷新)。因此,我从applicationContext.xml中删除了
    所以applicationContext.xml看起来像

    <bean id="messageSource"    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames" >
    <list>
       <value>/WEB-INF/properties/names</value>
       <value>/WEB-INF/properties/cities</value> 
     </list>
     </property>
    </bean>
    

    但上述代码的问题在于它刷新了applicationContext列表中定义的所有属性。如何刷新特定属性文件?假设我只想刷新“names.properties”。

    您应该有两个不同的ReloadableResourceBundleMessageSource实例。谢谢@JBNizet。在这种情况下,假设我有10个属性文件,我必须定义10个不同的ReloadableResourceBundleMessageSource实例!而且这将只解决刷新特定属性文件的问题。。从特定属性文件获取属性值怎么样?如果您从第一个可重新加载的ResourceBundleMessageSource bean获取消息,它将从第一个属性文件加载该消息。如果您从第二个可重新加载的ResourceBundleMessageSource bean获取消息,它将从第二个属性文件获取消息。@jbnitet获取消息时,
    ApplicationContext ApplicationContext=new ClassPathXmlApplicationContext(“ApplicationContext.xml”);字符串值=applicationContext.getMessage(“key1”,null,“DefaultValue”,null)无法告诉Spring它应该返回哪个可重新加载的ResourceBundleMessageSourcebean。。或者是否有?@JBNizet但正如您所说,对于刷新特定属性文件,我们可以使用您的技术
    public static void refreshProperties(final application context application context,String resourcebundlebename){ReloadableResourceBundleMessageSource res=(ReloadableResourceBundleMessageSource)applicationContext.getBean(ResourceBundleBenName);res.clearCache();}
     public static void refreshProperties(final ApplicationContext applicationContext,String propertyFileName) 
     {
          ReloadableResourceBundleMessageSource res = (ReloadableResourceBundleMessageSource) 
                       applicationContext.getBean("messageSource");
          res.clearCache();
     }