当前位置: 首页 > article >正文

使用国际化资源文件ResourceBundle和反射手段,实现将配置文件properties信息映射到Java对象中

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保持一致

/*** @Description:数据库连接池属性信息* @author hutao* @mail hutao_2017@aliyun.com* @date 2020年07月08日*/
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;}@Overridepublic 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 + "]";}
}
/*** @Description:数据库连接池管理* @author hutao* @mail hutao_2017@aliyun.com* @date 2020年07月08日*/
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();}}/*** @Description:数据库连接池database配置文件映射到java对象* @author hutao* @mail hutao_2017@aliyun.com* @date 2020年07月08日*/private static void properites2Object() throws NoSuchFieldException, IllegalAccessException {properties = new DbProperties();ResourceBundle resourceBundle = ResourceBundle.getBundle(sourcePath);//获取资源文件中所有的keyEnumeration<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);}}}
}

演示效果
在这里插入图片描述

http://www.lryc.cn/news/2417377.html

相关文章:

  • android开发入门
  • Recordset对象方法详解
  • 探索R Markdown Book:一个强大的文档创作与分享平台
  • HTML常用语法总结【精华】
  • 基于Spring Boot的网吧管理系统-计算机毕业设计源码03067
  • 推荐文章:bt2qbt —— 简化你的种子迁移之旅
  • tsql_TSQL的历史
  • Linux mlabel命令教程:如何给磁盘添加卷标(附案例详解和注意事项)
  • ABAP基础知识 复杂程序的性能优化
  • 1.23聚类算法(kmeans(初始随机选k,迭代收敛),DBSCAN(dij选点),MEANSHIFT(质心收敛),AGENS(最小生成树)),蚁群算法(参数理解、过程理解、伪代码、代码)
  • 【设计模式】备忘录模式 ( 简介 | 适用场景 | 优缺点 | 代码示例 )
  • linux内核register_chrdev_region()系列函数
  • eclipse中如何使用svn
  • 结构体sockaddr、sockaddr_in、sockaddr_in6之间的区别和联系
  • I3D模型_2017_CVPR
  • Framework3.5 最终文件下载及离线安装
  • OpenGL 入门(一)— 创建窗口
  • Matlab roundn()函数使用样例
  • iOS - 融云RTC功能梳理
  • 信息安全-网络安全测评技术与标准
  • 学习心得——析构函数
  • AutoIt3使用
  • Android 使用ExpandableListView实现三级列表
  • Java GUI三种常见的布局方式.FlowLayout,BorderLayout,GridLayout.教程
  • Linux驱动之模块参数param和符合导出export用法
  • 【MSTP+VRRP实验】华三MSTP+VRRP配置,华三MSTP+VRRP实验
  • portlet的开发介绍
  • Win7系统提示找不到LogonUI.exe文件的解决办法
  • Java解析XML文件--实现读取/导入、写入/导出功能
  • 资源网站mark