Skip to content

Commit

Permalink
代码优化: createDataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
xnat9 committed Oct 21, 2021
1 parent 8b12a2b commit 9887af4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/main/java/cn/xnatural/app/AppContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public void run() {

/**
* 添加对象源
* {@link #ep} 会找出source对象中所有其暴露的功能. 即: 用 @EL 标注的方法
* {@link #ep} 会找出source对象中所有其暴露的功能. 即: 用 {@link EL} 标注的方法
* 注: 为每个对象源都配一个 name 属性标识
* @param source bean 对象
* @param name bean 名字
Expand Down
53 changes: 26 additions & 27 deletions src/main/java/cn/xnatural/app/util/DB.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class DB implements AutoCloseable {
* @param dataSource 外部数据源
*/
public DB(DataSource dataSource) {
if (dataSource == null) throw new IllegalArgumentException("Param dataSource must not null");
if (dataSource == null) throw new IllegalArgumentException("Param dataSource required");
this.dataSource = dataSource;
}

Expand Down Expand Up @@ -370,50 +370,49 @@ public static DataSource createDataSource(Map<String, Object> dsAttr) {
try {
Map props = new HashMap();
dsAttr.forEach((s, o) -> props.put(s, Objects.toString(o, "")));
// if (!props.containsKey("validationQuery")) props.put("validationQuery", "select 1") // oracle
// if (!props.containsKey("validationQuery")) props.put("validationQuery", "select 1") // oracle 不行
if (!props.containsKey("filters")) { // 默认监控慢sql
props.put("filters", "stat");
}
if (!props.containsKey("connectionProperties")) {
// com.alibaba.druid.filter.stat.StatFilter
props.put("connectionProperties", "druid.stat.logSlowSql=true;druid.stat.slowSqlMillis=5000");
}
if (!props.containsKey(""))
ds = (DataSource) Class.forName("com.alibaba.druid.pool.DruidDataSourceFactory").getMethod("createDataSource", Map.class).invoke(null, props);
ds = (DataSource) Class.forName("com.alibaba.druid.pool.DruidDataSourceFactory").getMethod("createDataSource", Map.class).invoke(null, props);
}
catch(ClassNotFoundException ex) {}
catch(Exception ex) { throw new RuntimeException(ex); }
if (ds != null) return ds;

// Hikari 数据源
if (ds == null) {
try {
Class<?> clz = Class.forName("com.zaxxer.hikari.HikariDataSource");
ds = (DataSource) clz.newInstance();
for (PropertyDescriptor pd : Introspector.getBeanInfo(clz).getPropertyDescriptors()) {
Object v = dsAttr.get(pd.getName());
if (v != null) {
if (Integer.class.equals(pd.getPropertyType()) || int.class.equals(pd.getPropertyType())) pd.getWriteMethod().invoke(ds, Integer.valueOf(v.toString()));
else if (Long.class.equals(pd.getPropertyType()) || long.class.equals(pd.getPropertyType())) pd.getWriteMethod().invoke(ds, Long.valueOf(v.toString()));
else if (Boolean.class.equals(pd.getPropertyType()) || boolean.class.equals(pd.getPropertyType())) pd.getWriteMethod().invoke(ds, Boolean.valueOf(v.toString()));
else pd.getWriteMethod().invoke(ds, v);
}
try {
Class<?> clz = Class.forName("com.zaxxer.hikari.HikariDataSource");
ds = (DataSource) clz.newInstance();
for (PropertyDescriptor pd : Introspector.getBeanInfo(clz).getPropertyDescriptors()) {
Object v = dsAttr.get(pd.getName());
if (v != null) {
if (Integer.class.equals(pd.getPropertyType()) || int.class.equals(pd.getPropertyType())) pd.getWriteMethod().invoke(ds, Integer.valueOf(v.toString()));
else if (Long.class.equals(pd.getPropertyType()) || long.class.equals(pd.getPropertyType())) pd.getWriteMethod().invoke(ds, Long.valueOf(v.toString()));
else if (Boolean.class.equals(pd.getPropertyType()) || boolean.class.equals(pd.getPropertyType())) pd.getWriteMethod().invoke(ds, Boolean.valueOf(v.toString()));
else pd.getWriteMethod().invoke(ds, v);
}
}
catch(ClassNotFoundException ex) {}
catch(Exception ex) { throw new RuntimeException(ex); }
}
catch(ClassNotFoundException ex) {}
catch(Exception ex) { throw new RuntimeException(ex); }
if (ds != null) return ds;

// dbcp2 数据源
if (ds == null) {
try {
Properties props = new Properties();
dsAttr.forEach((s, o) -> props.put(s, Objects.toString(o, "")));
// if (!props.containsKey("validationQuery")) props.put("validationQuery", "select 1");
ds = (DataSource) Class.forName("org.apache.commons.dbcp2.BasicDataSourceFactory").getMethod("createDataSource", Properties.class).invoke(null, props);
}
catch(ClassNotFoundException ex) {}
catch(Exception ex) { throw new RuntimeException(ex); }
try {
Properties props = new Properties();
dsAttr.forEach((s, o) -> props.put(s, Objects.toString(o, "")));
// if (!props.containsKey("validationQuery")) props.put("validationQuery", "select 1");
ds = (DataSource) Class.forName("org.apache.commons.dbcp2.BasicDataSourceFactory").getMethod("createDataSource", Properties.class).invoke(null, props);
}
catch(ClassNotFoundException ex) {}
catch(Exception ex) { throw new RuntimeException(ex); }

if (ds == null) throw new RuntimeException("No found DataSource impl class");
return ds;
}
}
2 changes: 2 additions & 0 deletions src/main/java/cn/xnatural/app/util/ToMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

/**
* 转换成Map
* use {@link Copier}
* @param <T>
*/
@Deprecated
public class ToMap<T> {
private T bean;
private Map<String, String> propAlias;
Expand Down

0 comments on commit 9887af4

Please sign in to comment.