Spring MVC 2.5:如何加载属性文件 [英] Spring MVC 2.5: how to load properties file
问题描述
我需要加载许多属性文件,这些文件位于资源文件夹中。
我有一个名为abc_en.properties的资源,其内容如下:
a = x
b = y
c = z
<我需要在Java方法中使用属性sing java.util.Properties:
java.util.Properties reportProperties = new java.util.Properties();
...
字符串a = reportProperties.getProperty(a);
我该怎么做?
谢谢
您需要在上下文文件中定义propertyConfigurer bean:
< 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>
编辑:
要使用 java.util.Properties
,您需要在上下文文件中定义 PropertiesFactoryBean
bean:
< bean id =propertiesclass =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
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加载到您的身上class,但如果您使用 @Autowired
注释,则需要输入< context:annotation-config />
你上下文文件中的元素。
I need to load many properties files, which are in the resources folder.
I have a resource called abc_en.properties with the content below:
a = x
b = y
c = z
and I need to use the properties sing java.util.Properties in a Java Method:
java.util.Properties reportProperties = new java.util.Properties();
...
String a = reportProperties.getProperty("a");
How can I do this?
Thanks
You need to define the propertyConfigurer bean in your context file:
<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>
EDIT:
In order to use java.util.Properties
you need to define the PropertiesFactoryBean
bean in your context file :
<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>
Then in your class you need to define a java.util.Properties
varible and load the properties bean into it :
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");
}
}
There are other ways to load the properties bean into you class, but if you use the @Autowired
annotation, you need to put the <context:annotation-config />
element in you context file.
这篇关于Spring MVC 2.5:如何加载属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!