在Spring Boot应用程序中,可以通过两种方式运行特定代码:
例如,创建一个实现ApplicationRunner接口的类:
@Component
public class MyApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
// 在应用程序启动后执行的代码
System.out.println("应用程序启动后执行的代码");
}
}
例如,创建一个带有@PostConstruct注解的方法:
@Component
public class MyBean {
@PostConstruct
public void init() {
// 在应用程序启动后执行的代码
System.out.println("应用程序启动后执行的代码");
}
}
无论您选择哪种方式,都可以在应用程序启动后运行特定代码。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:springboot启动类运行怎么配置