Commit 11a1bc93 authored by Stephane Nicoll's avatar Stephane Nicoll

polish ApplicationReadyEvent

Rework 7b2b1190 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
parent 81d25a57
......@@ -37,7 +37,6 @@ import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ApplicationListener;
......@@ -325,7 +324,6 @@ public class SpringApplication {
runListener.finished(context, null);
}
context.publishEvent(new ApplicationReadyEvent(context, args));
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(
......
/*
* 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");
* 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.
*
* @author Dave Syer
* @see ApplicationReadyEvent
*/
@SuppressWarnings("serial")
public class ApplicationFailedEvent extends SpringApplicationEvent {
......
......@@ -16,37 +16,27 @@
package org.springframework.boot.context.event;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.boot.SpringApplication;
/**
* 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
* {@link ConfigurableApplicationContext}.
* 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
* @see ApplicationFailedEvent
*/
@SuppressWarnings("serial")
public class ApplicationReadyEvent extends ApplicationEvent {
private final String[] args;
public class ApplicationReadyEvent extends SpringApplicationEvent {
/**
* @param applicationContext the main application context
* @param application the current application
* @param args the arguments the application is running with
*/
public ApplicationReadyEvent(ConfigurableApplicationContext applicationContext, String[] args) {
super(applicationContext);
this.args = args;
}
public ConfigurableApplicationContext getApplicationContext() {
return (ConfigurableApplicationContext) getSource();
}
public String[] getArgs() {
return args;
public ApplicationReadyEvent(SpringApplication application, String[] args) {
super(application, args);
}
}
/*
* 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");
* 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);
publishEvent(event);
}
else {
ApplicationReadyEvent event = new ApplicationReadyEvent(this.application,
this.args);
publishEvent(event);
}
}
private void publishEvent(SpringApplicationEvent event) {
......
......@@ -228,16 +228,16 @@ public class SpringApplicationTests {
public void applicationRunningEventListener() {
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebEnvironment(false);
final AtomicReference<ApplicationContext> reference = new AtomicReference<ApplicationContext>();
final AtomicReference<SpringApplication> reference = new AtomicReference<SpringApplication>();
class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent> {
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
reference.set(event.getApplicationContext());
reference.set(event.getSpringApplication());
}
}
application.addListeners(new ApplicationReadyEventListener());
this.context = application.run("--foo=bar");
assertThat(this.context, sameInstance(reference.get()));
assertThat(application, sameInstance(reference.get()));
}
@Test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment