#1.方法1:常规取法
java本身就给我们提供了属性文件的读取方法,即java集合框架中的properties,详见这篇文章介绍
https://my.oschina.net/u/2312022/blog/748813
#2.方法2:通过spring注解读取
1.工程中新建配置文件
2.配置文件内容
#资源位置
driverLetter = E\:/Resource
#用户头像
userImgPath =/img/user/
3.spring扫描该配置文件
<bean id="propertiesReader" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<value>classpath:resourceConfig.properties</value>
</property>
</bean>
4.java中配置文件对应的bean
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("ConfigReader")
public class ConfigReader {
/** 本地盘符 **/
@Value("#{propertiesReader[driverLetter]}")
private String driverLetter;
/** 项目路径 **/
@Value("#{propertiesReader[localPath]}")
private String localPath;
public String getDriverLetter() {
return driverLetter;
}
public void setDriverLetter(String driverLetter) {
this.driverLetter = driverLetter;
}
public String getLocalPath() {
return localPath;
}
public void setLocalPath(String localPath) {
this.localPath = localPath;
}
}
5.使用该bean
@Service
public class CommonServiceImpl implements CommonService {
@Autowired
private ConfigReader configReader;
/**
* 图片
*/
public boolean doImg(Integer userId,String filePath){
//使用
String driverLetter = configReader.getDriverLetter();
}
}