Skip to content

Commit c9c4336

Browse files
author
Dave Syer
committed
Add ApplicationContext to the EmbeddedServletContainerInitializedEvent
1 parent 4d60b09 commit c9c4336

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedServletContainerInitializedEvent.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.context.embedded;
1818

19+
import org.springframework.context.ApplicationContext;
1920
import org.springframework.context.ApplicationEvent;
2021

2122
/**
@@ -28,13 +29,32 @@
2829
*/
2930
public class EmbeddedServletContainerInitializedEvent extends ApplicationEvent {
3031

31-
public EmbeddedServletContainerInitializedEvent(EmbeddedServletContainer source) {
32+
private ApplicationContext applicationContext;
33+
34+
public EmbeddedServletContainerInitializedEvent(
35+
ApplicationContext applicationContext, EmbeddedServletContainer source) {
3236
super(source);
37+
this.applicationContext = applicationContext;
3338
}
3439

40+
/**
41+
* Access the source of the event (an {@link EmbeddedServletContainer}).
42+
*
43+
* @return the embedded servlet container
44+
*/
3545
@Override
3646
public EmbeddedServletContainer getSource() {
3747
return (EmbeddedServletContainer) super.getSource();
3848
}
3949

50+
/**
51+
* Access the application context that the container was created in. Sometimes it is
52+
* prudent to check that this matches expectations (like being equal to the current
53+
* context) before acting on the server container itself.
54+
*
55+
* @return the applicationContext that the container was created from
56+
*/
57+
public ApplicationContext getApplicationContext() {
58+
return this.applicationContext;
59+
}
4060
}

spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ protected void finishRefresh() {
147147
super.finishRefresh();
148148
startEmbeddedServletContainer();
149149
if (this.embeddedServletContainer != null) {
150-
publishEvent(new EmbeddedServletContainerInitializedEvent(
150+
publishEvent(new EmbeddedServletContainerInitializedEvent(this,
151151
this.embeddedServletContainer));
152152
}
153153
}

spring-boot/src/test/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContextTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import static org.hamcrest.Matchers.instanceOf;
4848
import static org.hamcrest.Matchers.not;
4949
import static org.hamcrest.Matchers.nullValue;
50+
import static org.junit.Assert.assertEquals;
5051
import static org.junit.Assert.assertNotNull;
5152
import static org.junit.Assert.assertThat;
5253
import static org.junit.Assert.assertTrue;
@@ -128,6 +129,7 @@ public void containerEventPublished() throws Exception {
128129
MockListener.class).getEvent();
129130
assertNotNull(event);
130131
assertTrue(event.getSource().getPort() >= 0);
132+
assertEquals(this.context, event.getApplicationContext());
131133
}
132134

133135
@Test

0 commit comments

Comments
 (0)