Spring MVC 2.5:如何加载属性文件
我需要加载许多属性文件,这些文件位于resources文件夹中。
我有一个名为abc_en.properties的资源,其内容如下:
a = x
b = y
c = z
我需要在Java方法中使用属性sing java.util.Properties:
java.util.Properties reportProperties = new java.util.Properties(); ... String a = reportProperties.getProperty("a");
我怎样才能做到这一点?
谢谢
您需要在上下文文件中定义propertyConfigurer bean:
classpath:abc.properties classpath:efg.properties
编辑:
要使用java.util.Properties
您需要在上下文文件中定义PropertiesFactoryBean
bean:
classpath:abc_en.properties classpath:abc_fr.properties
然后在您的类中,您需要定义一个java.util.Properties
varible并将属性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
批注,则需要在上下文文件中放置
元素。
您需要在xml文件中定义messagesource bean。
试试这种方式
resources.abc.abc