zoukankan      html  css  js  c++  java
  • 容器启动后执行和执行数据库脚本

     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 @Component
    16 public class SqlScriptInitExecutor implements ApplicationListener<ContextRefreshedEvent> {
    17 
    18     private static final Logger LOGGER = LoggerFactory.getLogger(SqlScriptInitExecutor.class);
    19 
    20     @Resource
    21     private DataSource mysqlDataSource;
    22 
    23     @Override
    24     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就会执行
  • 相关阅读:
    Bind和Eval的区别详解(ZT)
    .net中IndexOf和LastIndexOf的区别 
    个人学习代码保存:例3. GridView相关练习操作
    Convert.ToInt32和int.Parse有什么区别?
    个人学习代码保存:例1.asp.net DataList相关操作
    Sum(构造,枚举)
    1278 相离的圆(51nod)
    Wheels(bfs遍历,CERC 2014)
    详解 Android 的 Activity 组件
    Android应用程序变量
  • 原文地址:https://www.cnblogs.com/wihainan/p/6237401.html
Copyright © 2011-2022 走看看