-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppModuleConfig.java
More file actions
151 lines (128 loc) · 3.9 KB
/
AppModuleConfig.java
File metadata and controls
151 lines (128 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.maxleap.config;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* 应用配置,包括数据源信息,需要从环境变量加载
* User:David Young
* Date:2017/11/6
*/
@Configuration
@EnableTransactionManagement
public class AppModuleConfig implements EnvironmentAware{
private String prefix;
private String appId;
private String restAPIKey;
private String masterKey;
private String url;
private String user;
private String password;
public String prefix(){
return "/ext/"+prefix;
}
/**
* 每次服务启动初始化db数据
* 需要防止重复初始化操作
* @param connection
*/
public void initDB(Connection connection) {
try {
DatabaseMetaData metaData = connection.getMetaData();
ResultSet res = metaData.getTables(null, null, "test_user", new String[]{"TABLE"});
if (!res.next()) {
Statement statement = connection.createStatement();
statement.executeUpdate("CREATE TABLE IF NOT EXISTS `test_user`(\n" +
" `id` int(11) unsigned NOT NULL AUTO_INCREMENT,\n" +
" `first_name` varchar(32) NOT NULL,\n" +
" `last_name` varchar(32) NOT NULL,\n" +
" `password` VARCHAR(255) NOT NULL,\n" +
" PRIMARY KEY (`id`)\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");
}
System.out.println("init db success.");
} catch (Throwable tx) {
tx.printStackTrace();
}
}
@Bean
public DataSource primaryDataSource(){
return DataSourceBuilder.create()
.driverClassName("com.mysql.jdbc.Driver")
.username(user)
.password(password)
.url("jdbc:mysql://"+url+"?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true")
.build();
}
@Override
public void setEnvironment(Environment environment) {
this.prefix = environment.getProperty("prefix");
this.appId = environment.getProperty("appId");
this.restAPIKey = environment.getProperty("restAPIKey");
this.masterKey = environment.getProperty("masterKey");
this.url = environment.getProperty("leapCloudMysqlUrl");
this.user = environment.getProperty("leapCloudMysqlUser");
this.password = environment.getProperty("leapCloudMysqlPassword");
}
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getRestAPIKey() {
return restAPIKey;
}
public void setRestAPIKey(String restAPIKey) {
this.restAPIKey = restAPIKey;
}
public String getMasterKey() {
return masterKey;
}
public void setMasterKey(String masterKey) {
this.masterKey = masterKey;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "AppModuleConfig{" +
"prefix='" + prefix + '\'' +
", appId='" + appId + '\'' +
", restAPIKey='" + restAPIKey + '\'' +
", masterKey='" + masterKey + '\'' +
", url='" + url + '\'' +
", user='" + user + '\'' +
", password='" + password + '\'' +
'}';
}
}