Skip to content

Commit 11a1bc9

Browse files
committed
polish ApplicationReadyEvent
Rework 7b2b119 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
1 parent 81d25a5 commit 11a1bc9

File tree

5 files changed

+20
-26
lines changed

5 files changed

+20
-26
lines changed

spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
3838
import org.springframework.beans.factory.support.BeanNameGenerator;
3939
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
40-
import org.springframework.boot.context.event.ApplicationReadyEvent;
4140
import org.springframework.context.ApplicationContext;
4241
import org.springframework.context.ApplicationContextInitializer;
4342
import org.springframework.context.ApplicationListener;
@@ -325,7 +324,6 @@ public ConfigurableApplicationContext run(String... args) {
325324
runListener.finished(context, null);
326325
}
327326

328-
context.publishEvent(new ApplicationReadyEvent(context, args));
329327
stopWatch.stop();
330328
if (this.logStartupInfo) {
331329
new StartupInfoLogger(this.mainApplicationClass).logStarted(

spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationFailedEvent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
* Event published by a {@link SpringApplication} when it fails to start.
2424
*
2525
* @author Dave Syer
26+
* @see ApplicationReadyEvent
2627
*/
2728
@SuppressWarnings("serial")
2829
public class ApplicationFailedEvent extends SpringApplicationEvent {

spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationReadyEvent.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,27 @@
1616

1717
package org.springframework.boot.context.event;
1818

19-
import org.springframework.context.ApplicationEvent;
20-
import org.springframework.context.ConfigurableApplicationContext;
19+
import org.springframework.boot.SpringApplication;
2120

2221
/**
2322
* Event published as late as conceivably possible to indicate that the application is
24-
* ready to service requests. The source of the event is the created
25-
* {@link ConfigurableApplicationContext}.
23+
* ready to service requests. The source of the event is the {@link SpringApplication}
24+
* itself, but beware of modifying its internal state since since all initialization
25+
* steps will have been completed by then.
2626
*
2727
* @author Stephane Nicoll
2828
* @since 1.3.0
29+
* @see ApplicationFailedEvent
2930
*/
3031
@SuppressWarnings("serial")
31-
public class ApplicationReadyEvent extends ApplicationEvent {
32-
33-
private final String[] args;
32+
public class ApplicationReadyEvent extends SpringApplicationEvent {
3433

3534
/**
36-
* @param applicationContext the main application context
35+
* @param application the current application
3736
* @param args the arguments the application is running with
3837
*/
39-
public ApplicationReadyEvent(ConfigurableApplicationContext applicationContext, String[] args) {
40-
super(applicationContext);
41-
this.args = args;
42-
}
43-
44-
public ConfigurableApplicationContext getApplicationContext() {
45-
return (ConfigurableApplicationContext) getSource();
46-
}
47-
48-
public String[] getArgs() {
49-
return args;
38+
public ApplicationReadyEvent(SpringApplication application, String[] args) {
39+
super(application, args);
5040
}
5141

5242
}

spring-boot/src/main/java/org/springframework/boot/context/event/EventPublishingRunListener.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -94,6 +94,11 @@ public void finished(ConfigurableApplicationContext context, Throwable exception
9494
this.args, context, exception);
9595
publishEvent(event);
9696
}
97+
else {
98+
ApplicationReadyEvent event = new ApplicationReadyEvent(this.application,
99+
this.args);
100+
publishEvent(event);
101+
}
97102
}
98103

99104
private void publishEvent(SpringApplicationEvent event) {

spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,16 +228,16 @@ public void initialize(ConfigurableApplicationContext context) {
228228
public void applicationRunningEventListener() {
229229
SpringApplication application = new SpringApplication(ExampleConfig.class);
230230
application.setWebEnvironment(false);
231-
final AtomicReference<ApplicationContext> reference = new AtomicReference<ApplicationContext>();
231+
final AtomicReference<SpringApplication> reference = new AtomicReference<SpringApplication>();
232232
class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent> {
233233
@Override
234234
public void onApplicationEvent(ApplicationReadyEvent event) {
235-
reference.set(event.getApplicationContext());
235+
reference.set(event.getSpringApplication());
236236
}
237237
}
238238
application.addListeners(new ApplicationReadyEventListener());
239239
this.context = application.run("--foo=bar");
240-
assertThat(this.context, sameInstance(reference.get()));
240+
assertThat(application, sameInstance(reference.get()));
241241
}
242242

243243
@Test

0 commit comments

Comments
 (0)