springmvc --java读取配置文件

#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();
        }

}
相关文章
相关标签/搜索