博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
容器启动后执行和执行数据库脚本
阅读量:4672 次
发布时间:2019-06-09

本文共 1597 字,大约阅读时间需要 5 分钟。

1 package com.jt.mongo.demo.modules.init; 2  3 import org.slf4j.Logger; 4 import org.slf4j.LoggerFactory; 5 import org.springframework.context.ApplicationListener; 6 import org.springframework.context.event.ContextRefreshedEvent; 7 import org.springframework.core.io.ClassPathResource; 8 import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; 9 import org.springframework.stereotype.Component;10 11 import javax.annotation.Resource;12 import javax.sql.DataSource;13 import java.sql.SQLException;14 15 @Component16 public class SqlScriptInitExecutor implements ApplicationListener
{17 18 private static final Logger LOGGER = LoggerFactory.getLogger(SqlScriptInitExecutor.class);19 20 @Resource21 private DataSource mysqlDataSource;22 23 @Override24 public void onApplicationEvent(ContextRefreshedEvent event) {25 initMySqlTables();26 }27 28 private void initMySqlTables() {29 try {30 31 ResourceDatabasePopulator populator = new ResourceDatabasePopulator();32 populator.addScript(new ClassPathResource("sql/mysql_pdca_manager.sql"));33 populator.populate(mysqlDataSource.getConnection());34 35 LOGGER.info("init pdca manager tables success.");36 37 } catch (SQLException e) {38 LOGGER.error("init pdca manager tables error", e);39 }40 }41 }

 

ContextRefreshedEvent:
Event raised when an {@code ApplicationContext} gets initialized or refreshed. 当容器初始化或者重启之后实例化到容器中,然后监听他的listener就会执行

转载于:https://www.cnblogs.com/wihainan/p/6237401.html

你可能感兴趣的文章
concurrency runtime学习笔记之二:并行
查看>>
python基础(三)
查看>>
GraphQL实战经验和性能问题的解决方案
查看>>
MySql大数据量恢复
查看>>
java-字符串反转
查看>>
获取一个目录下的所有文件
查看>>
微软发布Sample Browser for Windows 8版:5000示例代码,"触手可及"
查看>>
Windows 10 使用问题
查看>>
linux xargs命令
查看>>
用CSS3实现图像风格
查看>>
转载--黎曼
查看>>
mysql的建表语句
查看>>
免费的HTML5版uploadify
查看>>
机器学习之路:python 集成分类器 随机森林分类RandomForestClassifier 梯度提升决策树分类GradientBoostingClassifier 预测泰坦尼克号幸存者...
查看>>
通过onkeydown事件来控制只允许数字
查看>>
Python实现常用的数据结构
查看>>
snort简介以及在Ubuntu下的安装
查看>>
从SVN资源库下载项目
查看>>
Class.isAssignableFrom(Class clz)方法 与 instanceof 关键字的区别
查看>>
php克隆 自动加载
查看>>