利用java.util.ResourceBundle快速获取properties配置内容
前言
之前加载properties文件,使用类加载器,今天学习到可以使用ResourceBundle的静态方法来加载,比之前更简洁,但需要注意,只需要写配置文件名就可以了,也就是说后缀.properties不需要写了
提示:以下是本篇文章正文内容,下面案例可供参考
二、具体代码如下
1.以JedisPool(连接池)配置文件演示
代码如下(示例):
文件名为redis.properties
redis.maxTotal=50
redis.maxIdel=10
redis.host=127.0.0.1
redis.port=6379
2.读取配置文件
代码如下(示例):
private static JedisPool pool ;
static {
ResourceBundle bundle = ResourceBundle.getBundle("redis");
String maxTotal = bundle.getString("redis.maxTotal");
String maxIdel = bundle.getString("redis.maxIdel");
String host = bundle.getString("redis.host");
String port = bundle.getString("redis.port");
//设置jedis连接池的配置文件
JedisPoolConfig config=new JedisPoolConfig();
config.setMaxIdle(Integer.parseInt(maxIdel));
config.setMaxTotal(Integer.parseInt(maxTotal));
pool=new JedisPool(config,host,Integer.parseInt(port));
}
总结
提示:这里对文章进行总结:
在使用过程中,我们需要再次注意,不需要写.properties后缀名,只需要名称就可以了.