Skip to content

Commit 5c197a6

Browse files
committed
优化配置
1 parent f480ce7 commit 5c197a6

File tree

2 files changed

+112
-19
lines changed

2 files changed

+112
-19
lines changed

hsweb-web-datasource/src/main/java/org/hsweb/web/datasource/dynamic/DynamicDataSourceProperties.java

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@
1616

1717
package org.hsweb.web.datasource.dynamic;
1818

19+
import com.atomikos.datasource.pool.ConnectionPool;
1920
import com.atomikos.jdbc.AtomikosDataSourceBean;
2021
import org.hsweb.web.core.datasource.DatabaseType;
2122
import org.springframework.beans.factory.BeanClassLoaderAware;
2223
import org.springframework.beans.factory.InitializingBean;
2324
import org.springframework.boot.context.properties.ConfigurationProperties;
25+
import org.springframework.util.Assert;
2426
import org.springframework.util.ClassUtils;
2527
import org.springframework.util.StringUtils;
2628

29+
import java.io.PrintWriter;
2730
import java.sql.SQLException;
2831
import java.util.LinkedList;
2932
import java.util.List;
@@ -38,25 +41,25 @@
3841
public class DynamicDataSourceProperties
3942
implements BeanClassLoaderAware, InitializingBean {
4043
private static final List<String> supportDatasourceType;
41-
private String name = "core";
42-
private DatabaseType type = DatabaseType.h2;
43-
private String datasourceName = null;
44-
private String username = "sa";
45-
private String password = "";
46-
private String url = "jdbc:h2:file:./data/h2db;DB_CLOSE_ON_EXIT=FALSE";
47-
private String testQuery = null;
48-
private int loginTimeout = -1;
49-
private int maxLifetime = -1;
50-
private int minPoolSize = 2;
51-
private int maxPoolSize = 20;
52-
private int borrowConnectionTimeout = 30;
53-
private int reapTimeout = 0;
54-
private int maxIdleTime = 60;
55-
private int maintenanceInterval = 60;
56-
private int defaultIsolationLevel = -1;
57-
private int transactionTimeout = 300;
58-
private Properties properties = null;
59-
private ClassLoader classLoader = null;
44+
private String name = "core";
45+
private DatabaseType type = null;
46+
private String datasourceName = null;
47+
private String username = "sa";
48+
private String password = "";
49+
private String url = "jdbc:h2:file:./data/h2db;DB_CLOSE_ON_EXIT=FALSE";
50+
private String testQuery;
51+
private int loginTimeout = 0;
52+
private int maxLifetime = 0;
53+
private int minPoolSize = 2;
54+
private int maxPoolSize = 20;
55+
private int borrowConnectionTimeout = 30;
56+
private int reapTimeout = 0;
57+
private int maxIdleTime = 60;
58+
private int maintenanceInterval = 60;
59+
private int defaultIsolationLevel = -1;
60+
private int transactionTimeout = 300;
61+
private Properties properties = null;
62+
private ClassLoader classLoader = null;
6063

6164
static {
6265
supportDatasourceType = new LinkedList<>();
@@ -218,9 +221,14 @@ public void setProperties(Properties properties) {
218221

219222
@Override
220223
public void afterPropertiesSet() throws Exception {
224+
Assert.notNull(url);
225+
Assert.notNull(username);
221226
if (datasourceName == null) {
222227
datasourceName = lookupSupportDatasourceName();
223228
}
229+
if (type == null) {
230+
type = DatabaseType.fromJdbcUrl(getUrl());
231+
}
224232
if (!StringUtils.hasText(testQuery)) testQuery = getType().getTestQuery();
225233
}
226234

hsweb-web-datasource/src/main/resources/META-INF/spring-configuration-metadata.json

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,91 @@
1111
"name": "hsweb.dynamic-datasource.name",
1212
"type": "java.lang.String",
1313
"sourceType": "org.hsweb.web.datasource.dynamic.DynamicDataSourceProperties"
14+
},
15+
{
16+
"name": "hsweb.dynamic-datasource.type",
17+
"type": "org.hsweb.web.core.datasource.DatabaseType",
18+
"sourceType": "org.hsweb.web.datasource.dynamic.DynamicDataSourceProperties"
19+
},
20+
{
21+
"name": "hsweb.dynamic-datasource.datasource-name",
22+
"type": "java.lang.String",
23+
"sourceType": "org.hsweb.web.datasource.dynamic.DynamicDataSourceProperties"
24+
},
25+
{
26+
"name": "hsweb.dynamic-datasource.username",
27+
"type": "java.lang.String",
28+
"sourceType": "org.hsweb.web.datasource.dynamic.DynamicDataSourceProperties"
29+
},
30+
{
31+
"name": "hsweb.dynamic-datasource.password",
32+
"type": "java.lang.String",
33+
"sourceType": "org.hsweb.web.datasource.dynamic.DynamicDataSourceProperties"
34+
},
35+
{
36+
"name": "hsweb.dynamic-datasource.url",
37+
"type": "java.lang.String",
38+
"sourceType": "org.hsweb.web.datasource.dynamic.DynamicDataSourceProperties"
39+
},
40+
{
41+
"name": "hsweb.dynamic-datasource.testQuery",
42+
"type": "java.lang.String",
43+
"sourceType": "org.hsweb.web.datasource.dynamic.DynamicDataSourceProperties"
44+
},
45+
{
46+
"name": "hsweb.dynamic-datasource.login-timeout",
47+
"type": "java.lang.Integer",
48+
"sourceType": "org.hsweb.web.datasource.dynamic.DynamicDataSourceProperties"
49+
},
50+
{
51+
"name": "hsweb.dynamic-datasource.max-life-time",
52+
"type": "java.lang.Integer",
53+
"sourceType": "org.hsweb.web.datasource.dynamic.DynamicDataSourceProperties"
54+
},
55+
{
56+
"name": "hsweb.dynamic-datasource.min-pool-size",
57+
"type": "java.lang.Integer",
58+
"sourceType": "org.hsweb.web.datasource.dynamic.DynamicDataSourceProperties"
59+
},
60+
{
61+
"name": "hsweb.dynamic-datasource.max-pool-size",
62+
"type": "java.lang.Integer",
63+
"sourceType": "org.hsweb.web.datasource.dynamic.DynamicDataSourceProperties"
64+
},
65+
{
66+
"name": "hsweb.dynamic-datasource.borrow-connection-timeout",
67+
"type": "java.lang.Integer",
68+
"sourceType": "org.hsweb.web.datasource.dynamic.DynamicDataSourceProperties"
69+
},
70+
{
71+
"name": "hsweb.dynamic-datasource.reap-timeout",
72+
"type": "java.lang.Integer",
73+
"sourceType": "org.hsweb.web.datasource.dynamic.DynamicDataSourceProperties"
74+
},
75+
{
76+
"name": "hsweb.dynamic-datasource.max-idle-time",
77+
"type": "java.lang.Integer",
78+
"sourceType": "org.hsweb.web.datasource.dynamic.DynamicDataSourceProperties"
79+
},
80+
{
81+
"name": "hsweb.dynamic-datasource.maintenance-interval",
82+
"type": "java.lang.Integer",
83+
"sourceType": "org.hsweb.web.datasource.dynamic.DynamicDataSourceProperties"
84+
},
85+
{
86+
"name": "hsweb.dynamic-datasource.default-isolation-level",
87+
"type": "java.lang.Integer",
88+
"sourceType": "org.hsweb.web.datasource.dynamic.DynamicDataSourceProperties"
89+
},
90+
{
91+
"name": "hsweb.dynamic-datasource.transaction-timeout",
92+
"type": "java.lang.Integer",
93+
"sourceType": "org.hsweb.web.datasource.dynamic.DynamicDataSourceProperties"
94+
},
95+
{
96+
"name": "hsweb.dynamic-datasource.properties",
97+
"type": "java.util.Properties",
98+
"sourceType": "org.hsweb.web.datasource.dynamic.DynamicDataSourceProperties"
1499
}
15100
],
16101
"hints": [

0 commit comments

Comments
 (0)