ResourceBundle类的作用就是读取资源属性文件(properties),然后根据.properties文件的名称信息(本地化信息),匹配当前系统的国别语言信息(也可以程序指定),然后获取相应的properties文件的内容。
properties
1.ResourceBundle的常用方法:
1.1根据资源文件path加载资源
String sourcePath = "XXXXX.properties"
ResourceBundle resourceBundle = ResourceBundle.getBundle(sourcePath);
1.2获取资源文件中所有的key
Enumeration<String> keys = resourceBundle.getKeys();
1.3根据key读取对应的value值
String value = resourceBundle.getString(key);
使用场景
ResourceBundle已经给我们提供了通过key获取value的方法,相对来说比较方便。如果我们需要获取配置文件中的用户名,则如下代码就可以实现。
String userName= resourceBundle.getString("userName");
但是假设配置属性很多时,我们就需要重复写很多这样的荣誉代码,有没有适合Java编程思想里面的好的方式,将这些属性映射到java对象中,然后我们自己从java对象中获取嗯?
这里我们可以通过反射的手段将properties的属性值的key和java对象的属性字段进行匹配,如果配置文件的key值等于对象的属性字段,则将key对应的value映射到java对象的字段的属性上。
2.代码实现
2.1代码结构如下
2.2database.properties
##驱动名称:不推荐com.mysql.jdbc.Driver,而是推荐使用com.mysql.cj.jdbc.Driver
driverName = com.mysql.cj.jdbc.Driver
##数据库连接地址
url = jdbc:mysql://127.0.0.1:3306/testdb
userName = root
passWord = root
##连接池名字
poolName = Hutao Connection Pool
##空闲池,最小连接数
minConnections = 1
##空闲池,最大连接数
maxConnections = 10
##初始化连接数
initConnections = 5
##重复获得连接的频率 一秒
connTimeOut = 1000
##最大允许的连接数,和数据库对应
maxActiveConnections = 100
##连接超时时间,默认20分钟 1000 * 60 * 20
connectionTimeOut = 1200000
2.2DbProperties对象
该对象的所有属性名称和配置文件中的key保持一致
public class DbProperties {
private String driverName;
private String url;
private String userName;
private String passWord;
private String poolName;
private int minConnections;
private int maxConnections;
private int initConnections;
private long connTimeOut;
private int maxActiveConnections;
private long connectionTimeOut;
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getPoolName() {
return poolName;
}
public void setPoolName(String poolName) {
this.poolName = poolName;
}
public int getMinConnections() {
return minConnections;
}
public void setMinConnections(int minConnections) {
this.minConnections = minConnections;
}
public int getMaxConnections() {
return maxConnections;
}
public void setMaxConnections(int maxConnections) {
this.maxConnections = maxConnections;
}
public int getInitConnections() {
return initConnections;
}
public void setInitConnections(int initConnections) {
this.initConnections = initConnections;
}
public long getConnTimeOut() {
return connTimeOut;
}
public void setConnTimeOut(long connTimeOut) {
this.connTimeOut = connTimeOut;
}
public int getMaxActiveConnections() {
return maxActiveConnections;
}
public void setMaxActiveConnections(int maxActiveConnections) {
this.maxActiveConnections = maxActiveConnections;
}
public long getConnectionTimeOut() {
return connectionTimeOut;
}
public void setConnectionTimeOut(long connectionTimeOut) {
this.connectionTimeOut = connectionTimeOut;
}
@Override
public String toString() {
return "DbProperties [driverName=" + driverName + ", url=" + url + ", userName=" + userName + ", passWord="
+ passWord + ", poolName=" + poolName + ", minConnections=" + minConnections + ", maxConnections="
+ maxConnections + ", initConnections=" + initConnections + ", connTimeOut=" + connTimeOut
+ ", maxActiveConnections=" + maxActiveConnections + ", connectionTimeOut=" + connectionTimeOut + "]";
}
}
public class DbPoolManager {
private static String sourcePath = "com/hutao/resources/database";
private static DbProperties properties = null;
static {
try {
if(properties == null) {
synchronized(DbPoolManager.class) {
if(properties == null) {
properites2Object();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void properites2Object() throws NoSuchFieldException, IllegalAccessException {
properties = new DbProperties();
ResourceBundle resourceBundle = ResourceBundle.getBundle(sourcePath);
//获取资源文件中所有的key
Enumeration<String> keys = resourceBundle.getKeys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
//反射获取类中的属性字段
Field field= DbProperties.class.getDeclaredField(key);
//属性字段的类型
Type genericType = field.getGenericType();
//属性设置可访问
field.setAccessible(true);
//根据key读取对应的value值
String value = resourceBundle.getString(key);
if("int".equals(genericType.getTypeName())) {
//反射给属性赋值
field.set(properties, Integer.parseInt(value));
}else if("long".equals(genericType.getTypeName())) {
field.set(properties, Long.parseLong(value));
}else if("java.lang.String".equals(genericType.getTypeName())) {
field.set(properties,value);
}
}
}
}
演示效果