diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedServletContainerInitializedEvent.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedServletContainerInitializedEvent.java index bbece783595..0019f8b5d8d 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedServletContainerInitializedEvent.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedServletContainerInitializedEvent.java @@ -16,6 +16,7 @@ package org.springframework.boot.context.embedded; +import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationEvent; /** @@ -28,13 +29,32 @@ import org.springframework.context.ApplicationEvent; */ public class EmbeddedServletContainerInitializedEvent extends ApplicationEvent { - public EmbeddedServletContainerInitializedEvent(EmbeddedServletContainer source) { + private ApplicationContext applicationContext; + + public EmbeddedServletContainerInitializedEvent( + ApplicationContext applicationContext, EmbeddedServletContainer source) { super(source); + this.applicationContext = applicationContext; } + /** + * Access the source of the event (an {@link EmbeddedServletContainer}). + * + * @return the embedded servlet container + */ @Override public EmbeddedServletContainer getSource() { return (EmbeddedServletContainer) super.getSource(); } + /** + * Access the application context that the container was created in. Sometimes it is + * prudent to check that this matches expectations (like being equal to the current + * context) before acting on the server container itself. + * + * @return the applicationContext that the container was created from + */ + public ApplicationContext getApplicationContext() { + return this.applicationContext; + } } diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContext.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContext.java index c9b46593303..1d9506fd454 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContext.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContext.java @@ -147,7 +147,7 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext super.finishRefresh(); startEmbeddedServletContainer(); if (this.embeddedServletContainer != null) { - publishEvent(new EmbeddedServletContainerInitializedEvent( + publishEvent(new EmbeddedServletContainerInitializedEvent(this, this.embeddedServletContainer)); } } diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContextTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContextTests.java index f953e5ad2ec..8b876b16ad4 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContextTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContextTests.java @@ -47,6 +47,7 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; @@ -128,6 +129,7 @@ public class EmbeddedWebApplicationContextTests { MockListener.class).getEvent(); assertNotNull(event); assertTrue(event.getSource().getPort() >= 0); + assertEquals(this.context, event.getApplicationContext()); } @Test