#1.方法1:常規取法
java自己就給咱們提供了屬性文件的讀取方法,即java集合框架中的properties,詳見這篇文章介紹
http://www.javashuo.com/article/p-ragldpob-ew.html
#2.方法2:經過spring註解讀取
1.工程中新建配置文件
2.配置文件內容java
#資源位置 driverLetter = E\:/Resource #用戶頭像 userImgPath =/img/user/
3.spring掃描該配置文件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.使用該beanthis
@Service public class CommonServiceImpl implements CommonService { @Autowired private ConfigReader configReader; /** * 圖片 */ public boolean doImg(Integer userId,String filePath){ //使用 String driverLetter = configReader.getDriverLetter(); } }