diff --git a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/RestartApplicationListener.java b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/RestartApplicationListener.java index 2ad0723fc5c..8cd7d81229a 100644 --- a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/RestartApplicationListener.java +++ b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/RestartApplicationListener.java @@ -19,7 +19,7 @@ package org.springframework.boot.devtools.restart; import org.springframework.boot.context.event.ApplicationFailedEvent; import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.boot.context.event.ApplicationReadyEvent; -import org.springframework.boot.context.event.ApplicationStartedEvent; +import org.springframework.boot.context.event.ApplicationStartingEvent; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.core.Ordered; @@ -41,8 +41,8 @@ public class RestartApplicationListener @Override public void onApplicationEvent(ApplicationEvent event) { - if (event instanceof ApplicationStartedEvent) { - onApplicationStartedEvent((ApplicationStartedEvent) event); + if (event instanceof ApplicationStartingEvent) { + onApplicationStartingEvent((ApplicationStartingEvent) event); } if (event instanceof ApplicationPreparedEvent) { Restarter.getInstance() @@ -57,7 +57,7 @@ public class RestartApplicationListener } } - private void onApplicationStartedEvent(ApplicationStartedEvent event) { + private void onApplicationStartingEvent(ApplicationStartingEvent event) { // It's too early to use the Spring environment but we should still allow // users to disable restart using a System property. String enabled = System.getProperty(ENABLED_PROPERTY); diff --git a/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestartApplicationListenerTests.java b/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestartApplicationListenerTests.java index e87b713e405..31acad0f0e2 100644 --- a/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestartApplicationListenerTests.java +++ b/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestartApplicationListenerTests.java @@ -24,7 +24,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.context.event.ApplicationFailedEvent; import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.boot.context.event.ApplicationReadyEvent; -import org.springframework.boot.context.event.ApplicationStartedEvent; +import org.springframework.boot.context.event.ApplicationStartingEvent; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.Ordered; import org.springframework.test.util.ReflectionTestUtils; @@ -92,7 +92,7 @@ public class RestartApplicationListenerTests { SpringApplication application = new SpringApplication(); ConfigurableApplicationContext context = mock( ConfigurableApplicationContext.class); - listener.onApplicationEvent(new ApplicationStartedEvent(application, ARGS)); + listener.onApplicationEvent(new ApplicationStartingEvent(application, ARGS)); assertThat(Restarter.getInstance()).isNotEqualTo(nullValue()); assertThat(Restarter.getInstance().isFinished()).isFalse(); listener.onApplicationEvent( 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 8740f78b7b1..b66b83f8149 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -299,7 +299,7 @@ public class SpringApplication { FailureAnalyzers analyzers = null; configureHeadlessProperty(); SpringApplicationRunListeners listeners = getRunListeners(args); - listeners.started(); + listeners.starting(); try { ApplicationArguments applicationArguments = new DefaultApplicationArguments( args); diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplicationRunListener.java b/spring-boot/src/main/java/org/springframework/boot/SpringApplicationRunListener.java index 6369d2e9b3b..ecaabfe392f 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplicationRunListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplicationRunListener.java @@ -37,7 +37,7 @@ public interface SpringApplicationRunListener { * Called immediately when the run method has first started. Can be used for very * early initialization. */ - void started(); + void starting(); /** * Called once the environment has been prepared, but before the diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplicationRunListeners.java b/spring-boot/src/main/java/org/springframework/boot/SpringApplicationRunListeners.java index ec651f79b2c..c177807551d 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplicationRunListeners.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplicationRunListeners.java @@ -43,9 +43,9 @@ class SpringApplicationRunListeners { this.listeners = new ArrayList(listeners); } - public void started() { + public void starting() { for (SpringApplicationRunListener listener : this.listeners) { - listener.started(); + listener.starting(); } } diff --git a/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationStartedEvent.java b/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationStartedEvent.java index 9973a4cc3c0..f075302c2a3 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationStartedEvent.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationStartedEvent.java @@ -29,9 +29,11 @@ import org.springframework.core.env.Environment; * state too much at this early stage since it might be modified later in the lifecycle. * * @author Dave Syer + * @deprecated since 1.5.0 in favor of {@link ApplicationStartingEvent} */ +@Deprecated @SuppressWarnings("serial") -public class ApplicationStartedEvent extends SpringApplicationEvent { +public class ApplicationStartedEvent extends ApplicationStartingEvent { /** * Create a new {@link ApplicationStartedEvent} instance. diff --git a/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationStartingEvent.java b/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationStartingEvent.java new file mode 100644 index 00000000000..d645fa6f3b5 --- /dev/null +++ b/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationStartingEvent.java @@ -0,0 +1,47 @@ +/* + * Copyright 2012-2016 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.context.event; + +import org.springframework.boot.SpringApplication; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationListener; +import org.springframework.core.env.Environment; + +/** + * Event published as early as conceivably possible as soon as a {@link SpringApplication} + * has been started - before the {@link Environment} or {@link ApplicationContext} is + * available, but after the {@link ApplicationListener}s have been registered. The source + * of the event is the {@link SpringApplication} itself, but beware of using its internal + * state too much at this early stage since it might be modified later in the lifecycle. + * + * @author Phillip Webb + * @author Madhura Bhave + * @since 1.5.0 + */ +@SuppressWarnings("serial") +public class ApplicationStartingEvent extends SpringApplicationEvent { + + /** + * Create a new {@link ApplicationStartingEvent} instance. + * @param application the current application + * @param args the arguments the application is running with + */ + public ApplicationStartingEvent(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 de26352fb6c..4e2f3d9e4b6 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 @@ -58,7 +58,8 @@ public class EventPublishingRunListener implements SpringApplicationRunListener, } @Override - public void started() { + @SuppressWarnings("deprecation") + public void starting() { this.initialMulticaster .multicastEvent(new ApplicationStartedEvent(this.application, this.args)); } diff --git a/spring-boot/src/main/java/org/springframework/boot/liquibase/LiquibaseServiceLocatorApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/liquibase/LiquibaseServiceLocatorApplicationListener.java index 6a32be7677d..8cfc967d3ed 100644 --- a/spring-boot/src/main/java/org/springframework/boot/liquibase/LiquibaseServiceLocatorApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/liquibase/LiquibaseServiceLocatorApplicationListener.java @@ -21,7 +21,7 @@ import liquibase.servicelocator.ServiceLocator; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.boot.context.event.ApplicationStartedEvent; +import org.springframework.boot.context.event.ApplicationStartingEvent; import org.springframework.context.ApplicationListener; import org.springframework.util.ClassUtils; @@ -33,13 +33,13 @@ import org.springframework.util.ClassUtils; * @author Dave Syer */ public class LiquibaseServiceLocatorApplicationListener - implements ApplicationListener { + implements ApplicationListener { private static final Log logger = LogFactory .getLog(LiquibaseServiceLocatorApplicationListener.class); @Override - public void onApplicationEvent(ApplicationStartedEvent event) { + public void onApplicationEvent(ApplicationStartingEvent event) { if (ClassUtils.isPresent("liquibase.servicelocator.ServiceLocator", null)) { new LiquibasePresent().replaceServiceLocator(); } diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java index 01ac287e04f..e3650be579e 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java @@ -30,7 +30,7 @@ import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; import org.springframework.boot.context.event.ApplicationFailedEvent; import org.springframework.boot.context.event.ApplicationPreparedEvent; -import org.springframework.boot.context.event.ApplicationStartedEvent; +import org.springframework.boot.context.event.ApplicationStartingEvent; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; @@ -161,7 +161,7 @@ public class LoggingApplicationListener implements GenericApplicationListener { LOG_LEVEL_LOGGERS.add(LogLevel.DEBUG, "org.hibernate.SQL"); } - private static Class[] EVENT_TYPES = { ApplicationStartedEvent.class, + private static Class[] EVENT_TYPES = { ApplicationStartingEvent.class, ApplicationEnvironmentPreparedEvent.class, ApplicationPreparedEvent.class, ContextClosedEvent.class }; @@ -201,8 +201,8 @@ public class LoggingApplicationListener implements GenericApplicationListener { @Override public void onApplicationEvent(ApplicationEvent event) { - if (event instanceof ApplicationStartedEvent) { - onApplicationStartedEvent((ApplicationStartedEvent) event); + if (event instanceof ApplicationStartingEvent) { + onApplicationStartingEvent((ApplicationStartingEvent) event); } else if (event instanceof ApplicationEnvironmentPreparedEvent) { onApplicationEnvironmentPreparedEvent( @@ -220,7 +220,7 @@ public class LoggingApplicationListener implements GenericApplicationListener { } } - private void onApplicationStartedEvent(ApplicationStartedEvent event) { + private void onApplicationStartingEvent(ApplicationStartingEvent event) { this.loggingSystem = LoggingSystem .get(event.getSpringApplication().getClassLoader()); this.loggingSystem.beforeInitialize(); 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 d24a1afc937..d21326b900e 100644 --- a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -44,7 +44,7 @@ import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletCon import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.boot.context.event.ApplicationReadyEvent; -import org.springframework.boot.context.event.ApplicationStartedEvent; +import org.springframework.boot.context.event.ApplicationStartingEvent; import org.springframework.boot.testutil.InternalOutputCapture; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; @@ -302,6 +302,7 @@ public class SpringApplicationTests { } @Test + @SuppressWarnings("deprecation") public void eventsOrder() { SpringApplication application = new SpringApplication(ExampleConfig.class); application.setWebEnvironment(false); @@ -316,7 +317,9 @@ public class SpringApplicationTests { application.addListeners(new ApplicationRunningEventListener()); this.context = application.run(); assertThat(events).hasSize(5); - assertThat(events.get(0)).isInstanceOf(ApplicationStartedEvent.class); + assertThat(events.get(0)).isInstanceOf( + org.springframework.boot.context.event.ApplicationStartedEvent.class); + assertThat(events.get(0)).isInstanceOf(ApplicationStartingEvent.class); assertThat(events.get(1)).isInstanceOf(ApplicationEnvironmentPreparedEvent.class); assertThat(events.get(2)).isInstanceOf(ApplicationPreparedEvent.class); assertThat(events.get(3)).isInstanceOf(ContextRefreshedEvent.class); diff --git a/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerIntegrationTests.java b/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerIntegrationTests.java index 19bc4ba6e6c..7fd4b9dcb2c 100644 --- a/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerIntegrationTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerIntegrationTests.java @@ -22,7 +22,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.boot.context.event.ApplicationStartedEvent; +import org.springframework.boot.context.event.ApplicationStartingEvent; import org.springframework.boot.testutil.InternalOutputCapture; import org.springframework.context.ApplicationListener; import org.springframework.context.ConfigurableApplicationContext; @@ -56,16 +56,18 @@ public class LoggingApplicationListenerIntegrationTests { @Test public void loggingPerformedDuringChildApplicationStartIsNotLost() { new SpringApplicationBuilder(Config.class).web(false).child(Config.class) - .web(false).listeners(new ApplicationListener() { + .web(false) + .listeners(new ApplicationListener() { private final Logger logger = LoggerFactory.getLogger(getClass()); @Override - public void onApplicationEvent(ApplicationStartedEvent event) { - this.logger.info("Child application started"); + public void onApplicationEvent(ApplicationStartingEvent event) { + this.logger.info("Child application starting"); } + }).run(); - assertThat(this.outputCapture.toString()).contains("Child application started"); + assertThat(this.outputCapture.toString()).contains("Child application starting"); } @Component diff --git a/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java index cbc2647c682..4ef3fb53053 100644 --- a/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java @@ -39,7 +39,7 @@ import org.slf4j.bridge.SLF4JBridgeHandler; import org.springframework.boot.ApplicationPid; import org.springframework.boot.SpringApplication; import org.springframework.boot.context.event.ApplicationFailedEvent; -import org.springframework.boot.context.event.ApplicationStartedEvent; +import org.springframework.boot.context.event.ApplicationStartingEvent; import org.springframework.boot.logging.java.JavaLoggingSystem; import org.springframework.boot.testutil.InternalOutputCapture; import org.springframework.context.event.ContextClosedEvent; @@ -86,7 +86,7 @@ public class LoggingApplicationListenerTests { LogManager.getLogManager().readConfiguration( JavaLoggingSystem.class.getResourceAsStream("logging.properties")); this.initializer.onApplicationEvent( - new ApplicationStartedEvent(new SpringApplication(), NO_ARGS)); + new ApplicationStartingEvent(new SpringApplication(), NO_ARGS)); new File("target/foo.log").delete(); new File(tmpDir() + "/spring.log").delete(); } @@ -342,7 +342,7 @@ public class LoggingApplicationListenerTests { public void parseArgsDoesntReplace() throws Exception { this.initializer.setSpringBootLogging(LogLevel.ERROR); this.initializer.setParseArgs(false); - this.initializer.onApplicationEvent(new ApplicationStartedEvent( + this.initializer.onApplicationEvent(new ApplicationStartingEvent( this.springApplication, new String[] { "--debug" })); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); @@ -387,7 +387,7 @@ public class LoggingApplicationListenerTests { System.setProperty(LoggingSystem.class.getName(), TestShutdownHandlerLoggingSystem.class.getName()); listener.onApplicationEvent( - new ApplicationStartedEvent(new SpringApplication(), NO_ARGS)); + new ApplicationStartingEvent(new SpringApplication(), NO_ARGS)); listener.initialize(this.context.getEnvironment(), this.context.getClassLoader()); assertThat(listener.shutdownHook).isNull(); } @@ -400,7 +400,7 @@ public class LoggingApplicationListenerTests { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "logging.register_shutdown_hook=true"); listener.onApplicationEvent( - new ApplicationStartedEvent(new SpringApplication(), NO_ARGS)); + new ApplicationStartingEvent(new SpringApplication(), NO_ARGS)); listener.initialize(this.context.getEnvironment(), this.context.getClassLoader()); assertThat(listener.shutdownHook).isNotNull(); listener.shutdownHook.start(); @@ -413,7 +413,7 @@ public class LoggingApplicationListenerTests { System.setProperty(LoggingSystem.SYSTEM_PROPERTY, TestCleanupLoggingSystem.class.getName()); this.initializer.onApplicationEvent( - new ApplicationStartedEvent(this.springApplication, new String[0])); + new ApplicationStartingEvent(this.springApplication, new String[0])); TestCleanupLoggingSystem loggingSystem = (TestCleanupLoggingSystem) ReflectionTestUtils .getField(this.initializer, "loggingSystem"); assertThat(loggingSystem.cleanedUp).isFalse(); @@ -426,7 +426,7 @@ public class LoggingApplicationListenerTests { System.setProperty(LoggingSystem.SYSTEM_PROPERTY, TestCleanupLoggingSystem.class.getName()); this.initializer.onApplicationEvent( - new ApplicationStartedEvent(this.springApplication, new String[0])); + new ApplicationStartingEvent(this.springApplication, new String[0])); TestCleanupLoggingSystem loggingSystem = (TestCleanupLoggingSystem) ReflectionTestUtils .getField(this.initializer, "loggingSystem"); assertThat(loggingSystem.cleanedUp).isFalse(); @@ -472,7 +472,7 @@ public class LoggingApplicationListenerTests { System.setProperty(LoggingSystem.SYSTEM_PROPERTY, TestCleanupLoggingSystem.class.getName()); this.initializer.onApplicationEvent( - new ApplicationStartedEvent(this.springApplication, new String[0])); + new ApplicationStartingEvent(this.springApplication, new String[0])); TestCleanupLoggingSystem loggingSystem = (TestCleanupLoggingSystem) ReflectionTestUtils .getField(this.initializer, "loggingSystem"); assertThat(loggingSystem.cleanedUp).isFalse(); diff --git a/spring-boot/src/test/java/org/springframework/boot/system/ApplicationPidFileWriterTests.java b/spring-boot/src/test/java/org/springframework/boot/system/ApplicationPidFileWriterTests.java index 5b6ada3da39..698ec42a43f 100644 --- a/spring-boot/src/test/java/org/springframework/boot/system/ApplicationPidFileWriterTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/system/ApplicationPidFileWriterTests.java @@ -30,7 +30,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.boot.context.event.ApplicationReadyEvent; -import org.springframework.boot.context.event.ApplicationStartedEvent; +import org.springframework.boot.context.event.ApplicationStartingEvent; import org.springframework.boot.context.event.SpringApplicationEvent; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.ConfigurableEnvironment; @@ -129,9 +129,9 @@ public class ApplicationPidFileWriterTests { public void withNoEnvironment() throws Exception { File file = this.temporaryFolder.newFile(); ApplicationPidFileWriter listener = new ApplicationPidFileWriter(file); - listener.setTriggerEventType(ApplicationStartedEvent.class); + listener.setTriggerEventType(ApplicationStartingEvent.class); listener.onApplicationEvent( - new ApplicationStartedEvent(new SpringApplication(), new String[] {})); + new ApplicationStartingEvent(new SpringApplication(), new String[] {})); assertThat(FileCopyUtils.copyToString(new FileReader(file))).isNotEmpty(); }