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