Add ApplicationContext to the EmbeddedServletContainerInitializedEvent

pull/184/head
Dave Syer 11 years ago
parent 4d60b09c9b
commit c9c433608f

@ -16,6 +16,7 @@
package org.springframework.boot.context.embedded; package org.springframework.boot.context.embedded;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
/** /**
@ -28,13 +29,32 @@ import org.springframework.context.ApplicationEvent;
*/ */
public class EmbeddedServletContainerInitializedEvent extends ApplicationEvent { public class EmbeddedServletContainerInitializedEvent extends ApplicationEvent {
public EmbeddedServletContainerInitializedEvent(EmbeddedServletContainer source) { private ApplicationContext applicationContext;
public EmbeddedServletContainerInitializedEvent(
ApplicationContext applicationContext, EmbeddedServletContainer source) {
super(source); super(source);
this.applicationContext = applicationContext;
} }
/**
* Access the source of the event (an {@link EmbeddedServletContainer}).
*
* @return the embedded servlet container
*/
@Override @Override
public EmbeddedServletContainer getSource() { public EmbeddedServletContainer getSource() {
return (EmbeddedServletContainer) super.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(); super.finishRefresh();
startEmbeddedServletContainer(); startEmbeddedServletContainer();
if (this.embeddedServletContainer != null) { if (this.embeddedServletContainer != null) {
publishEvent(new EmbeddedServletContainerInitializedEvent( publishEvent(new EmbeddedServletContainerInitializedEvent(this,
this.embeddedServletContainer)); this.embeddedServletContainer));
} }
} }

@ -47,6 +47,7 @@ import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -128,6 +129,7 @@ public class EmbeddedWebApplicationContextTests {
MockListener.class).getEvent(); MockListener.class).getEvent();
assertNotNull(event); assertNotNull(event);
assertTrue(event.getSource().getPort() >= 0); assertTrue(event.getSource().getPort() >= 0);
assertEquals(this.context, event.getApplicationContext());
} }
@Test @Test

Loading…
Cancel
Save