Restore WebServerInitializedEvent listener registration

This commit fixes ServerPortInfoApplicationContextInitializer so that
is registers a listener against `WebServerInitializedEvent`.

A former polish to use a lambda actually introduced a regression as the
listener was registered as `ApplicationListener<?>`.

Closes gh-10047
pull/9830/merge
Stephane Nicoll 7 years ago
parent dd08c31b9a
commit 2f51340047

@ -23,6 +23,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.server.WebServer; import org.springframework.boot.web.server.WebServer;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
@ -48,15 +49,15 @@ import org.springframework.core.env.PropertySource;
* @since 2.0.0 * @since 2.0.0
*/ */
public class ServerPortInfoApplicationContextInitializer public class ServerPortInfoApplicationContextInitializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> { implements ApplicationContextInitializer<ConfigurableApplicationContext>, ApplicationListener<WebServerInitializedEvent> {
@Override @Override
public void initialize(ConfigurableApplicationContext applicationContext) { public void initialize(ConfigurableApplicationContext applicationContext) {
applicationContext.addApplicationListener( applicationContext.addApplicationListener(this);
(WebServerInitializedEvent event) -> onApplicationEvent(event));
} }
protected void onApplicationEvent(WebServerInitializedEvent event) { @Override
public void onApplicationEvent(WebServerInitializedEvent event) {
String propertyName = "local." + event.getServerId() + ".port"; String propertyName = "local." + event.getServerId() + ".port";
setPortProperty(event.getApplicationContext(), propertyName, setPortProperty(event.getApplicationContext(), propertyName,
event.getWebServer().getPort()); event.getWebServer().getPort());

Loading…
Cancel
Save