1.创建一个properties文件config.properties
内容:
mp3.path=/home/music/
mp4.path=/home/movie/
<!-- 读取不同环境下的配置文件 -->
<bean
class= "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name= "locations">
<list>
<value> classpath:jdbc.properties</value >
<value> classpath:config.properties</value >
</list>
</property>
</bean>
.
.
.
<!-- 添加ConfigInfo对象用来读取mic和is9文件路径
-->
<bean id= "configInfo" class ="com.domain.ConfigInfo">
<property name= "MP3_PATH" value ="${mp3.path}"></ property>
<property name= "MP4_PATH" value ="${mp4.path}"></ property>
</bean>
---------------------------------------------------
这里configInfo这个bean可以单独写在另外的文件里,这样需要在app-context.xml主配置文件里
< import resource = "classpath:db_context.xml" />
当有多个properties文件时,只需在<list></list>里添加<value>即可。
3.创建一个ConfigInfo.class类,用来存储参数值
public class ConfigInfo {
private String MP3_PATH;
private String MP4_PATH;
public String getMP3_PATH() {
return MP3_PATH;
}
public String getMP4_PATH() {
return MP4_PATH;
}
public void setMP3_PATH(String
MP3_PATH) {
this. MP3_PATH =
MP3_PATH;
}
public void setMP4_PATH(String
MP4_PATH) {
this. MP4_PATH =
MP4_PATH;
}
}
4.剩下的就只是在Service或者Controller里使用注解的方式将存储了参数值的ConfigInfo类作为成员变量引入即可
@Resource
private ConfigInfo configInfo;
当使用到相应的参数值时,通过get方法进行调用。