Spring Boot启动后运行代码 [英] Running code after Spring Boot starts
问题描述
我希望在 spring-boot 应用程序开始监视目录以进行更改后运行代码。
I want to run code after my spring-boot app starts to monitor a directory for changes.
我尝试过运行新线程,但此时尚未设置 @Autowired
服务。
I have tried running a new thread but the @Autowired
services have not been set at that point.
我能找到 ApplicationPreparedEvent
,它在 @Autowired $ c之前触发$ c>注释已设置。理想情况下,我希望在应用程序准备好处理http请求后触发事件。
I have been able to find ApplicationPreparedEvent
, which fires before the @Autowired
annotations are set. Ideally I would like the event to fire once the application is ready to process http requests.
是否有更好的事件要使用,或者更好的方法是在应用程序在 spring-boot 中生效?
Is there a better event to use, or a better way of running code after the application is live in spring-boot?
推荐答案
尝试:
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application extends SpringBootServletInitializer {
@SuppressWarnings("resource")
public static void main(final String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
context.getBean(Table.class).fillWithTestdata(); // <-- here
}
}
这篇关于Spring Boot启动后运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!