From 11a1bc93c426f6fcddfe9ac1a311f76c756d253e Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 18 Mar 2015 10:03:32 +0100 Subject: [PATCH] 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 --- .../boot/SpringApplication.java | 2 -- .../context/event/ApplicationFailedEvent.java | 3 +- .../context/event/ApplicationReadyEvent.java | 28 ++++++------------- .../event/EventPublishingRunListener.java | 7 ++++- .../boot/SpringApplicationTests.java | 6 ++-- 5 files changed, 20 insertions(+), 26 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index 22a0b6205d3..598b07c83ff 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -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( diff --git a/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationFailedEvent.java b/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationFailedEvent.java index 5a3961e7146..da8016ae227 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationFailedEvent.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationFailedEvent.java @@ -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; * Event published by a {@link SpringApplication} when it fails to start. * * @author Dave Syer + * @see ApplicationReadyEvent */ @SuppressWarnings("serial") public class ApplicationFailedEvent extends SpringApplicationEvent { diff --git a/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationReadyEvent.java b/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationReadyEvent.java index ab714bb063f..9860e7c8f48 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationReadyEvent.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationReadyEvent.java @@ -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); } } diff --git a/spring-boot/src/main/java/org/springframework/boot/context/event/EventPublishingRunListener.java b/spring-boot/src/main/java/org/springframework/boot/context/event/EventPublishingRunListener.java index 5bf98672fb0..9e9b23025b6 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/event/EventPublishingRunListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/event/EventPublishingRunListener.java @@ -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 this.args, context, exception); publishEvent(event); } + else { + ApplicationReadyEvent event = new ApplicationReadyEvent(this.application, + this.args); + publishEvent(event); + } } private void publishEvent(SpringApplicationEvent event) { diff --git a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java index 4fbd929865d..2a11e3dfce8 100644 --- a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -228,16 +228,16 @@ public class SpringApplicationTests { public void applicationRunningEventListener() { SpringApplication application = new SpringApplication(ExampleConfig.class); application.setWebEnvironment(false); - final AtomicReference reference = new AtomicReference(); + final AtomicReference reference = new AtomicReference(); class ApplicationReadyEventListener implements ApplicationListener { @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