Pass context to ApplicationReadyEvent

Update ApplicationReadyEvent to also include the ApplicationContext.

See gh-2638
This commit is contained in:
Phillip Webb 2015-03-23 09:42:12 -07:00
parent 41a6fa9335
commit 4dbf55ea13
4 changed files with 32 additions and 16 deletions

View File

@ -34,8 +34,8 @@ public class ApplicationFailedEvent extends SpringApplicationEvent {
/**
* @param application the current application
* @param context the context that was being created (maybe null)
* @param args the arguments the application was running with
* @param context the context that was being created (maybe null)
* @param exception the exception that caused the error
*/
public ApplicationFailedEvent(SpringApplication application, String[] args,

View File

@ -17,12 +17,13 @@
package org.springframework.boot.context.event;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
/**
* Event published as late as conceivably possible to indicate that the application is
* ready to service requests. The source of the event is the {@link SpringApplication}
* itself, but beware of modifying its internal state since since all initialization
* steps will have been completed by then.
* ready to service requests. The source of the event is the {@link SpringApplication}
* itself, but beware of modifying its internal state since since all initialization steps
* will have been completed by then.
*
* @author Stephane Nicoll
* @since 1.3.0
@ -31,12 +32,24 @@ import org.springframework.boot.SpringApplication;
@SuppressWarnings("serial")
public class ApplicationReadyEvent extends SpringApplicationEvent {
private final ConfigurableApplicationContext context;
/**
* @param application the current application
* @param args the arguments the application is running with
* @param context the context that was being created (maybe null)
*/
public ApplicationReadyEvent(SpringApplication application, String[] args) {
public ApplicationReadyEvent(SpringApplication application, String[] args,
ConfigurableApplicationContext context) {
super(application, args);
this.context = context;
}
/**
* @return the context
*/
public ConfigurableApplicationContext getApplicationContext() {
return this.context;
}
}

View File

@ -89,16 +89,16 @@ public class EventPublishingRunListener implements SpringApplicationRunListener
@Override
public void finished(ConfigurableApplicationContext context, Throwable exception) {
publishEvent(getFinishedEvent(context, exception));
}
private SpringApplicationEvent getFinishedEvent(
ConfigurableApplicationContext context, Throwable exception) {
if (exception != null) {
ApplicationFailedEvent event = new ApplicationFailedEvent(this.application,
this.args, context, exception);
publishEvent(event);
}
else {
ApplicationReadyEvent event = new ApplicationReadyEvent(this.application,
this.args);
publishEvent(event);
return new ApplicationFailedEvent(this.application, this.args, context,
exception);
}
return new ApplicationReadyEvent(this.application, this.args, context);
}
private void publishEvent(SpringApplicationEvent event) {

View File

@ -229,7 +229,8 @@ public class SpringApplicationTests {
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebEnvironment(false);
final AtomicReference<SpringApplication> reference = new AtomicReference<SpringApplication>();
class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent> {
class ApplicationReadyEventListener implements
ApplicationListener<ApplicationReadyEvent> {
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
reference.set(event.getSpringApplication());
@ -263,7 +264,8 @@ public class SpringApplicationTests {
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebEnvironment(false);
final List<ApplicationEvent> events = new ArrayList<ApplicationEvent>();
class ApplicationRunningEventListener implements ApplicationListener<ApplicationEvent> {
class ApplicationRunningEventListener implements
ApplicationListener<ApplicationEvent> {
@Override
public void onApplicationEvent(ApplicationEvent event) {
events.add((event));
@ -273,7 +275,8 @@ public class SpringApplicationTests {
this.context = application.run();
assertThat(5, is(events.size()));
assertThat(events.get(0), is(instanceOf(ApplicationStartedEvent.class)));
assertThat(events.get(1), is(instanceOf(ApplicationEnvironmentPreparedEvent.class)));
assertThat(events.get(1),
is(instanceOf(ApplicationEnvironmentPreparedEvent.class)));
assertThat(events.get(2), is(instanceOf(ApplicationPreparedEvent.class)));
assertThat(events.get(3), is(instanceOf(ContextRefreshedEvent.class)));
assertThat(events.get(4), is(instanceOf(ApplicationReadyEvent.class)));