Spring读取properties文件的处理

[b]Spring读取properties文件的处理[/b]


[b]properties文件[/b]
1.properties文件以.properties为后缀
2.一行写一个参数的值(key-value一行一个).如:redis.host=127.0.0.1
3.字符串不用加“”,是以行来读取内容的,所以多一个空格都会得不到你想要的结果
redis.properties
# Redis settings
redis.host=127.0.0.1
redis.port=6379
redis.pass=

redis.maxIdle=300
redis.maxTotal=600
redis.maxWaitMillis=1000
redis.testOnBorrow=true



[b]XML读取properties文件内容[/b]
<context:property-placeholder location="classpath:redis.properties" />	//location指明properties文件路径(classpath以类路径作为参考)
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" /> //${redis.maxIdle},读取redis.properties里的redis.maxIdle的值
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>



[b]代码读取properties文件内容[/b]
public class ConfigUtils {
public static String host = null;
static {
ResourceBundle res = ResourceBundle.getBundle("redis");//读取redis.properties文件
WX_SRC = res.getString("redis.host");//读取redis.properties里的redis.host的值
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值