Browse Source

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
pull/2683/head
Stephane Nicoll 11 years ago
parent
commit
11a1bc93c4
  1. 2
      spring-boot/src/main/java/org/springframework/boot/SpringApplication.java
  2. 3
      spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationFailedEvent.java
  3. 28
      spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationReadyEvent.java
  4. 7
      spring-boot/src/main/java/org/springframework/boot/context/event/EventPublishingRunListener.java
  5. 6
      spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java

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

@ -37,7 +37,6 @@ import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader; @@ -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 { @@ -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(

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

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -23,6 +23,7 @@ import org.springframework.context.ConfigurableApplicationContext; @@ -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 {

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

@ -16,37 +16,27 @@ @@ -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);
}
}

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

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -94,6 +94,11 @@ public class EventPublishingRunListener implements SpringApplicationRunListener @@ -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) {

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

@ -228,16 +228,16 @@ public class SpringApplicationTests { @@ -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

Loading…
Cancel
Save