Pass context to ApplicationReadyEvent
Update ApplicationReadyEvent to also include the ApplicationContext. See gh-2638
This commit is contained in:
@@ -34,8 +34,8 @@ public class ApplicationFailedEvent extends SpringApplicationEvent {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param application the current application
|
* @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 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
|
* @param exception the exception that caused the error
|
||||||
*/
|
*/
|
||||||
public ApplicationFailedEvent(SpringApplication application, String[] args,
|
public ApplicationFailedEvent(SpringApplication application, String[] args,
|
||||||
|
|||||||
@@ -17,12 +17,13 @@
|
|||||||
package org.springframework.boot.context.event;
|
package org.springframework.boot.context.event;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Event published as late as conceivably possible to indicate that the application is
|
* 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}
|
* 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
|
* itself, but beware of modifying its internal state since since all initialization steps
|
||||||
* steps will have been completed by then.
|
* will have been completed by then.
|
||||||
*
|
*
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
* @since 1.3.0
|
* @since 1.3.0
|
||||||
@@ -31,12 +32,24 @@ import org.springframework.boot.SpringApplication;
|
|||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
public class ApplicationReadyEvent extends SpringApplicationEvent {
|
public class ApplicationReadyEvent extends SpringApplicationEvent {
|
||||||
|
|
||||||
|
private final ConfigurableApplicationContext context;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param application the current application
|
* @param application the current application
|
||||||
* @param args the arguments the application is running with
|
* @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);
|
super(application, args);
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the context
|
||||||
|
*/
|
||||||
|
public ConfigurableApplicationContext getApplicationContext() {
|
||||||
|
return this.context;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -89,16 +89,16 @@ public class EventPublishingRunListener implements SpringApplicationRunListener
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void finished(ConfigurableApplicationContext context, Throwable exception) {
|
public void finished(ConfigurableApplicationContext context, Throwable exception) {
|
||||||
|
publishEvent(getFinishedEvent(context, exception));
|
||||||
|
}
|
||||||
|
|
||||||
|
private SpringApplicationEvent getFinishedEvent(
|
||||||
|
ConfigurableApplicationContext context, Throwable exception) {
|
||||||
if (exception != null) {
|
if (exception != null) {
|
||||||
ApplicationFailedEvent event = new ApplicationFailedEvent(this.application,
|
return new ApplicationFailedEvent(this.application, this.args, context,
|
||||||
this.args, context, exception);
|
exception);
|
||||||
publishEvent(event);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
ApplicationReadyEvent event = new ApplicationReadyEvent(this.application,
|
|
||||||
this.args);
|
|
||||||
publishEvent(event);
|
|
||||||
}
|
}
|
||||||
|
return new ApplicationReadyEvent(this.application, this.args, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void publishEvent(SpringApplicationEvent event) {
|
private void publishEvent(SpringApplicationEvent event) {
|
||||||
|
|||||||
@@ -229,7 +229,8 @@ public class SpringApplicationTests {
|
|||||||
SpringApplication application = new SpringApplication(ExampleConfig.class);
|
SpringApplication application = new SpringApplication(ExampleConfig.class);
|
||||||
application.setWebEnvironment(false);
|
application.setWebEnvironment(false);
|
||||||
final AtomicReference<SpringApplication> reference = new AtomicReference<SpringApplication>();
|
final AtomicReference<SpringApplication> reference = new AtomicReference<SpringApplication>();
|
||||||
class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent> {
|
class ApplicationReadyEventListener implements
|
||||||
|
ApplicationListener<ApplicationReadyEvent> {
|
||||||
@Override
|
@Override
|
||||||
public void onApplicationEvent(ApplicationReadyEvent event) {
|
public void onApplicationEvent(ApplicationReadyEvent event) {
|
||||||
reference.set(event.getSpringApplication());
|
reference.set(event.getSpringApplication());
|
||||||
@@ -263,7 +264,8 @@ public class SpringApplicationTests {
|
|||||||
SpringApplication application = new SpringApplication(ExampleConfig.class);
|
SpringApplication application = new SpringApplication(ExampleConfig.class);
|
||||||
application.setWebEnvironment(false);
|
application.setWebEnvironment(false);
|
||||||
final List<ApplicationEvent> events = new ArrayList<ApplicationEvent>();
|
final List<ApplicationEvent> events = new ArrayList<ApplicationEvent>();
|
||||||
class ApplicationRunningEventListener implements ApplicationListener<ApplicationEvent> {
|
class ApplicationRunningEventListener implements
|
||||||
|
ApplicationListener<ApplicationEvent> {
|
||||||
@Override
|
@Override
|
||||||
public void onApplicationEvent(ApplicationEvent event) {
|
public void onApplicationEvent(ApplicationEvent event) {
|
||||||
events.add((event));
|
events.add((event));
|
||||||
@@ -273,7 +275,8 @@ public class SpringApplicationTests {
|
|||||||
this.context = application.run();
|
this.context = application.run();
|
||||||
assertThat(5, is(events.size()));
|
assertThat(5, is(events.size()));
|
||||||
assertThat(events.get(0), is(instanceOf(ApplicationStartedEvent.class)));
|
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(2), is(instanceOf(ApplicationPreparedEvent.class)));
|
||||||
assertThat(events.get(3), is(instanceOf(ContextRefreshedEvent.class)));
|
assertThat(events.get(3), is(instanceOf(ContextRefreshedEvent.class)));
|
||||||
assertThat(events.get(4), is(instanceOf(ApplicationReadyEvent.class)));
|
assertThat(events.get(4), is(instanceOf(ApplicationReadyEvent.class)));
|
||||||
|
|||||||
Reference in New Issue
Block a user