Springboot怎么在启动前执行代码

在Springboot自动实例化类前,我们需要准备一些数据在类型实例化的时候使用。 经过测试,在启动类添加注解@PostConstruct有效。

@SpringBootApplication
public class MyApplication {
	public static void main(String[] args) {
		SpringApplication.run(MyApplication.class, args);
	}
	// springboot正式启动前
	@PostConstruct
	public void postConstruct(){
		System.out.println("执行Springboot正式启动前的代码")
	}
}

实现Spring的ApplicationRunnerCommandLineRunner接口的办法,都只在Springboot启动完成后才执行。

以上就是Springboot怎么在启动前执行代码的详细内容,更多请关注www.sxiaw.com其它相关文章!