polish ApplicationReadyEvent

Rework 7b2b11903a so that it is more aligned with others spring
application events. Fix the package tangle by moving the publication part
to EventPublishingRunListener.

Closes gh-2638
This commit is contained in:
Stephane Nicoll
2015-03-18 10:03:32 +01:00
parent 81d25a5705
commit 11a1bc93c4
5 changed files with 20 additions and 26 deletions
@@ -37,7 +37,6 @@ import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
@@ -325,7 +324,6 @@ public class SpringApplication {
runListener.finished(context, null); runListener.finished(context, null);
} }
context.publishEvent(new ApplicationReadyEvent(context, args));
stopWatch.stop(); stopWatch.stop();
if (this.logStartupInfo) { if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted( new StartupInfoLogger(this.mainApplicationClass).logStarted(
@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2014 the original author or authors. * Copyright 2012-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@ import org.springframework.context.ConfigurableApplicationContext;
* Event published by a {@link SpringApplication} when it fails to start. * Event published by a {@link SpringApplication} when it fails to start.
* *
* @author Dave Syer * @author Dave Syer
* @see ApplicationReadyEvent
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class ApplicationFailedEvent extends SpringApplicationEvent { public class ApplicationFailedEvent extends SpringApplicationEvent {
@@ -16,37 +16,27 @@
package org.springframework.boot.context.event; package org.springframework.boot.context.event;
import org.springframework.context.ApplicationEvent; 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 created * ready to service requests. The source of the event is the {@link SpringApplication}
* {@link ConfigurableApplicationContext}. * itself, but beware of modifying its internal state since since all initialization
* steps will have been completed by then.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 1.3.0 * @since 1.3.0
* @see ApplicationFailedEvent
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class ApplicationReadyEvent extends ApplicationEvent { public class ApplicationReadyEvent extends SpringApplicationEvent {
private final String[] args;
/** /**
* @param applicationContext the main application context * @param application the current application
* @param args the arguments the application is running with * @param args the arguments the application is running with
*/ */
public ApplicationReadyEvent(ConfigurableApplicationContext applicationContext, String[] args) { public ApplicationReadyEvent(SpringApplication application, String[] args) {
super(applicationContext); super(application, args);
this.args = args;
}
public ConfigurableApplicationContext getApplicationContext() {
return (ConfigurableApplicationContext) getSource();
}
public String[] getArgs() {
return args;
} }
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2014 the original author or authors. * Copyright 2012-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -94,6 +94,11 @@ public class EventPublishingRunListener implements SpringApplicationRunListener
this.args, context, exception); this.args, context, exception);
publishEvent(event); publishEvent(event);
} }
else {
ApplicationReadyEvent event = new ApplicationReadyEvent(this.application,
this.args);
publishEvent(event);
}
} }
private void publishEvent(SpringApplicationEvent event) { private void publishEvent(SpringApplicationEvent event) {
@@ -228,16 +228,16 @@ public class SpringApplicationTests {
public void applicationRunningEventListener() { public void applicationRunningEventListener() {
SpringApplication application = new SpringApplication(ExampleConfig.class); SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebEnvironment(false); application.setWebEnvironment(false);
final AtomicReference<ApplicationContext> reference = new AtomicReference<ApplicationContext>(); 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.getApplicationContext()); reference.set(event.getSpringApplication());
} }
} }
application.addListeners(new ApplicationReadyEventListener()); application.addListeners(new ApplicationReadyEventListener());
this.context = application.run("--foo=bar"); this.context = application.run("--foo=bar");
assertThat(this.context, sameInstance(reference.get())); assertThat(application, sameInstance(reference.get()));
} }
@Test @Test