commit | author | age
|
9bcb19
|
1 |
/* |
I |
2 |
Copyright [2020] [https://www.xiaonuo.vip] |
|
3 |
|
|
4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
you may not use this file except in compliance with the License. |
|
6 |
You may obtain a copy of the License at |
|
7 |
|
|
8 |
http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
|
|
10 |
Unless required by applicable law or agreed to in writing, software |
|
11 |
distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
See the License for the specific language governing permissions and |
|
14 |
limitations under the License. |
|
15 |
|
|
16 |
Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: |
|
17 |
|
|
18 |
1.请不要删除和修改根目录下的LICENSE文件。 |
|
19 |
2.请不要删除和修改Snowy源码头部的版权声明。 |
|
20 |
3.请保留源码和相关描述文件的项目出处,作者声明等。 |
|
21 |
4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
22 |
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
23 |
6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip |
|
24 |
*/ |
|
25 |
package vip.xiaonuo.sys.core.listener; |
|
26 |
|
|
27 |
import cn.hutool.core.util.ObjectUtil; |
|
28 |
import cn.hutool.db.DbUtil; |
|
29 |
import cn.hutool.db.Entity; |
|
30 |
import cn.hutool.db.handler.EntityListHandler; |
|
31 |
import cn.hutool.db.sql.SqlExecutor; |
|
32 |
import cn.hutool.log.Log; |
|
33 |
import org.springframework.boot.context.event.ApplicationContextInitializedEvent; |
|
34 |
import org.springframework.context.ApplicationListener; |
|
35 |
import org.springframework.core.Ordered; |
|
36 |
import org.springframework.core.env.ConfigurableEnvironment; |
|
37 |
import vip.xiaonuo.core.consts.CommonConstant; |
|
38 |
import vip.xiaonuo.core.context.constant.ConstantContext; |
|
39 |
import vip.xiaonuo.core.enums.CommonStatusEnum; |
|
40 |
import vip.xiaonuo.core.exception.ServiceException; |
|
41 |
import vip.xiaonuo.sys.modular.consts.enums.SysConfigExceptionEnum; |
|
42 |
|
|
43 |
import java.sql.Connection; |
|
44 |
import java.sql.DriverManager; |
|
45 |
import java.sql.SQLException; |
|
46 |
import java.util.List; |
|
47 |
|
|
48 |
/** |
|
49 |
* 初始化常量的监听器 |
|
50 |
* <p> |
|
51 |
* 当spring装配好配置后,就去数据库读constants |
|
52 |
* |
|
53 |
* @author yubaoshan |
|
54 |
* @date 2020/6/6 23:39 |
|
55 |
*/ |
|
56 |
public class ConstantsInitListener implements ApplicationListener<ApplicationContextInitializedEvent>, Ordered { |
|
57 |
|
|
58 |
private static final Log log = Log.get(); |
|
59 |
|
|
60 |
private static final String CONFIG_LIST_SQL = "select code,value from sys_config where status = ?"; |
|
61 |
|
|
62 |
private static final String CAPITAL_CODE = "CODE"; |
|
63 |
|
|
64 |
private static final String CODE = "code"; |
|
65 |
|
|
66 |
private static final String CAPITAL_VALUE = "VALUE"; |
|
67 |
|
|
68 |
private static final String VALUE = "value"; |
|
69 |
|
|
70 |
@Override |
|
71 |
public int getOrder() { |
|
72 |
return Ordered.HIGHEST_PRECEDENCE; |
|
73 |
} |
|
74 |
|
|
75 |
@Override |
|
76 |
public void onApplicationEvent(ApplicationContextInitializedEvent applicationContextInitializedEvent) { |
|
77 |
ConfigurableEnvironment environment = applicationContextInitializedEvent.getApplicationContext().getEnvironment(); |
|
78 |
|
|
79 |
// 获取数据库连接配置 |
|
80 |
String dataSourceUrl = environment.getProperty("spring.datasource.url"); |
|
81 |
String dataSourceUsername = environment.getProperty("spring.datasource.username"); |
|
82 |
String dataSourcePassword = environment.getProperty("spring.datasource.password"); |
|
83 |
|
|
84 |
// 缓存中放入datasource链接,代码生成时候使用 |
|
85 |
ConstantContext.putConstant(CommonConstant.DATABASE_URL_NAME, dataSourceUrl); |
|
86 |
ConstantContext.putConstant(CommonConstant.DATABASE_DRIVER_NAME, environment.getProperty("spring.datasource.driver-class-name")); |
|
87 |
ConstantContext.putConstant(CommonConstant.DATABASE_USER_NAME, dataSourceUsername); |
|
88 |
|
|
89 |
// 如果有为空的配置,终止执行 |
|
90 |
if (ObjectUtil.hasEmpty(dataSourceUrl, dataSourceUsername)) { |
|
91 |
throw new ServiceException(SysConfigExceptionEnum.DATA_SOURCE_NOT_EXIST); |
|
92 |
} |
|
93 |
|
|
94 |
Connection conn = null; |
|
95 |
try { |
|
96 |
Class.forName(environment.getProperty("spring.datasource.driver-class-name")); |
|
97 |
assert dataSourceUrl != null; |
|
98 |
conn = DriverManager.getConnection(dataSourceUrl, dataSourceUsername, dataSourcePassword); |
|
99 |
|
|
100 |
// 获取sys_config表的数据 |
|
101 |
List<Entity> entityList = SqlExecutor.query(conn, CONFIG_LIST_SQL, new EntityListHandler(), CommonStatusEnum.ENABLE.getCode()); |
|
102 |
|
|
103 |
// 将查询到的参数配置添加到缓存 |
|
104 |
if (ObjectUtil.isNotEmpty(entityList)) { |
|
105 |
entityList.forEach(sysConfig -> |
|
106 |
ConstantContext.putConstant( |
|
107 |
sysConfig.getStr(CODE) == null ? sysConfig.getStr(CAPITAL_CODE) : sysConfig.getStr(CODE), |
|
108 |
sysConfig.getStr(VALUE) == null ? sysConfig.getStr(CAPITAL_VALUE) : sysConfig.getStr(VALUE) |
|
109 |
) |
|
110 |
); |
|
111 |
} |
|
112 |
} catch (SQLException | ClassNotFoundException e) { |
|
113 |
log.error(">>> 读取数据库constants配置信息出错:"); |
|
114 |
e.printStackTrace(); |
|
115 |
throw new ServiceException(SysConfigExceptionEnum.DATA_SOURCE_NOT_EXIST); |
|
116 |
} finally { |
|
117 |
DbUtil.close(conn); |
|
118 |
} |
|
119 |
|
|
120 |
} |
|
121 |
} |