springmvc --java读取配置文件

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

}

转载于:https://my.oschina.net/u/2312022/blog/752840

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值