use of cn.hutool.setting.Setting in project hutool by looly.
the class C3p0DSFactory method createDataSource.
/**
* 创建数据源
* @param group 分组
* @return C3P0数据源 {@link ComboPooledDataSource}
*/
private ComboPooledDataSource createDataSource(String group) {
final Setting config = setting.getSetting(group);
if (CollectionUtil.isEmpty(config)) {
throw new DbRuntimeException("No C3P0 config for group: [{}]", group);
}
final ComboPooledDataSource ds = new ComboPooledDataSource();
// 基本信息
final String url = config.getAndRemoveStr(KEY_ALIAS_URL);
if (StrUtil.isBlank(url)) {
throw new DbRuntimeException("No JDBC URL for group: [{}]", group);
}
ds.setJdbcUrl(url);
ds.setUser(config.getAndRemoveStr(KEY_ALIAS_USER));
ds.setPassword(config.getAndRemoveStr(KEY_ALIAS_PASSWORD));
final String driver = config.getAndRemoveStr(KEY_ALIAS_DRIVER);
try {
if (StrUtil.isNotBlank(driver)) {
ds.setDriverClass(driver);
} else {
ds.setDriverClass(DbUtil.identifyDriver(url));
}
} catch (Exception e) {
throw new DbRuntimeException(e);
}
// 注入属性
config.toBean(ds);
return ds;
}
use of cn.hutool.setting.Setting in project hutool by looly.
the class DruidDSFactory method createDataSource.
/**
* 创建数据源
* @param group 分组
* @return Druid数据源 {@link DruidDataSource}
*/
private DruidDataSource createDataSource(String group) {
final Setting config = setting.getSetting(group);
if (CollectionUtil.isEmpty(config)) {
throw new DbRuntimeException("No Druid config for group: [{}]", group);
}
final DruidDataSource ds = new DruidDataSource();
// 基本信息
final String url = config.getAndRemoveStr(KEY_ALIAS_URL);
if (StrUtil.isBlank(url)) {
throw new DbRuntimeException("No JDBC URL for group: [{}]", group);
}
ds.setUrl(url);
ds.setUsername(config.getAndRemoveStr(KEY_ALIAS_USER));
ds.setPassword(config.getAndRemoveStr(KEY_ALIAS_PASSWORD));
final String driver = config.getAndRemoveStr(KEY_ALIAS_DRIVER);
// 在未提供JDBC驱动的情况下,Druid会自动识别驱动
if (StrUtil.isNotBlank(driver)) {
ds.setDriverClassName(driver);
}
// 规范化属性名
Properties config2 = new Properties();
String keyStr;
for (Entry<Object, Object> entry : config.entrySet()) {
keyStr = StrUtil.addPrefixIfNot(Convert.toStr(entry.getKey()), "druid.");
config2.put(keyStr, entry.getValue());
}
// 连接池信息
ds.configFromPropety(config2);
// 检查关联配置,在用户未设置某项配置时,
if (null == ds.getValidationQuery()) {
// 在validationQuery未设置的情况下,以下三项设置都将无效
ds.setTestOnBorrow(false);
ds.setTestOnReturn(false);
ds.setTestWhileIdle(false);
}
return ds;
}
use of cn.hutool.setting.Setting in project hutool by looly.
the class TomcatDSFactory method createDataSource.
/**
* 创建数据源
* @param group 分组
* @return Tomcat数据源 {@link DataSource}
*/
private DataSource createDataSource(String group) {
if (group == null) {
group = StrUtil.EMPTY;
}
Setting config = setting.getSetting(group);
if (null == config || config.isEmpty()) {
throw new DbRuntimeException("No Tomcat jdbc pool config for group: [{}]", group);
}
final PoolProperties poolProps = new PoolProperties();
// 基本信息
poolProps.setUrl(getAndRemoveProperty(config, "url", "jdbcUrl"));
poolProps.setUsername(getAndRemoveProperty(config, "username", "user"));
poolProps.setPassword(getAndRemoveProperty(config, "password", "pass"));
final String driver = getAndRemoveProperty(config, "driver", "driverClassName");
if (StrUtil.isNotBlank(driver)) {
poolProps.setDriverClassName(driver);
} else {
poolProps.setDriverClassName(DbUtil.identifyDriver(poolProps.getUrl()));
}
// 扩展属性
config.toBean(poolProps);
final DataSource ds = new DataSource(poolProps);
return ds;
}
use of cn.hutool.setting.Setting in project hutool by looly.
the class SettingTest method settingTestForCustom.
@Test
public void settingTestForCustom() {
Setting setting = new Setting();
setting.put("group1.user", "root");
setting.put("group2.user", "root2");
setting.set("user", "group3", "root3");
setting.set("user", "root4");
Assert.assertEquals("root", setting.getByGroup("user", "group1"));
Assert.assertEquals("root2", setting.getByGroup("user", "group2"));
Assert.assertEquals("root3", setting.getByGroup("user", "group3"));
Assert.assertEquals("root4", setting.get("user"));
}
use of cn.hutool.setting.Setting in project hutool by looly.
the class SettingTest method settingTest.
@Test
public void settingTest() {
Setting setting = new Setting("test.setting", true);
String driver = setting.getByGroup("driver", "demo");
Assert.assertEquals(driver, "com.mysql.jdbc.Driver");
String user = setting.getByGroup("user", "demo");
Assert.assertEquals(user, "rootcom.mysql.jdbc.Driver");
}
Aggregations