当前位置:网站首页>七、HikariConfig初始化分析
七、HikariConfig初始化分析
2022-07-29 21:41:00 【InfoQ】
HikariConfig无参构造
public HikariConfig() {
dataSourceProperties = new Properties();
healthCheckProperties = new Properties();
//①
minIdle = -1;
maxPoolSize = -1;
maxLifetime = MAX_LIFETIME;
connectionTimeout = CONNECTION_TIMEOUT;
validationTimeout = VALIDATION_TIMEOUT;
idleTimeout = IDLE_TIMEOUT;
isAutoCommit = true;
isInitializationFailFast = true;
//②
String systemProp = System.getProperty("hikaricp.configurationFile");
if (systemProp != null) {
loadProperties(systemProp);
}
}
- ①初始化默认值
- ②获取配置文件地址
hikaricp.configurationFile-DloadProperties protected void loadProperties(String propertyFileName) {
//①将propertyFileName当作一个路径, 直接加载文件
final File propFile = new File(propertyFileName);
//②判断propFile是一个文件, 就直接读取; 不是文件, 就继续查找, 加载不到, 直接报错
try (final InputStream is = propFile.isFile() ? new FileInputStream(propFile) : this.getClass().getResourceAsStream(propertyFileName)) {
if (is != null) {
Properties props = new Properties();
props.load(is);
//③
PropertyElf.setTargetFromProperties(this, props);
} else {
throw new IllegalArgumentException("Cannot find property file: " + propertyFileName);
}
} catch (IOException io) {
throw new RuntimeException("Failed to read property file", io);
}
}
- ①加载文件
- ②查找文件
- ③获取属性
HikariConfig配置赋值
public static void setTargetFromProperties(final Object target, final Properties properties) {
if (target == null || properties == null) {
return;
}
//①获取HikariConfig中的所有方法
List<Method> methods = Arrays.asList(target.getClass().getMethods());
//获取properties配置文件的所有配置项名称的集合, 例如dataSource.username = tom , 此处拿到的是dataSource.username这种等号左边的所有配置项
Enumeration<?> propertyNames = properties.propertyNames();
//遍历所有的配置项, 复制到HikariConfig中
while (propertyNames.hasMoreElements()) {
Object key = propertyNames.nextElement();
String propName = key.toString();
//获取配置项对应的值
Object propValue = properties.getProperty(propName);
if (propValue == null) {
propValue = properties.get(key);
}
//②如果不是HikariConfig的配置, 是dataSource的配置, 也就是数据库相关的配置, 就添加到DataSourceProperty中
// 因为每个数据库的配置可能不一样, 所有没有 setter 方法
if (target instanceof HikariConfig && propName.startsWith("dataSource.")) {
((HikariConfig) target).addDataSourceProperty(propName.substring("dataSource.".length()), propValue);
} else {
//③如果不是 dataSource配置, 那就调用 setter 方法, 给HikariConfig赋值
setProperty(target, propName, propValue, methods);
}
}
}
HikariConfigHikariConfigdataSource.XXXdataSource.setProperty边栏推荐
猜你喜欢

一篇关于Web3.0如何走向安全时代的说明

解决报错 WARNING: IPv4 forwarding is disabled. Networking will not work.

第3章业务功能开发(线索关联市场活动,动态搜索)

【板栗糖GIS】DOS—如何在目录文件中批量建立子文件夹

南信大提出TIPCB,一个简单但有效的用于基于文本的人员搜索的基于部分的卷积baseline

使用脚本安装mysql

install mysql using script

新库上线 | CnOpenData租赁和商务服务业工商注册企业基本信息数据

程序员自由工作的三大痛点?一文教你统统解决

程序员「小镇做题」出身,月薪是父母半年收入 ……
随机推荐
对不起,你很难赚到中年人的钱
An article to understand service governance in distributed development
【板栗糖GIS】DOS—如何在目录文件中批量建立子文件夹
B. Party(图论/暴力/顶点的度数)
GBASE 8s 自定义存储过程和函数使用
解决reudx中的异步问题 applyMiddleware thunk
GBASE 8s 数据库的逻辑日志备份
GBASE 8s 如何估算索引使用多少空间
SwiftUI CoreData 教程之如何加速搜索速度
Xshell 7 提示 “要继续使用此程序,您必须应用最新的更新或使用新版本”
Verilog 加法器设计
AI全流程开发难题破解之钥
C. Color the Picture(贪心/构造)
容器网络硬核技术内幕 (24) 知微知彰,知柔知刚 (上)
GBASE 8s 数据库的大对象和日志备份
GBASE 8s PAM插入式身份验证模块
24-hour London gold chart analysis
阿里 P8 爆出的这份大厂面试指南,看完工资暴涨 30k!
Add obsolete flag to pdf
GBASE 8s 自定义存储过程和函数示例