Add ApplicationContext to the EmbeddedServletContainerInitializedEvent
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user