容器啟動後執行和執行資料庫指令碼

來源:互聯網
上載者:User

標籤:exec   ram   base   ror   manage   dev   package   ini   容器   

 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<ContextRefreshedEvent> {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就會執行

容器啟動後執行和執行資料庫指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.