修改监听tomcat容器启动完成的方式

This commit is contained in:
winLsy
2018-03-27 22:40:54 +08:00
parent 596fb91919
commit 0202e29135
@@ -5,9 +5,7 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.io.IOException;
@@ -25,36 +23,11 @@ import java.util.Map;
*/
@Component
@Slf4j
public class InitSystem implements ApplicationListener<ContextRefreshedEvent> {
public static final String ANNOTATION_CONFIG_EMBEDDED_WEB_APPLICATION_CONTEXT = "AnnotationConfigEmbeddedWebApplicationContext";
public static final String ANNOTATION_CONFIG_APPLICATION_CONTEXT = "AnnotationConfigApplicationContext";
public class InitSystem implements CommandLineRunner {
@Autowired(required = false)
private Map<String,SqlInitProperties> sqlInitPropertiesMap;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ApplicationContext applicationContext = event.getApplicationContext();
ApplicationContext parent = applicationContext.getParent();
// 保证springBoot上下文初始化完毕
if (applicationContext.getDisplayName().contains(ANNOTATION_CONFIG_EMBEDDED_WEB_APPLICATION_CONTEXT)
&& parent.getDisplayName().contains(ANNOTATION_CONFIG_APPLICATION_CONTEXT)) {
if ( sqlInitPropertiesMap != null) {
// 初始化数据表
sqlInitPropertiesMap.forEach((key,value) -> {
if (value != null) {
runInitSql(value.getDriverClassName(),value.getUrl(),value.getUsername(),value.getPassword(),value.getSqlPath());
}
});
log.info("name:{} displayName: {} parentName:{} parentName: {}",
applicationContext.getApplicationName(),
applicationContext.getDisplayName(),
parent.getApplicationName(),
parent.getDisplayName());
}
}
}
/**
* 执行初始化sql
* @param driver 数据库驱动全类名
@@ -82,4 +55,16 @@ public class InitSystem implements ApplicationListener<ContextRefreshedEvent> {
e.printStackTrace();
}
}
@Override
public void run(String... args) throws Exception {
if ( sqlInitPropertiesMap != null) {
// 初始化数据表
sqlInitPropertiesMap.forEach((key,value) -> {
if (value != null) {
runInitSql(value.getDriverClassName(),value.getUrl(),value.getUsername(),value.getPassword(),value.getSqlPath());
}
});
}
}
}