From 92f01cf9bc9689b2b4479e96ada8f0d8f959a720 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 31 Jan 2014 10:59:57 -0800 Subject: [PATCH] Rename SpringApplication Events Move SpringApplication events to their own package, create a common base class and rename classes to match Spring conventions. --- ...ConfigurationReportLoggingInitializer.java | 8 ++-- ...gurationReportLoggingInitializerTests.java | 14 +++--- .../SecurityAutoConfigurationTests.java | 6 +-- .../boot/SpringApplication.java | 29 +++++++----- .../ClasspathLoggingApplicationListener.java | 20 ++++----- .../ConfigFileApplicationListener.java | 6 +-- ...nvironmentDelegateApplicationListener.java | 6 +-- .../FileEncodingApplicationListener.java | 8 ++-- .../listener/LoggingApplicationListener.java | 13 +++--- .../listener/VcapApplicationListener.java | 8 ++-- .../ApplicationEnvironmentPreparedEvent.java} | 35 ++++----------- .../ApplicationFailedEvent.java} | 35 +++++---------- .../ApplicationPreparedEvent.java} | 38 +++++----------- .../ApplicationStartedEvent.java} | 31 +++---------- .../boot/event/SpringApplicationEvent.java | 44 +++++++++++++++++++ .../LiquibaseServiceLocatorInitializer.java | 6 +-- .../ServletListenerRegistrationBeanTests.java | 5 ++- .../ConfigFileApplicationListenerTests.java | 6 +-- ...nmentDelegateApplicationListenerTests.java | 14 +++--- .../FileEncodingApplicationListenerTests.java | 6 +-- .../LoggingApplicationListenerTests.java | 6 +-- .../VcapApplicationListenerTests.java | 6 +-- .../logback/LogbackLoggingSystemTests.java | 4 +- 23 files changed, 170 insertions(+), 184 deletions(-) rename spring-boot/src/main/java/org/springframework/boot/{SpringApplicationEnvironmentAvailableEvent.java => event/ApplicationEnvironmentPreparedEvent.java} (62%) rename spring-boot/src/main/java/org/springframework/boot/{SpringApplicationErrorEvent.java => event/ApplicationFailedEvent.java} (64%) rename spring-boot/src/main/java/org/springframework/boot/{SpringApplicationBeforeRefreshEvent.java => event/ApplicationPreparedEvent.java} (58%) rename spring-boot/src/main/java/org/springframework/boot/{SpringApplicationStartEvent.java => event/ApplicationStartedEvent.java} (66%) create mode 100644 spring-boot/src/main/java/org/springframework/boot/event/SpringApplicationEvent.java diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigurationReportLoggingInitializer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigurationReportLoggingInitializer.java index eb427c184e..e261f2df2c 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigurationReportLoggingInitializer.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigurationReportLoggingInitializer.java @@ -20,9 +20,9 @@ import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.boot.SpringApplicationErrorEvent; import org.springframework.boot.autoconfigure.AutoConfigurationReport.ConditionAndOutcome; import org.springframework.boot.autoconfigure.AutoConfigurationReport.ConditionAndOutcomes; +import org.springframework.boot.event.ApplicationFailedEvent; import org.springframework.boot.logging.LogLevel; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ApplicationEvent; @@ -76,7 +76,7 @@ public class AutoConfigurationReportLoggingInitializer implements @Override public boolean supportsEventType(Class type) { return ContextRefreshedEvent.class.isAssignableFrom(type) - || SpringApplicationErrorEvent.class.isAssignableFrom(type); + || ApplicationFailedEvent.class.isAssignableFrom(type); } @Override @@ -91,8 +91,8 @@ public class AutoConfigurationReportLoggingInitializer implements logAutoConfigurationReport(); } } - else if (event instanceof SpringApplicationErrorEvent) { - if (((SpringApplicationErrorEvent) event).getApplicationContext() == this.applicationContext) { + else if (event instanceof ApplicationFailedEvent) { + if (((ApplicationFailedEvent) event).getApplicationContext() == this.applicationContext) { logAutoConfigurationReport(true); } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AutoConfigurationReportLoggingInitializerTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AutoConfigurationReportLoggingInitializerTests.java index 288033f415..59d67704f1 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AutoConfigurationReportLoggingInitializerTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AutoConfigurationReportLoggingInitializerTests.java @@ -30,9 +30,9 @@ import org.junit.Test; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import org.springframework.boot.SpringApplication; -import org.springframework.boot.SpringApplicationErrorEvent; import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; +import org.springframework.boot.event.ApplicationFailedEvent; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -127,8 +127,8 @@ public class AutoConfigurationReportLoggingInitializerTests { fail("Did not error"); } catch (Exception ex) { - this.initializer.onApplicationEvent(new SpringApplicationErrorEvent( - new SpringApplication(), context, new String[] {}, ex)); + this.initializer.onApplicationEvent(new ApplicationFailedEvent( + new SpringApplication(), new String[0], context, ex)); } assertThat(this.debugLog.size(), not(equalTo(0))); @@ -146,8 +146,8 @@ public class AutoConfigurationReportLoggingInitializerTests { fail("Did not error"); } catch (Exception ex) { - this.initializer.onApplicationEvent(new SpringApplicationErrorEvent( - new SpringApplication(), context, new String[] {}, ex)); + this.initializer.onApplicationEvent(new ApplicationFailedEvent( + new SpringApplication(), new String[0], context, ex)); } assertThat(this.debugLog.size(), equalTo(0)); @@ -190,8 +190,8 @@ public class AutoConfigurationReportLoggingInitializerTests { @Test public void noErrorIfNotInitialized() throws Exception { - this.initializer.onApplicationEvent(new SpringApplicationErrorEvent( - new SpringApplication(), null, new String[0], new RuntimeException( + this.initializer.onApplicationEvent(new ApplicationFailedEvent( + new SpringApplication(), new String[0], null, new RuntimeException( "Planned"))); assertThat(this.infoLog.get(0), containsString("Unable to provide auto-configuration report")); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/SecurityAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/SecurityAutoConfigurationTests.java index c9631f5170..d96a874247 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/SecurityAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/SecurityAutoConfigurationTests.java @@ -18,7 +18,6 @@ package org.springframework.boot.autoconfigure.security; import org.junit.Test; import org.springframework.boot.SpringApplication; -import org.springframework.boot.SpringApplicationBeforeRefreshEvent; import org.springframework.boot.autoconfigure.AutoConfigurationReportLoggingInitializer; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage; @@ -26,6 +25,7 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.test.City; import org.springframework.boot.context.listener.LoggingApplicationListener; +import org.springframework.boot.event.ApplicationPreparedEvent; import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -119,8 +119,8 @@ public class SecurityAutoConfigurationTests { AnnotationConfigWebApplicationContext context) { EnvironmentTestUtils.addEnvironment(context, "debug:true"); LoggingApplicationListener logging = new LoggingApplicationListener(); - logging.onApplicationEvent(new SpringApplicationBeforeRefreshEvent( - new SpringApplication(), context, new String[0])); + logging.onApplicationEvent(new ApplicationPreparedEvent(new SpringApplication(), + new String[0], context)); AutoConfigurationReportLoggingInitializer initializer = new AutoConfigurationReportLoggingInitializer(); initializer.initialize(context); context.refresh(); 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 6433b546f6..9736f2c30b 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -36,6 +36,10 @@ 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.event.ApplicationEnvironmentPreparedEvent; +import org.springframework.boot.event.ApplicationFailedEvent; +import org.springframework.boot.event.ApplicationPreparedEvent; +import org.springframework.boot.event.ApplicationStartedEvent; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ApplicationEvent; @@ -313,8 +317,9 @@ public class SpringApplication { try { Set sources = getSources(); registerListeners(multicaster, sources); + // Allow logging and stuff to initialize very early - multicaster.multicastEvent(new SpringApplicationStartEvent(this, args)); + multicaster.multicastEvent(new ApplicationStartedEvent(this, args)); // Create and configure the environment ConfigurableEnvironment environment = getOrCreateEnvironment(); @@ -324,12 +329,13 @@ public class SpringApplication { } // Notify listeners of the environment creation - multicaster.multicastEvent(new SpringApplicationEnvironmentAvailableEvent( - this, environment, args)); + multicaster.multicastEvent(new ApplicationEnvironmentPreparedEvent(this, + args, environment)); // Sources might have changed when environment was applied sources = getSources(); Assert.notEmpty(sources, "Sources must not be empty"); + if (this.showBanner) { printBanner(); } @@ -349,9 +355,10 @@ public class SpringApplication { } load(context, sources.toArray(new Object[sources.size()])); + // Notify listeners of intention to refresh - multicaster.multicastEvent(new SpringApplicationBeforeRefreshEvent(this, - context, args)); + multicaster.multicastEvent(new ApplicationPreparedEvent(this, args, context)); + refresh(context); stopWatch.stop(); @@ -364,21 +371,21 @@ public class SpringApplication { return context; } catch (RuntimeException ex) { - handleError(context, multicaster, ex, args); + handleFailure(context, multicaster, ex, args); throw ex; } catch (Error ex) { - handleError(context, multicaster, ex, args); + handleFailure(context, multicaster, ex, args); throw ex; } } - protected void handleError(ConfigurableApplicationContext context, + protected void handleFailure(ConfigurableApplicationContext context, ApplicationEventMulticaster multicaster, Throwable exception, String... args) { try { - multicaster.multicastEvent(new SpringApplicationErrorEvent(this, context, - args, exception)); + multicaster.multicastEvent(new ApplicationFailedEvent(this, args, context, + exception)); } catch (Exception ex) { // We don't want to fail here and mask the original exception @@ -1024,7 +1031,7 @@ public class SpringApplication { ApplicationEvent event) { List> listeners = new ArrayList>(); listeners.addAll(super.getApplicationListeners(event)); - if (event instanceof SpringApplicationErrorEvent) { + if (event instanceof ApplicationFailedEvent) { Collections.reverse(listeners); } return listeners; diff --git a/spring-boot/src/main/java/org/springframework/boot/context/listener/ClasspathLoggingApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/context/listener/ClasspathLoggingApplicationListener.java index 446653e094..94841e9dab 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/listener/ClasspathLoggingApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/listener/ClasspathLoggingApplicationListener.java @@ -21,16 +21,16 @@ import java.util.Arrays; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.boot.SpringApplicationErrorEvent; -import org.springframework.boot.SpringApplicationStartEvent; +import org.springframework.boot.event.ApplicationFailedEvent; +import org.springframework.boot.event.ApplicationStartedEvent; import org.springframework.context.ApplicationEvent; import org.springframework.context.event.SmartApplicationListener; /** - * A {@link SmartApplicationListener} that reacts to {@link SpringApplicationStartEvent - * start events} by logging the classpath of the thread context class loader (TCCL) at - * {@code DEBUG} level and to {@link SpringApplicationErrorEvent error events} by logging - * the TCCL's classpath at {@code INFO} level. + * A {@link SmartApplicationListener} that reacts to {@link ApplicationStartedEvent start + * events} by logging the classpath of the thread context class loader (TCCL) at + * {@code DEBUG} level and to {@link ApplicationFailedEvent error events} by logging the + * TCCL's classpath at {@code INFO} level. * * @author Andy Wilkinson */ @@ -43,13 +43,13 @@ public final class ClasspathLoggingApplicationListener implements @Override public void onApplicationEvent(ApplicationEvent event) { - if (event instanceof SpringApplicationStartEvent) { + if (event instanceof ApplicationStartedEvent) { if (this.logger.isDebugEnabled()) { this.logger .debug("Application started with classpath: " + getClasspath()); } } - else if (event instanceof SpringApplicationErrorEvent) { + else if (event instanceof ApplicationFailedEvent) { if (this.logger.isInfoEnabled()) { this.logger.info("Application failed to start with classpath: " + getClasspath()); @@ -64,8 +64,8 @@ public final class ClasspathLoggingApplicationListener implements @Override public boolean supportsEventType(Class type) { - return SpringApplicationStartEvent.class.isAssignableFrom(type) - || SpringApplicationErrorEvent.class.isAssignableFrom(type); + return ApplicationStartedEvent.class.isAssignableFrom(type) + || ApplicationFailedEvent.class.isAssignableFrom(type); } @Override diff --git a/spring-boot/src/main/java/org/springframework/boot/context/listener/ConfigFileApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/context/listener/ConfigFileApplicationListener.java index 56546d1de9..e6943ac13e 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/listener/ConfigFileApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/listener/ConfigFileApplicationListener.java @@ -31,12 +31,12 @@ import java.util.Set; import org.springframework.beans.PropertyValues; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.boot.SpringApplication; -import org.springframework.boot.SpringApplicationEnvironmentAvailableEvent; import org.springframework.boot.bind.PropertySourcesPropertyValues; import org.springframework.boot.bind.RelaxedDataBinder; import org.springframework.boot.config.DefaultPropertySourceLoadersFactory; import org.springframework.boot.config.PropertySourceLoader; import org.springframework.boot.config.PropertySourceLoadersFactory; +import org.springframework.boot.event.ApplicationEnvironmentPreparedEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.AnnotatedBeanDefinitionReader; import org.springframework.context.annotation.PropertySources; @@ -81,7 +81,7 @@ import org.springframework.util.StringUtils; * @author Phillip Webb */ public class ConfigFileApplicationListener implements - ApplicationListener, Ordered { + ApplicationListener, Ordered { private static final String ACTIVE_PROFILES_PROPERTY = "spring.profiles.active"; @@ -110,7 +110,7 @@ public class ConfigFileApplicationListener implements * ("spring.main.show_banner=false"). */ @Override - public void onApplicationEvent(SpringApplicationEnvironmentAvailableEvent event) { + public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { Environment environment = event.getEnvironment(); if (environment instanceof ConfigurableEnvironment) { configure((ConfigurableEnvironment) environment, event.getSpringApplication()); diff --git a/spring-boot/src/main/java/org/springframework/boot/context/listener/EnvironmentDelegateApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/context/listener/EnvironmentDelegateApplicationListener.java index ecdb61a118..6119631b14 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/listener/EnvironmentDelegateApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/listener/EnvironmentDelegateApplicationListener.java @@ -20,7 +20,7 @@ import java.util.ArrayList; import java.util.List; import org.springframework.beans.BeanUtils; -import org.springframework.boot.SpringApplicationEnvironmentAvailableEvent; +import org.springframework.boot.event.ApplicationEnvironmentPreparedEvent; import org.springframework.context.ApplicationContextException; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; @@ -52,8 +52,8 @@ public class EnvironmentDelegateApplicationListener implements @Override public void onApplicationEvent(ApplicationEvent event) { - if (event instanceof SpringApplicationEnvironmentAvailableEvent) { - List> delegates = getListeners(((SpringApplicationEnvironmentAvailableEvent) event) + if (event instanceof ApplicationEnvironmentPreparedEvent) { + List> delegates = getListeners(((ApplicationEnvironmentPreparedEvent) event) .getEnvironment()); if (delegates.isEmpty()) { return; diff --git a/spring-boot/src/main/java/org/springframework/boot/context/listener/FileEncodingApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/context/listener/FileEncodingApplicationListener.java index 134783295b..62e3b9a759 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/listener/FileEncodingApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/listener/FileEncodingApplicationListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2014 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. @@ -18,8 +18,8 @@ package org.springframework.boot.context.listener; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.boot.SpringApplicationEnvironmentAvailableEvent; import org.springframework.boot.bind.RelaxedPropertyResolver; +import org.springframework.boot.event.ApplicationEnvironmentPreparedEvent; import org.springframework.context.ApplicationListener; /** @@ -42,12 +42,12 @@ import org.springframework.context.ApplicationListener; * @author Dave Syer */ public class FileEncodingApplicationListener implements - ApplicationListener { + ApplicationListener { private static Log logger = LogFactory.getLog(FileEncodingApplicationListener.class); @Override - public void onApplicationEvent(SpringApplicationEnvironmentAvailableEvent event) { + public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { RelaxedPropertyResolver resolver = new RelaxedPropertyResolver( event.getEnvironment(), "spring."); if (resolver.containsProperty("mandatoryFileEncoding")) { diff --git a/spring-boot/src/main/java/org/springframework/boot/context/listener/LoggingApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/context/listener/LoggingApplicationListener.java index 7f0c6fc52d..900d836c8a 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/listener/LoggingApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/listener/LoggingApplicationListener.java @@ -26,8 +26,8 @@ import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.boot.SpringApplication; -import org.springframework.boot.SpringApplicationEnvironmentAvailableEvent; -import org.springframework.boot.SpringApplicationStartEvent; +import org.springframework.boot.event.ApplicationEnvironmentPreparedEvent; +import org.springframework.boot.event.ApplicationStartedEvent; import org.springframework.boot.logging.LogLevel; import org.springframework.boot.logging.LoggingSystem; import org.springframework.context.ApplicationEvent; @@ -94,9 +94,8 @@ public class LoggingApplicationListener implements SmartApplicationListener { @SuppressWarnings("unchecked") private static Collection> EVENT_TYPES = Arrays - .> asList( - SpringApplicationStartEvent.class, - SpringApplicationEnvironmentAvailableEvent.class); + .> asList(ApplicationStartedEvent.class, + ApplicationEnvironmentPreparedEvent.class); private final Log logger = LogFactory.getLog(getClass()); @@ -123,8 +122,8 @@ public class LoggingApplicationListener implements SmartApplicationListener { @Override public void onApplicationEvent(ApplicationEvent event) { - if (event instanceof SpringApplicationEnvironmentAvailableEvent) { - SpringApplicationEnvironmentAvailableEvent available = (SpringApplicationEnvironmentAvailableEvent) event; + if (event instanceof ApplicationEnvironmentPreparedEvent) { + ApplicationEnvironmentPreparedEvent available = (ApplicationEnvironmentPreparedEvent) event; initialize(available.getEnvironment(), available.getSpringApplication() .getClassLoader()); } diff --git a/spring-boot/src/main/java/org/springframework/boot/context/listener/VcapApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/context/listener/VcapApplicationListener.java index 0f6832e973..5f03ce87b1 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/listener/VcapApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/listener/VcapApplicationListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2012 the original author or authors. + * Copyright 2010-2014 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. @@ -26,9 +26,9 @@ import java.util.Properties; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.boot.SpringApplicationEnvironmentAvailableEvent; import org.springframework.boot.config.JsonParser; import org.springframework.boot.config.JsonParserFactory; +import org.springframework.boot.event.ApplicationEnvironmentPreparedEvent; import org.springframework.context.ApplicationListener; import org.springframework.core.Ordered; import org.springframework.core.env.CommandLinePropertySource; @@ -88,7 +88,7 @@ import org.springframework.util.StringUtils; * @author Dave Syer */ public class VcapApplicationListener implements - ApplicationListener, Ordered { + ApplicationListener, Ordered { private static final Log logger = LogFactory.getLog(VcapApplicationListener.class); @@ -112,7 +112,7 @@ public class VcapApplicationListener implements } @Override - public void onApplicationEvent(SpringApplicationEnvironmentAvailableEvent event) { + public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { ConfigurableEnvironment environment = event.getEnvironment(); if (!environment.containsProperty(VCAP_APPLICATION) diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplicationEnvironmentAvailableEvent.java b/spring-boot/src/main/java/org/springframework/boot/event/ApplicationEnvironmentPreparedEvent.java similarity index 62% rename from spring-boot/src/main/java/org/springframework/boot/SpringApplicationEnvironmentAvailableEvent.java rename to spring-boot/src/main/java/org/springframework/boot/event/ApplicationEnvironmentPreparedEvent.java index f9e904becb..b8bddaa9c5 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplicationEnvironmentAvailableEvent.java +++ b/spring-boot/src/main/java/org/springframework/boot/event/ApplicationEnvironmentPreparedEvent.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2014 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. @@ -14,9 +14,9 @@ * limitations under the License. */ -package org.springframework.boot; +package org.springframework.boot.event; -import org.springframework.context.ApplicationEvent; +import org.springframework.boot.SpringApplication; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; @@ -26,36 +26,19 @@ import org.springframework.core.env.Environment; * * @author Dave Syer */ -public class SpringApplicationEnvironmentAvailableEvent extends ApplicationEvent { +public class ApplicationEnvironmentPreparedEvent extends SpringApplicationEvent { private final ConfigurableEnvironment environment; - private final String[] args; /** - * @param springApplication the current application - * @param environment the environment that was just created + * @param application the current application * @param args the argumemts the application is running with + * @param environment the environment that was just created */ - public SpringApplicationEnvironmentAvailableEvent( - SpringApplication springApplication, ConfigurableEnvironment environment, - String[] args) { - super(springApplication); + public ApplicationEnvironmentPreparedEvent(SpringApplication application, + String[] args, ConfigurableEnvironment environment) { + super(application, args); this.environment = environment; - this.args = args; - } - - /** - * @return the springApplication - */ - public SpringApplication getSpringApplication() { - return (SpringApplication) getSource(); - } - - /** - * @return the args - */ - public String[] getArgs() { - return this.args; } /** diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplicationErrorEvent.java b/spring-boot/src/main/java/org/springframework/boot/event/ApplicationFailedEvent.java similarity index 64% rename from spring-boot/src/main/java/org/springframework/boot/SpringApplicationErrorEvent.java rename to spring-boot/src/main/java/org/springframework/boot/event/ApplicationFailedEvent.java index d7ead8bd5e..f4d7bab64b 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplicationErrorEvent.java +++ b/spring-boot/src/main/java/org/springframework/boot/event/ApplicationFailedEvent.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2014 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. @@ -14,9 +14,9 @@ * limitations under the License. */ -package org.springframework.boot; +package org.springframework.boot.event; -import org.springframework.context.ApplicationEvent; +import org.springframework.boot.SpringApplication; import org.springframework.context.ConfigurableApplicationContext; /** @@ -24,23 +24,22 @@ import org.springframework.context.ConfigurableApplicationContext; * * @author Dave Syer */ -public class SpringApplicationErrorEvent extends ApplicationEvent { +public class ApplicationFailedEvent extends SpringApplicationEvent { - private final String[] args; - private final Throwable exception; private final ConfigurableApplicationContext context; + private final Throwable exception; + /** - * @param springApplication the current application + * @param application the current application * @param context the context that was being created (maybe null) * @param args the arguments the application was running with * @param exception the exception that caused the error */ - public SpringApplicationErrorEvent(SpringApplication springApplication, - ConfigurableApplicationContext context, String[] args, Throwable exception) { - super(springApplication); + public ApplicationFailedEvent(SpringApplication application, String[] args, + ConfigurableApplicationContext context, Throwable exception) { + super(application, args); this.context = context; - this.args = args; this.exception = exception; } @@ -51,13 +50,6 @@ public class SpringApplicationErrorEvent extends ApplicationEvent { return this.context; } - /** - * @return the springApplication - */ - public SpringApplication getSpringApplication() { - return (SpringApplication) getSource(); - } - /** * @return the exception */ @@ -65,11 +57,4 @@ public class SpringApplicationErrorEvent extends ApplicationEvent { return this.exception; } - /** - * @return the args - */ - public String[] getArgs() { - return this.args; - } - } diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplicationBeforeRefreshEvent.java b/spring-boot/src/main/java/org/springframework/boot/event/ApplicationPreparedEvent.java similarity index 58% rename from spring-boot/src/main/java/org/springframework/boot/SpringApplicationBeforeRefreshEvent.java rename to spring-boot/src/main/java/org/springframework/boot/event/ApplicationPreparedEvent.java index 25e14e4acd..934ec66263 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplicationBeforeRefreshEvent.java +++ b/spring-boot/src/main/java/org/springframework/boot/event/ApplicationPreparedEvent.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2014 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. @@ -14,49 +14,33 @@ * limitations under the License. */ -package org.springframework.boot; +package org.springframework.boot.event; +import org.springframework.boot.SpringApplication; import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationEvent; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.Environment; /** * Event published as when a {@link SpringApplication} is starting up and the - * {@link ApplicationContext} is about to refresh. The bean definitions will be loaded and - * the {@link Environment} is ready for use at this stage. + * {@link ApplicationContext} is fully prepared but not refreshed. The bean definitions + * will be loaded and the {@link Environment} is ready for use at this stage. * * @author Dave Syer */ -public class SpringApplicationBeforeRefreshEvent extends ApplicationEvent { +public class ApplicationPreparedEvent extends SpringApplicationEvent { - private final String[] args; private final ConfigurableApplicationContext context; /** - * @param springApplication the current application - * @param context the ApplicationContext about to be refreshed + * @param application the current application * @param args the argumemts the application is running with + * @param context the ApplicationContext about to be refreshed */ - public SpringApplicationBeforeRefreshEvent(SpringApplication springApplication, - ConfigurableApplicationContext context, String[] args) { - super(springApplication); + public ApplicationPreparedEvent(SpringApplication application, String[] args, + ConfigurableApplicationContext context) { + super(application, args); this.context = context; - this.args = args; - } - - /** - * @return the springApplication - */ - public SpringApplication getSpringApplication() { - return (SpringApplication) getSource(); - } - - /** - * @return the args - */ - public String[] getArgs() { - return this.args; } /** diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplicationStartEvent.java b/spring-boot/src/main/java/org/springframework/boot/event/ApplicationStartedEvent.java similarity index 66% rename from spring-boot/src/main/java/org/springframework/boot/SpringApplicationStartEvent.java rename to spring-boot/src/main/java/org/springframework/boot/event/ApplicationStartedEvent.java index 9481e9b38b..d226907004 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplicationStartEvent.java +++ b/spring-boot/src/main/java/org/springframework/boot/event/ApplicationStartedEvent.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2014 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. @@ -14,10 +14,10 @@ * limitations under the License. */ -package org.springframework.boot; +package org.springframework.boot.event; +import org.springframework.boot.SpringApplication; import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.core.env.Environment; @@ -30,31 +30,14 @@ import org.springframework.core.env.Environment; * * @author Dave Syer */ -public class SpringApplicationStartEvent extends ApplicationEvent { - - private final String[] args; +public class ApplicationStartedEvent extends SpringApplicationEvent { /** - * @param springApplication the current application + * @param application the current application * @param args the argumemts the application is running with */ - public SpringApplicationStartEvent(SpringApplication springApplication, String[] args) { - super(springApplication); - this.args = args; - } - - /** - * @return the springApplication - */ - public SpringApplication getSpringApplication() { - return (SpringApplication) getSource(); - } - - /** - * @return the args - */ - public String[] getArgs() { - return this.args; + public ApplicationStartedEvent(SpringApplication application, String[] args) { + super(application, args); } } diff --git a/spring-boot/src/main/java/org/springframework/boot/event/SpringApplicationEvent.java b/spring-boot/src/main/java/org/springframework/boot/event/SpringApplicationEvent.java new file mode 100644 index 0000000000..cc3d96f9b2 --- /dev/null +++ b/spring-boot/src/main/java/org/springframework/boot/event/SpringApplicationEvent.java @@ -0,0 +1,44 @@ +/* + * Copyright 2012-2014 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.event; + +import org.springframework.boot.SpringApplication; +import org.springframework.context.ApplicationEvent; + +/** + * Base class for {@link ApplicationEvent} related to a {@link SpringApplication}. + * + * @author Phillip Webb + */ +public abstract class SpringApplicationEvent extends ApplicationEvent { + + private final String[] args; + + public SpringApplicationEvent(SpringApplication application, String[] args) { + super(application); + this.args = args; + } + + public SpringApplication getSpringApplication() { + return (SpringApplication) getSource(); + } + + public final String[] getArgs() { + return this.args; + } + +} diff --git a/spring-boot/src/main/java/org/springframework/boot/liquibase/LiquibaseServiceLocatorInitializer.java b/spring-boot/src/main/java/org/springframework/boot/liquibase/LiquibaseServiceLocatorInitializer.java index d719de24fd..c2d77538ce 100644 --- a/spring-boot/src/main/java/org/springframework/boot/liquibase/LiquibaseServiceLocatorInitializer.java +++ b/spring-boot/src/main/java/org/springframework/boot/liquibase/LiquibaseServiceLocatorInitializer.java @@ -5,7 +5,7 @@ import liquibase.servicelocator.ServiceLocator; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.boot.SpringApplicationStartEvent; +import org.springframework.boot.event.ApplicationStartedEvent; import org.springframework.context.ApplicationListener; import org.springframework.util.ClassUtils; @@ -17,12 +17,12 @@ import org.springframework.util.ClassUtils; * @author Dave Syer */ public class LiquibaseServiceLocatorInitializer implements - ApplicationListener { + ApplicationListener { static final Log logger = LogFactory.getLog(LiquibaseServiceLocatorInitializer.class); @Override - public void onApplicationEvent(SpringApplicationStartEvent event) { + public void onApplicationEvent(ApplicationStartedEvent event) { if (ClassUtils.isPresent("liquibase.servicelocator.ServiceLocator", null)) { new LiquibasePresent().replaceServiceLocator(); } diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/ServletListenerRegistrationBeanTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/ServletListenerRegistrationBeanTests.java index 181db38994..e87c4fd784 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/ServletListenerRegistrationBeanTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/ServletListenerRegistrationBeanTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2014 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. @@ -41,7 +41,8 @@ public class ServletListenerRegistrationBeanTests { @Rule public ExpectedException thrown = ExpectedException.none(); - private final ServletContextListener listener = Mockito.mock(ServletContextListener.class); + private final ServletContextListener listener = Mockito + .mock(ServletContextListener.class); @Mock private ServletContext servletContext; diff --git a/spring-boot/src/test/java/org/springframework/boot/context/listener/ConfigFileApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/listener/ConfigFileApplicationListenerTests.java index 0cbf802938..76f4cddc42 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/listener/ConfigFileApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/listener/ConfigFileApplicationListenerTests.java @@ -24,9 +24,9 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.springframework.boot.SpringApplication; -import org.springframework.boot.SpringApplicationEnvironmentAvailableEvent; import org.springframework.boot.config.PropertySourceLoader; import org.springframework.boot.config.PropertySourceLoadersFactory; +import org.springframework.boot.event.ApplicationEnvironmentPreparedEvent; import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Configuration; @@ -56,8 +56,8 @@ public class ConfigFileApplicationListenerTests { private final StandardEnvironment environment = new StandardEnvironment(); - private final SpringApplicationEnvironmentAvailableEvent event = new SpringApplicationEnvironmentAvailableEvent( - new SpringApplication(), this.environment, new String[0]); + private final ApplicationEnvironmentPreparedEvent event = new ApplicationEnvironmentPreparedEvent( + new SpringApplication(), new String[0], this.environment); private final ConfigFileApplicationListener initializer = new ConfigFileApplicationListener(); diff --git a/spring-boot/src/test/java/org/springframework/boot/context/listener/EnvironmentDelegateApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/listener/EnvironmentDelegateApplicationListenerTests.java index 89f84aa2cb..a7b7ccfdb0 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/listener/EnvironmentDelegateApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/listener/EnvironmentDelegateApplicationListenerTests.java @@ -21,7 +21,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.springframework.boot.SpringApplication; -import org.springframework.boot.SpringApplicationEnvironmentAvailableEvent; +import org.springframework.boot.event.ApplicationEnvironmentPreparedEvent; import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.context.ApplicationListener; import org.springframework.context.ConfigurableApplicationContext; @@ -53,8 +53,8 @@ public class EnvironmentDelegateApplicationListenerTests { public void orderedInitialize() throws Exception { EnvironmentTestUtils.addEnvironment(this.context, "context.listener.classes:" + MockInitB.class.getName() + "," + MockInitA.class.getName()); - this.listener.onApplicationEvent(new SpringApplicationEnvironmentAvailableEvent( - new SpringApplication(), this.context.getEnvironment(), new String[0])); + this.listener.onApplicationEvent(new ApplicationEnvironmentPreparedEvent( + new SpringApplication(), new String[0], this.context.getEnvironment())); this.context.getBeanFactory().registerSingleton("testListener", this.listener); this.context.refresh(); assertThat(this.context.getBeanFactory().getSingleton("a"), equalTo((Object) "a")); @@ -63,15 +63,15 @@ public class EnvironmentDelegateApplicationListenerTests { @Test public void noInitializers() throws Exception { - this.listener.onApplicationEvent(new SpringApplicationEnvironmentAvailableEvent( - new SpringApplication(), this.context.getEnvironment(), new String[0])); + this.listener.onApplicationEvent(new ApplicationEnvironmentPreparedEvent( + new SpringApplication(), new String[0], this.context.getEnvironment())); } @Test public void emptyInitializers() throws Exception { EnvironmentTestUtils.addEnvironment(this.context, "context.listener.classes:"); - this.listener.onApplicationEvent(new SpringApplicationEnvironmentAvailableEvent( - new SpringApplication(), this.context.getEnvironment(), new String[0])); + this.listener.onApplicationEvent(new ApplicationEnvironmentPreparedEvent( + new SpringApplication(), new String[0], this.context.getEnvironment())); } @Order(Ordered.HIGHEST_PRECEDENCE) diff --git a/spring-boot/src/test/java/org/springframework/boot/context/listener/FileEncodingApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/listener/FileEncodingApplicationListenerTests.java index fb19d95c8d..04ee16834f 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/listener/FileEncodingApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/listener/FileEncodingApplicationListenerTests.java @@ -19,7 +19,7 @@ package org.springframework.boot.context.listener; import org.junit.Assume; import org.junit.Test; import org.springframework.boot.SpringApplication; -import org.springframework.boot.SpringApplicationEnvironmentAvailableEvent; +import org.springframework.boot.event.ApplicationEnvironmentPreparedEvent; import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.StandardEnvironment; @@ -33,8 +33,8 @@ public class FileEncodingApplicationListenerTests { private final FileEncodingApplicationListener initializer = new FileEncodingApplicationListener(); private final ConfigurableEnvironment environment = new StandardEnvironment(); - private final SpringApplicationEnvironmentAvailableEvent event = new SpringApplicationEnvironmentAvailableEvent( - new SpringApplication(), this.environment, new String[0]); + private final ApplicationEnvironmentPreparedEvent event = new ApplicationEnvironmentPreparedEvent( + new SpringApplication(), new String[0], this.environment); @Test(expected = IllegalStateException.class) public void testIllegalState() { diff --git a/spring-boot/src/test/java/org/springframework/boot/context/listener/LoggingApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/listener/LoggingApplicationListenerTests.java index 503ba8c377..d4f54ce0cf 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/listener/LoggingApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/listener/LoggingApplicationListenerTests.java @@ -29,7 +29,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.springframework.boot.SpringApplication; -import org.springframework.boot.SpringApplicationStartEvent; +import org.springframework.boot.event.ApplicationStartedEvent; import org.springframework.boot.logging.LogLevel; import org.springframework.boot.logging.java.JavaLoggingSystem; import org.springframework.boot.test.EnvironmentTestUtils; @@ -70,7 +70,7 @@ public class LoggingApplicationListenerTests { public void init() throws SecurityException, IOException { LogManager.getLogManager().readConfiguration( JavaLoggingSystem.class.getResourceAsStream("logging.properties")); - this.initializer.onApplicationEvent(new SpringApplicationStartEvent( + this.initializer.onApplicationEvent(new ApplicationStartedEvent( new SpringApplication(), NO_ARGS)); new File("target/foo.log").delete(); } @@ -190,7 +190,7 @@ public class LoggingApplicationListenerTests { public void parseArgsDoesntReplace() throws Exception { this.initializer.setSpringBootLogging(LogLevel.ERROR); this.initializer.setParseArgs(false); - this.initializer.onApplicationEvent(new SpringApplicationStartEvent( + this.initializer.onApplicationEvent(new ApplicationStartedEvent( this.springApplication, new String[] { "--debug" })); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); diff --git a/spring-boot/src/test/java/org/springframework/boot/context/listener/VcapApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/listener/VcapApplicationListenerTests.java index 2e9498df3a..7799af255a 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/listener/VcapApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/listener/VcapApplicationListenerTests.java @@ -18,7 +18,7 @@ package org.springframework.boot.context.listener; import org.junit.Test; import org.springframework.boot.SpringApplication; -import org.springframework.boot.SpringApplicationEnvironmentAvailableEvent; +import org.springframework.boot.event.ApplicationEnvironmentPreparedEvent; import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -34,8 +34,8 @@ public class VcapApplicationListenerTests { private final VcapApplicationListener initializer = new VcapApplicationListener(); private final ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(); - private final SpringApplicationEnvironmentAvailableEvent event = new SpringApplicationEnvironmentAvailableEvent( - new SpringApplication(), this.context.getEnvironment(), new String[0]); + private final ApplicationEnvironmentPreparedEvent event = new ApplicationEnvironmentPreparedEvent( + new SpringApplication(), new String[0], this.context.getEnvironment()); @Test public void testApplicationProperties() { diff --git a/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java b/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java index 8430c1001d..6293ccd365 100644 --- a/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java @@ -40,8 +40,8 @@ public class LogbackLoggingSystemTests { @Rule public OutputCapture output = new OutputCapture(); - private final LogbackLoggingSystem loggingSystem = new LogbackLoggingSystem(getClass() - .getClassLoader()); + private final LogbackLoggingSystem loggingSystem = new LogbackLoggingSystem( + getClass().getClassLoader()); private Log logger;