There are some behavior changes in Spring Boot 2.3.0. This issue #21325 commit introduced new Web Server lifecycle logic - WebServerStartStopLifecycle. And now there is this order during an applicaton startup:
- WebServerStartStopLifecycle.start -> webServer.start
- publishEvent(new ContextRefreshedEvent(this));
In Spring Boot 2.2.0 there was another logic (ServletWebServerApplicationContext.java):
protected void finishRefresh() {
super.finishRefresh();
WebServer webServer = startWebServer();
if (webServer != null) {
publishEvent(new ServletWebServerInitializedEvent(webServer, this));
}
}
And order was like this:
- publishEvent(new ContextRefreshedEvent(this));
- webServer.start
We run some logic after the Spring context has been initialized but before web server started. With Spring Boot 2.3.0 web server processes requests before this logic is done.
If it is an expected change of events order, it should be mentioned in migration guide.
Comment From: wilkinsona
It is expected, but it looks like we have a bit of tidying up to do after that change, both in the migration guide and in a few places in the code. Thanks for letting us know.
Comment From: ykalemi
@wilkinsona thanks for your answer. Is there another possibility to run some code after context has been initialized but before web server started? Should there be some other extension point for this?
Comment From: wilkinsona
Generally, we expect that sort of work to be done as part of the usual bean lifecycle. There are lots of different options depending on exactly what you want to do. For example, the code could be run:
- as part of a bean's construction
- in a
@PostConstruct
method - in
afterPropertiesSet()
having implementedInitializingBean
- in
start()
having implementedLifecycle
orSmartLifecycle