Before Springboot automatically instantiates a class, we need to prepare some data to be used when the type is instantiated. After testing, adding the annotation @PostConstruct
to the startup class is effective.
@SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } // springboot正式启动前 @PostConstruct public void postConstruct(){ System.out.println("执行Springboot正式启动前的代码") } }
The methods of implementing Spring's ApplicationRunner
and CommandLineRunner
interfaces are only executed after Springboot is started.
The above is the detailed content of How does Springboot execute code before startup?. For more information, please follow other related articles on the PHP Chinese website!