它's confusing to explain, but you'将看到下面的代码 . 当Spring-Boot在其依赖项中以 devtools 开始时,它首先启动,然后立即重新启动第一次通过 devtools . 您可以动态添加命令行参数来跟踪重新启动并更改 devtools 重新启动时使用的Spring配置文件:
@SpringBootApplication
public class App {
public static void main(String[] args)
throws ParseException, IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
String profile = "";
//(1) Very first time - Spring Boot doesn't really load, it only kick start devtool then restarts.
if (args.length == 0) {
args = new String[] { "spring-boot-loaded" };
profile = "no-devtools-yet";
}
//(2) The first time the application loads with devtools
else if (args.length == 1 && args[0].equals("spring-boot-loaded")) {
args = new String[] { "spring-boot-loaded", "devtools-loaded" };
profile = "devtools";
Field argsField = Restarter.class.getDeclaredField("args");
argsField.setAccessible(true);
argsField.set(Restarter.getInstance(), args);
}
//(3) This is the first restart - You don't want to re-initialized the database here
else {
profile = "devtools-reloaded";
}
new SpringApplicationBuilder() //
.sources(App.class)//
.profiles(profile) //
.run(args);
}
}
1 回答
这是一个想法:
它's confusing to explain, but you'将看到下面的代码 . 当Spring-Boot在其依赖项中以
devtools
开始时,它首先启动,然后立即重新启动第一次通过devtools
. 您可以动态添加命令行参数来跟踪重新启动并更改devtools
重新启动时使用的Spring配置文件:粗略的部分是
Restarter
保留原始参数(在本例中为"no-devtools-yet") . 因此,当devtools
首次启动时,您需要替换Restarter
的内部参数