配置文件主要是为了方便项目上线后配置信息随时改变,比较灵活。公司使用的paoding rose,和spring大体差不多。
1. 配置文件bean
package com.xx.gamecenter.dashboard.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author zexi
* @time 2016年10月20日 下午1:41:30
* 用户信息判定文件
*/
@Component
public class FilePathConfig {
@Value("#{propertyConfigurer['woman.packagename.file.path']}")
private String womanPackageNameFilePath;
@Value("#{propertyConfigurer['man.packagename.file.path']}")
private String manPackageNameFilePath;
@Value("#{propertyConfigurer['student.packagename.file.path']}")
private String studentPackageNameFilePath;
@Value("#{propertyConfigurer['internetuser.packagename.file.path']}")
private String internetuserPackageNameFilePath;
public String getWomanPackageNameFilePath() {
return womanPackageNameFilePath;
}
public void setWomanPackageNameFilePath(String womanPackageNameFilePath) {
this.womanPackageNameFilePath = womanPackageNameFilePath;
}
public String getManPackageNameFilePath() {
return manPackageNameFilePath;
}
public void setManPackageNameFilePath(String manPackageNameFilePath) {
this.manPackageNameFilePath = manPackageNameFilePath;
}
public String getStudentPackageNameFilePath() {
return studentPackageNameFilePath;
}
public void setStudentPackageNameFilePath(String studentPackageNameFilePath) {
this.studentPackageNameFilePath = studentPackageNameFilePath;
}
public String getInternetuserPackageNameFilePath() {
return internetuserPackageNameFilePath;
}
public void setInternetuserPackageNameFilePath(String internetuserPackageNameFilePath) {
this.internetuserPackageNameFilePath = internetuserPackageNameFilePath;
}
}
- 配置文件app.properties
woman.packagename.file.path=/data0/test/userInfo_confirm/woman_packageName.txt
man.packagename.file.path=/data0/test/userInfo_confirm/man_packageName.txt
student.packagename.file.path=/data0/test/userInfo_confirm/student_packageName.txt
internetuser.packagename.file.path=/data0/test/userInfo_confirm/internetuser_packageName.txt
- applicationContext.xml设置服务器启动加载配置文件
#单个配置文件写法
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location">
<value>classpath:server.properties</value>
</property>
<property name="fileEncoding" value="utf-8" />
</bean>
#多个配置文件写法
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:server.properties</value>
<value>classpath:app.properties</value>
</list>
</property>
<property name="fileEncoding" value="utf-8" />
</bean>
- 调用配置文件属性
@Autowired
private FilePathConfig filePathConfig;
filePathConfig.getXXX()调用;
再接再厉!
border="0" width="330" height="86" src="http://music.163.com/outchain/player?type=2&id=28427765&auto=1&height=66">