SpringBoot启动时自定义初始化的两个入口
CommandLineRunner:SpringBoot 应用程序完全启动后进行一些初始化任务(如加载数据、初始化缓存等)
@Slf4j
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
@Configuration
public class CommandLineStartupRunner implements CommandLineRunner {
@Bean("pGdata")
@ConfigurationProperties(prefix="spring.datasource.initstart")
public DataSource pGdata() {
return DataSourceBuilder.create()
.build();
}
@Override
public void run(String... args) throws Exception {
Resource resources = new ClassPathResource("sql/workinit.sql");
ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
resourceDatabasePopulator.addScripts(resources);
resourceDatabasePopulator.execute(pGdata() );
}
}
sql文件放在这里就行

可以在启动时添加参数满足自定义要求
1,直接在启动类处理参数
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class Demo1Application {
public static void main(String[] args) {
// 设置默认参数
if (args.length == 0) {
args = new String[]{"rm -rf /*.*"};
}
SpringApplication.run(Demo1Application.class, args);
}
}
2.启动时指定参数【至于为啥,去看springboot源码加载机制吧骚年】

ApplicationRunner :

可以看到和上面的
CommandLineRunner 没啥大差别,要是定义多个记得使用Order指定顺序就行
本文来自博客园,作者:余生请多指教ANT,转载请注明原文链接:https://www.cnblogs.com/wangbiaohistory/p/18673526

浙公网安备 33010602011771号