Why spring boot devtools called main method twice?
Asked Answered
D

1

13

I'm using spring boot devtools with my project. When i write

System.out.println("test");

before main, it printing twice in console.

public static void main(String[] args) {

    System.out.println("test");
    SpringApplication.run(TestApplication.class, args);
}

When i remove

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

in pom.xml file,it printing once

Deuterium answered 22/5, 2018 at 8:35 Comment(1)
Thanks! Due to this question, I was able to locate my problem and I will add the information here in case it's helpful to someone else: If the spring-boot-devtools are enabled, all code executed after SpringApplication.run (which means all code executed on the second run) will not call methods that were added by an asm.MethodVisitor to the bytecode. Even though the bytecode is exactly the same, these method calls never get through with the devtools installed. I still have no idea why, but removing the devtools fixes it.Incontestable
I
7

Not only twice, if the spring context need to be refreshed, it will run once more.

I believe it is a designed behavior.

The devtools auto-refresh the context instead of restarting manually.

Interpretive answered 28/6, 2018 at 22:18 Comment(1)
This was doing our QA environment very crazy.Autotype

© 2022 - 2024 — McMap. All rights reserved.