From 4dbf55ea13c8ed1e9700a4c6986dc048747759b5 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 23 Mar 2015 09:42:12 -0700 Subject: [PATCH] Pass context to ApplicationReadyEvent Update ApplicationReadyEvent to also include the ApplicationContext. See gh-2638 --- .../context/event/ApplicationFailedEvent.java | 2 +- .../context/event/ApplicationReadyEvent.java | 21 +++++++++++++++---- .../event/EventPublishingRunListener.java | 16 +++++++------- .../boot/SpringApplicationTests.java | 9 +++++--- 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationFailedEvent.java b/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationFailedEvent.java index da8016ae227..d1a6ce26e03 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationFailedEvent.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationFailedEvent.java @@ -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, diff --git a/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationReadyEvent.java b/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationReadyEvent.java index 9860e7c8f48..0401df58052 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationReadyEvent.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationReadyEvent.java @@ -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; } } diff --git a/spring-boot/src/main/java/org/springframework/boot/context/event/EventPublishingRunListener.java b/spring-boot/src/main/java/org/springframework/boot/context/event/EventPublishingRunListener.java index 9e9b23025b6..3dffba480cc 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/event/EventPublishingRunListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/event/EventPublishingRunListener.java @@ -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) { diff --git a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java index 2a11e3dfce8..53781d18a4d 100644 --- a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -229,7 +229,8 @@ public class SpringApplicationTests { SpringApplication application = new SpringApplication(ExampleConfig.class); application.setWebEnvironment(false); final AtomicReference reference = new AtomicReference(); - class ApplicationReadyEventListener implements ApplicationListener { + class ApplicationReadyEventListener implements + ApplicationListener { @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 events = new ArrayList(); - class ApplicationRunningEventListener implements ApplicationListener { + class ApplicationRunningEventListener implements + ApplicationListener { @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)));