SpringBoot启动的时候不去校验数据库连接配置是否正确
时间:2021-07-01 10:21:17
帮助过:372人阅读
@Component
2 public class ApplicationContextHelper
implements ApplicationContextAware {
3
4 private static ApplicationContext context;
5
6 @Override
7 public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
8 try {
9 context =
applicationContext;
10 // ===== 在项目初始化bean后检验数据库连接是否
11 DataSource dataSource = (DataSource) context.getBean("dataSource"
);
12 dataSource.getConnection();
13 }
catch (Exception e) {
14 e.printStackTrace();
15 // ===== 当检测数据库连接失败时, 停止项目启动
16 System.exit(-1
);
17 }
18 }
19
20 public ApplicationContext getApplicationContext() {
21 return context;
22 }
23
24 }
SpringBoot启动的时候不去校验数据库连接配置是否正确
标签:nbsp turn trace getbean ati 项目 app stack data