Rename SpringApplication Events

Move SpringApplication events to their own package, create a common
base class and rename classes to match Spring conventions.
pull/302/head
Phillip Webb 11 years ago
parent 818326d820
commit 92f01cf9bc

@ -20,9 +20,9 @@ import java.util.Map;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.boot.SpringApplicationErrorEvent;
import org.springframework.boot.autoconfigure.AutoConfigurationReport.ConditionAndOutcome; import org.springframework.boot.autoconfigure.AutoConfigurationReport.ConditionAndOutcome;
import org.springframework.boot.autoconfigure.AutoConfigurationReport.ConditionAndOutcomes; import org.springframework.boot.autoconfigure.AutoConfigurationReport.ConditionAndOutcomes;
import org.springframework.boot.event.ApplicationFailedEvent;
import org.springframework.boot.logging.LogLevel; import org.springframework.boot.logging.LogLevel;
import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
@ -76,7 +76,7 @@ public class AutoConfigurationReportLoggingInitializer implements
@Override @Override
public boolean supportsEventType(Class<? extends ApplicationEvent> type) { public boolean supportsEventType(Class<? extends ApplicationEvent> type) {
return ContextRefreshedEvent.class.isAssignableFrom(type) return ContextRefreshedEvent.class.isAssignableFrom(type)
|| SpringApplicationErrorEvent.class.isAssignableFrom(type); || ApplicationFailedEvent.class.isAssignableFrom(type);
} }
@Override @Override
@ -91,8 +91,8 @@ public class AutoConfigurationReportLoggingInitializer implements
logAutoConfigurationReport(); logAutoConfigurationReport();
} }
} }
else if (event instanceof SpringApplicationErrorEvent) { else if (event instanceof ApplicationFailedEvent) {
if (((SpringApplicationErrorEvent) event).getApplicationContext() == this.applicationContext) { if (((ApplicationFailedEvent) event).getApplicationContext() == this.applicationContext) {
logAutoConfigurationReport(true); logAutoConfigurationReport(true);
} }
} }

@ -30,9 +30,9 @@ import org.junit.Test;
import org.mockito.invocation.InvocationOnMock; import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationErrorEvent;
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration; import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.event.ApplicationFailedEvent;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -127,8 +127,8 @@ public class AutoConfigurationReportLoggingInitializerTests {
fail("Did not error"); fail("Did not error");
} }
catch (Exception ex) { catch (Exception ex) {
this.initializer.onApplicationEvent(new SpringApplicationErrorEvent( this.initializer.onApplicationEvent(new ApplicationFailedEvent(
new SpringApplication(), context, new String[] {}, ex)); new SpringApplication(), new String[0], context, ex));
} }
assertThat(this.debugLog.size(), not(equalTo(0))); assertThat(this.debugLog.size(), not(equalTo(0)));
@ -146,8 +146,8 @@ public class AutoConfigurationReportLoggingInitializerTests {
fail("Did not error"); fail("Did not error");
} }
catch (Exception ex) { catch (Exception ex) {
this.initializer.onApplicationEvent(new SpringApplicationErrorEvent( this.initializer.onApplicationEvent(new ApplicationFailedEvent(
new SpringApplication(), context, new String[] {}, ex)); new SpringApplication(), new String[0], context, ex));
} }
assertThat(this.debugLog.size(), equalTo(0)); assertThat(this.debugLog.size(), equalTo(0));
@ -190,8 +190,8 @@ public class AutoConfigurationReportLoggingInitializerTests {
@Test @Test
public void noErrorIfNotInitialized() throws Exception { public void noErrorIfNotInitialized() throws Exception {
this.initializer.onApplicationEvent(new SpringApplicationErrorEvent( this.initializer.onApplicationEvent(new ApplicationFailedEvent(
new SpringApplication(), null, new String[0], new RuntimeException( new SpringApplication(), new String[0], null, new RuntimeException(
"Planned"))); "Planned")));
assertThat(this.infoLog.get(0), assertThat(this.infoLog.get(0),
containsString("Unable to provide auto-configuration report")); containsString("Unable to provide auto-configuration report"));

@ -18,7 +18,6 @@ package org.springframework.boot.autoconfigure.security;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationBeforeRefreshEvent;
import org.springframework.boot.autoconfigure.AutoConfigurationReportLoggingInitializer; import org.springframework.boot.autoconfigure.AutoConfigurationReportLoggingInitializer;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage; 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.HibernateJpaAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.test.City; import org.springframework.boot.autoconfigure.orm.jpa.test.City;
import org.springframework.boot.context.listener.LoggingApplicationListener; import org.springframework.boot.context.listener.LoggingApplicationListener;
import org.springframework.boot.event.ApplicationPreparedEvent;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -119,8 +119,8 @@ public class SecurityAutoConfigurationTests {
AnnotationConfigWebApplicationContext context) { AnnotationConfigWebApplicationContext context) {
EnvironmentTestUtils.addEnvironment(context, "debug:true"); EnvironmentTestUtils.addEnvironment(context, "debug:true");
LoggingApplicationListener logging = new LoggingApplicationListener(); LoggingApplicationListener logging = new LoggingApplicationListener();
logging.onApplicationEvent(new SpringApplicationBeforeRefreshEvent( logging.onApplicationEvent(new ApplicationPreparedEvent(new SpringApplication(),
new SpringApplication(), context, new String[0])); new String[0], context));
AutoConfigurationReportLoggingInitializer initializer = new AutoConfigurationReportLoggingInitializer(); AutoConfigurationReportLoggingInitializer initializer = new AutoConfigurationReportLoggingInitializer();
initializer.initialize(context); initializer.initialize(context);
context.refresh(); context.refresh();

@ -36,6 +36,10 @@ import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; 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.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
@ -313,8 +317,9 @@ public class SpringApplication {
try { try {
Set<Object> sources = getSources(); Set<Object> sources = getSources();
registerListeners(multicaster, sources); registerListeners(multicaster, sources);
// Allow logging and stuff to initialize very early // 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 // Create and configure the environment
ConfigurableEnvironment environment = getOrCreateEnvironment(); ConfigurableEnvironment environment = getOrCreateEnvironment();
@ -324,12 +329,13 @@ public class SpringApplication {
} }
// Notify listeners of the environment creation // Notify listeners of the environment creation
multicaster.multicastEvent(new SpringApplicationEnvironmentAvailableEvent( multicaster.multicastEvent(new ApplicationEnvironmentPreparedEvent(this,
this, environment, args)); args, environment));
// Sources might have changed when environment was applied // Sources might have changed when environment was applied
sources = getSources(); sources = getSources();
Assert.notEmpty(sources, "Sources must not be empty"); Assert.notEmpty(sources, "Sources must not be empty");
if (this.showBanner) { if (this.showBanner) {
printBanner(); printBanner();
} }
@ -349,9 +355,10 @@ public class SpringApplication {
} }
load(context, sources.toArray(new Object[sources.size()])); load(context, sources.toArray(new Object[sources.size()]));
// Notify listeners of intention to refresh // Notify listeners of intention to refresh
multicaster.multicastEvent(new SpringApplicationBeforeRefreshEvent(this, multicaster.multicastEvent(new ApplicationPreparedEvent(this, args, context));
context, args));
refresh(context); refresh(context);
stopWatch.stop(); stopWatch.stop();
@ -364,21 +371,21 @@ public class SpringApplication {
return context; return context;
} }
catch (RuntimeException ex) { catch (RuntimeException ex) {
handleError(context, multicaster, ex, args); handleFailure(context, multicaster, ex, args);
throw ex; throw ex;
} }
catch (Error ex) { catch (Error ex) {
handleError(context, multicaster, ex, args); handleFailure(context, multicaster, ex, args);
throw ex; throw ex;
} }
} }
protected void handleError(ConfigurableApplicationContext context, protected void handleFailure(ConfigurableApplicationContext context,
ApplicationEventMulticaster multicaster, Throwable exception, String... args) { ApplicationEventMulticaster multicaster, Throwable exception, String... args) {
try { try {
multicaster.multicastEvent(new SpringApplicationErrorEvent(this, context, multicaster.multicastEvent(new ApplicationFailedEvent(this, args, context,
args, exception)); exception));
} }
catch (Exception ex) { catch (Exception ex) {
// We don't want to fail here and mask the original exception // We don't want to fail here and mask the original exception
@ -1024,7 +1031,7 @@ public class SpringApplication {
ApplicationEvent event) { ApplicationEvent event) {
List<ApplicationListener<?>> listeners = new ArrayList<ApplicationListener<?>>(); List<ApplicationListener<?>> listeners = new ArrayList<ApplicationListener<?>>();
listeners.addAll(super.getApplicationListeners(event)); listeners.addAll(super.getApplicationListeners(event));
if (event instanceof SpringApplicationErrorEvent) { if (event instanceof ApplicationFailedEvent) {
Collections.reverse(listeners); Collections.reverse(listeners);
} }
return listeners; return listeners;

@ -21,16 +21,16 @@ import java.util.Arrays;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.boot.SpringApplicationErrorEvent; import org.springframework.boot.event.ApplicationFailedEvent;
import org.springframework.boot.SpringApplicationStartEvent; import org.springframework.boot.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.SmartApplicationListener; import org.springframework.context.event.SmartApplicationListener;
/** /**
* A {@link SmartApplicationListener} that reacts to {@link SpringApplicationStartEvent * A {@link SmartApplicationListener} that reacts to {@link ApplicationStartedEvent start
* start events} by logging the classpath of the thread context class loader (TCCL) at * events} by logging the classpath of the thread context class loader (TCCL) at
* {@code DEBUG} level and to {@link SpringApplicationErrorEvent error events} by logging * {@code DEBUG} level and to {@link ApplicationFailedEvent error events} by logging the
* the TCCL's classpath at {@code INFO} level. * TCCL's classpath at {@code INFO} level.
* *
* @author Andy Wilkinson * @author Andy Wilkinson
*/ */
@ -43,13 +43,13 @@ public final class ClasspathLoggingApplicationListener implements
@Override @Override
public void onApplicationEvent(ApplicationEvent event) { public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof SpringApplicationStartEvent) { if (event instanceof ApplicationStartedEvent) {
if (this.logger.isDebugEnabled()) { if (this.logger.isDebugEnabled()) {
this.logger this.logger
.debug("Application started with classpath: " + getClasspath()); .debug("Application started with classpath: " + getClasspath());
} }
} }
else if (event instanceof SpringApplicationErrorEvent) { else if (event instanceof ApplicationFailedEvent) {
if (this.logger.isInfoEnabled()) { if (this.logger.isInfoEnabled()) {
this.logger.info("Application failed to start with classpath: " this.logger.info("Application failed to start with classpath: "
+ getClasspath()); + getClasspath());
@ -64,8 +64,8 @@ public final class ClasspathLoggingApplicationListener implements
@Override @Override
public boolean supportsEventType(Class<? extends ApplicationEvent> type) { public boolean supportsEventType(Class<? extends ApplicationEvent> type) {
return SpringApplicationStartEvent.class.isAssignableFrom(type) return ApplicationStartedEvent.class.isAssignableFrom(type)
|| SpringApplicationErrorEvent.class.isAssignableFrom(type); || ApplicationFailedEvent.class.isAssignableFrom(type);
} }
@Override @Override

@ -31,12 +31,12 @@ import java.util.Set;
import org.springframework.beans.PropertyValues; import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationEnvironmentAvailableEvent;
import org.springframework.boot.bind.PropertySourcesPropertyValues; import org.springframework.boot.bind.PropertySourcesPropertyValues;
import org.springframework.boot.bind.RelaxedDataBinder; import org.springframework.boot.bind.RelaxedDataBinder;
import org.springframework.boot.config.DefaultPropertySourceLoadersFactory; import org.springframework.boot.config.DefaultPropertySourceLoadersFactory;
import org.springframework.boot.config.PropertySourceLoader; import org.springframework.boot.config.PropertySourceLoader;
import org.springframework.boot.config.PropertySourceLoadersFactory; import org.springframework.boot.config.PropertySourceLoadersFactory;
import org.springframework.boot.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader; import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
import org.springframework.context.annotation.PropertySources; import org.springframework.context.annotation.PropertySources;
@ -81,7 +81,7 @@ import org.springframework.util.StringUtils;
* @author Phillip Webb * @author Phillip Webb
*/ */
public class ConfigFileApplicationListener implements public class ConfigFileApplicationListener implements
ApplicationListener<SpringApplicationEnvironmentAvailableEvent>, Ordered { ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {
private static final String ACTIVE_PROFILES_PROPERTY = "spring.profiles.active"; private static final String ACTIVE_PROFILES_PROPERTY = "spring.profiles.active";
@ -110,7 +110,7 @@ public class ConfigFileApplicationListener implements
* ("spring.main.show_banner=false"). * ("spring.main.show_banner=false").
*/ */
@Override @Override
public void onApplicationEvent(SpringApplicationEnvironmentAvailableEvent event) { public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
Environment environment = event.getEnvironment(); Environment environment = event.getEnvironment();
if (environment instanceof ConfigurableEnvironment) { if (environment instanceof ConfigurableEnvironment) {
configure((ConfigurableEnvironment) environment, event.getSpringApplication()); configure((ConfigurableEnvironment) environment, event.getSpringApplication());

@ -20,7 +20,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.springframework.beans.BeanUtils; 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.ApplicationContextException;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
@ -52,8 +52,8 @@ public class EnvironmentDelegateApplicationListener implements
@Override @Override
public void onApplicationEvent(ApplicationEvent event) { public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof SpringApplicationEnvironmentAvailableEvent) { if (event instanceof ApplicationEnvironmentPreparedEvent) {
List<ApplicationListener<ApplicationEvent>> delegates = getListeners(((SpringApplicationEnvironmentAvailableEvent) event) List<ApplicationListener<ApplicationEvent>> delegates = getListeners(((ApplicationEnvironmentPreparedEvent) event)
.getEnvironment()); .getEnvironment());
if (delegates.isEmpty()) { if (delegates.isEmpty()) {
return; return;

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.boot.SpringApplicationEnvironmentAvailableEvent;
import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
/** /**
@ -42,12 +42,12 @@ import org.springframework.context.ApplicationListener;
* @author Dave Syer * @author Dave Syer
*/ */
public class FileEncodingApplicationListener implements public class FileEncodingApplicationListener implements
ApplicationListener<SpringApplicationEnvironmentAvailableEvent> { ApplicationListener<ApplicationEnvironmentPreparedEvent> {
private static Log logger = LogFactory.getLog(FileEncodingApplicationListener.class); private static Log logger = LogFactory.getLog(FileEncodingApplicationListener.class);
@Override @Override
public void onApplicationEvent(SpringApplicationEnvironmentAvailableEvent event) { public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver( RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(
event.getEnvironment(), "spring."); event.getEnvironment(), "spring.");
if (resolver.containsProperty("mandatoryFileEncoding")) { if (resolver.containsProperty("mandatoryFileEncoding")) {

@ -26,8 +26,8 @@ import java.util.Map;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationEnvironmentAvailableEvent; import org.springframework.boot.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.SpringApplicationStartEvent; import org.springframework.boot.event.ApplicationStartedEvent;
import org.springframework.boot.logging.LogLevel; import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.LoggingSystem; import org.springframework.boot.logging.LoggingSystem;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
@ -94,9 +94,8 @@ public class LoggingApplicationListener implements SmartApplicationListener {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private static Collection<Class<? extends ApplicationEvent>> EVENT_TYPES = Arrays private static Collection<Class<? extends ApplicationEvent>> EVENT_TYPES = Arrays
.<Class<? extends ApplicationEvent>> asList( .<Class<? extends ApplicationEvent>> asList(ApplicationStartedEvent.class,
SpringApplicationStartEvent.class, ApplicationEnvironmentPreparedEvent.class);
SpringApplicationEnvironmentAvailableEvent.class);
private final Log logger = LogFactory.getLog(getClass()); private final Log logger = LogFactory.getLog(getClass());
@ -123,8 +122,8 @@ public class LoggingApplicationListener implements SmartApplicationListener {
@Override @Override
public void onApplicationEvent(ApplicationEvent event) { public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof SpringApplicationEnvironmentAvailableEvent) { if (event instanceof ApplicationEnvironmentPreparedEvent) {
SpringApplicationEnvironmentAvailableEvent available = (SpringApplicationEnvironmentAvailableEvent) event; ApplicationEnvironmentPreparedEvent available = (ApplicationEnvironmentPreparedEvent) event;
initialize(available.getEnvironment(), available.getSpringApplication() initialize(available.getEnvironment(), available.getSpringApplication()
.getClassLoader()); .getClassLoader());
} }

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.boot.SpringApplicationEnvironmentAvailableEvent;
import org.springframework.boot.config.JsonParser; import org.springframework.boot.config.JsonParser;
import org.springframework.boot.config.JsonParserFactory; import org.springframework.boot.config.JsonParserFactory;
import org.springframework.boot.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.env.CommandLinePropertySource; import org.springframework.core.env.CommandLinePropertySource;
@ -88,7 +88,7 @@ import org.springframework.util.StringUtils;
* @author Dave Syer * @author Dave Syer
*/ */
public class VcapApplicationListener implements public class VcapApplicationListener implements
ApplicationListener<SpringApplicationEnvironmentAvailableEvent>, Ordered { ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {
private static final Log logger = LogFactory.getLog(VcapApplicationListener.class); private static final Log logger = LogFactory.getLog(VcapApplicationListener.class);
@ -112,7 +112,7 @@ public class VcapApplicationListener implements
} }
@Override @Override
public void onApplicationEvent(SpringApplicationEnvironmentAvailableEvent event) { public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
ConfigurableEnvironment environment = event.getEnvironment(); ConfigurableEnvironment environment = event.getEnvironment();
if (!environment.containsProperty(VCAP_APPLICATION) if (!environment.containsProperty(VCAP_APPLICATION)

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -14,9 +14,9 @@
* limitations under the License. * 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.ConfigurableEnvironment;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
@ -26,36 +26,19 @@ import org.springframework.core.env.Environment;
* *
* @author Dave Syer * @author Dave Syer
*/ */
public class SpringApplicationEnvironmentAvailableEvent extends ApplicationEvent { public class ApplicationEnvironmentPreparedEvent extends SpringApplicationEvent {
private final ConfigurableEnvironment environment; private final ConfigurableEnvironment environment;
private final String[] args;
/** /**
* @param springApplication the current application * @param application the current application
* @param environment the environment that was just created
* @param args the argumemts the application is running with * @param args the argumemts the application is running with
* @param environment the environment that was just created
*/ */
public SpringApplicationEnvironmentAvailableEvent( public ApplicationEnvironmentPreparedEvent(SpringApplication application,
SpringApplication springApplication, ConfigurableEnvironment environment, String[] args, ConfigurableEnvironment environment) {
String[] args) { super(application, args);
super(springApplication);
this.environment = environment; this.environment = environment;
this.args = args;
}
/**
* @return the springApplication
*/
public SpringApplication getSpringApplication() {
return (SpringApplication) getSource();
}
/**
* @return the args
*/
public String[] getArgs() {
return this.args;
} }
/** /**

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -14,9 +14,9 @@
* limitations under the License. * 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; import org.springframework.context.ConfigurableApplicationContext;
/** /**
@ -24,23 +24,22 @@ import org.springframework.context.ConfigurableApplicationContext;
* *
* @author Dave Syer * @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 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 context the context that was being created (maybe null)
* @param args the arguments the application was running with * @param args the arguments the application was running with
* @param exception the exception that caused the error * @param exception the exception that caused the error
*/ */
public SpringApplicationErrorEvent(SpringApplication springApplication, public ApplicationFailedEvent(SpringApplication application, String[] args,
ConfigurableApplicationContext context, String[] args, Throwable exception) { ConfigurableApplicationContext context, Throwable exception) {
super(springApplication); super(application, args);
this.context = context; this.context = context;
this.args = args;
this.exception = exception; this.exception = exception;
} }
@ -51,13 +50,6 @@ public class SpringApplicationErrorEvent extends ApplicationEvent {
return this.context; return this.context;
} }
/**
* @return the springApplication
*/
public SpringApplication getSpringApplication() {
return (SpringApplication) getSource();
}
/** /**
* @return the exception * @return the exception
*/ */
@ -65,11 +57,4 @@ public class SpringApplicationErrorEvent extends ApplicationEvent {
return this.exception; return this.exception;
} }
/**
* @return the args
*/
public String[] getArgs() {
return this.args;
}
} }

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -14,49 +14,33 @@
* limitations under the License. * 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.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
/** /**
* Event published as when a {@link SpringApplication} is starting up and the * 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 * {@link ApplicationContext} is fully prepared but not refreshed. The bean definitions
* the {@link Environment} is ready for use at this stage. * will be loaded and the {@link Environment} is ready for use at this stage.
* *
* @author Dave Syer * @author Dave Syer
*/ */
public class SpringApplicationBeforeRefreshEvent extends ApplicationEvent { public class ApplicationPreparedEvent extends SpringApplicationEvent {
private final String[] args;
private final ConfigurableApplicationContext context; private final ConfigurableApplicationContext context;
/** /**
* @param springApplication the current application * @param application the current application
* @param context the ApplicationContext about to be refreshed
* @param args the argumemts the application is running with * @param args the argumemts the application is running with
* @param context the ApplicationContext about to be refreshed
*/ */
public SpringApplicationBeforeRefreshEvent(SpringApplication springApplication, public ApplicationPreparedEvent(SpringApplication application, String[] args,
ConfigurableApplicationContext context, String[] args) { ConfigurableApplicationContext context) {
super(springApplication); super(application, args);
this.context = context; this.context = context;
this.args = args;
}
/**
* @return the springApplication
*/
public SpringApplication getSpringApplication() {
return (SpringApplication) getSource();
}
/**
* @return the args
*/
public String[] getArgs() {
return this.args;
} }
/** /**

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -14,10 +14,10 @@
* limitations under the License. * 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.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
@ -30,31 +30,14 @@ import org.springframework.core.env.Environment;
* *
* @author Dave Syer * @author Dave Syer
*/ */
public class SpringApplicationStartEvent extends ApplicationEvent { public class ApplicationStartedEvent extends SpringApplicationEvent {
private final String[] args;
/** /**
* @param springApplication the current application * @param application the current application
* @param args the argumemts the application is running with * @param args the argumemts the application is running with
*/ */
public SpringApplicationStartEvent(SpringApplication springApplication, String[] args) { public ApplicationStartedEvent(SpringApplication application, String[] args) {
super(springApplication); super(application, args);
this.args = args;
}
/**
* @return the springApplication
*/
public SpringApplication getSpringApplication() {
return (SpringApplication) getSource();
}
/**
* @return the args
*/
public String[] getArgs() {
return this.args;
} }
} }

@ -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;
}
}

@ -5,7 +5,7 @@ import liquibase.servicelocator.ServiceLocator;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.context.ApplicationListener;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
@ -17,12 +17,12 @@ import org.springframework.util.ClassUtils;
* @author Dave Syer * @author Dave Syer
*/ */
public class LiquibaseServiceLocatorInitializer implements public class LiquibaseServiceLocatorInitializer implements
ApplicationListener<SpringApplicationStartEvent> { ApplicationListener<ApplicationStartedEvent> {
static final Log logger = LogFactory.getLog(LiquibaseServiceLocatorInitializer.class); static final Log logger = LogFactory.getLog(LiquibaseServiceLocatorInitializer.class);
@Override @Override
public void onApplicationEvent(SpringApplicationStartEvent event) { public void onApplicationEvent(ApplicationStartedEvent event) {
if (ClassUtils.isPresent("liquibase.servicelocator.ServiceLocator", null)) { if (ClassUtils.isPresent("liquibase.servicelocator.ServiceLocator", null)) {
new LiquibasePresent().replaceServiceLocator(); new LiquibasePresent().replaceServiceLocator();
} }

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -41,7 +41,8 @@ public class ServletListenerRegistrationBeanTests {
@Rule @Rule
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();
private final ServletContextListener listener = Mockito.mock(ServletContextListener.class); private final ServletContextListener listener = Mockito
.mock(ServletContextListener.class);
@Mock @Mock
private ServletContext servletContext; private ServletContext servletContext;

@ -24,9 +24,9 @@ import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationEnvironmentAvailableEvent;
import org.springframework.boot.config.PropertySourceLoader; import org.springframework.boot.config.PropertySourceLoader;
import org.springframework.boot.config.PropertySourceLoadersFactory; import org.springframework.boot.config.PropertySourceLoadersFactory;
import org.springframework.boot.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -56,8 +56,8 @@ public class ConfigFileApplicationListenerTests {
private final StandardEnvironment environment = new StandardEnvironment(); private final StandardEnvironment environment = new StandardEnvironment();
private final SpringApplicationEnvironmentAvailableEvent event = new SpringApplicationEnvironmentAvailableEvent( private final ApplicationEnvironmentPreparedEvent event = new ApplicationEnvironmentPreparedEvent(
new SpringApplication(), this.environment, new String[0]); new SpringApplication(), new String[0], this.environment);
private final ConfigFileApplicationListener initializer = new ConfigFileApplicationListener(); private final ConfigFileApplicationListener initializer = new ConfigFileApplicationListener();

@ -21,7 +21,7 @@ import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.springframework.boot.SpringApplication; 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.boot.test.EnvironmentTestUtils;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
@ -53,8 +53,8 @@ public class EnvironmentDelegateApplicationListenerTests {
public void orderedInitialize() throws Exception { public void orderedInitialize() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "context.listener.classes:" EnvironmentTestUtils.addEnvironment(this.context, "context.listener.classes:"
+ MockInitB.class.getName() + "," + MockInitA.class.getName()); + MockInitB.class.getName() + "," + MockInitA.class.getName());
this.listener.onApplicationEvent(new SpringApplicationEnvironmentAvailableEvent( this.listener.onApplicationEvent(new ApplicationEnvironmentPreparedEvent(
new SpringApplication(), this.context.getEnvironment(), new String[0])); new SpringApplication(), new String[0], this.context.getEnvironment()));
this.context.getBeanFactory().registerSingleton("testListener", this.listener); this.context.getBeanFactory().registerSingleton("testListener", this.listener);
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBeanFactory().getSingleton("a"), equalTo((Object) "a")); assertThat(this.context.getBeanFactory().getSingleton("a"), equalTo((Object) "a"));
@ -63,15 +63,15 @@ public class EnvironmentDelegateApplicationListenerTests {
@Test @Test
public void noInitializers() throws Exception { public void noInitializers() throws Exception {
this.listener.onApplicationEvent(new SpringApplicationEnvironmentAvailableEvent( this.listener.onApplicationEvent(new ApplicationEnvironmentPreparedEvent(
new SpringApplication(), this.context.getEnvironment(), new String[0])); new SpringApplication(), new String[0], this.context.getEnvironment()));
} }
@Test @Test
public void emptyInitializers() throws Exception { public void emptyInitializers() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "context.listener.classes:"); EnvironmentTestUtils.addEnvironment(this.context, "context.listener.classes:");
this.listener.onApplicationEvent(new SpringApplicationEnvironmentAvailableEvent( this.listener.onApplicationEvent(new ApplicationEnvironmentPreparedEvent(
new SpringApplication(), this.context.getEnvironment(), new String[0])); new SpringApplication(), new String[0], this.context.getEnvironment()));
} }
@Order(Ordered.HIGHEST_PRECEDENCE) @Order(Ordered.HIGHEST_PRECEDENCE)

@ -19,7 +19,7 @@ package org.springframework.boot.context.listener;
import org.junit.Assume; import org.junit.Assume;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.SpringApplication; 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.boot.test.EnvironmentTestUtils;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.StandardEnvironment;
@ -33,8 +33,8 @@ public class FileEncodingApplicationListenerTests {
private final FileEncodingApplicationListener initializer = new FileEncodingApplicationListener(); private final FileEncodingApplicationListener initializer = new FileEncodingApplicationListener();
private final ConfigurableEnvironment environment = new StandardEnvironment(); private final ConfigurableEnvironment environment = new StandardEnvironment();
private final SpringApplicationEnvironmentAvailableEvent event = new SpringApplicationEnvironmentAvailableEvent( private final ApplicationEnvironmentPreparedEvent event = new ApplicationEnvironmentPreparedEvent(
new SpringApplication(), this.environment, new String[0]); new SpringApplication(), new String[0], this.environment);
@Test(expected = IllegalStateException.class) @Test(expected = IllegalStateException.class)
public void testIllegalState() { public void testIllegalState() {

@ -29,7 +29,7 @@ import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.springframework.boot.SpringApplication; 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.LogLevel;
import org.springframework.boot.logging.java.JavaLoggingSystem; import org.springframework.boot.logging.java.JavaLoggingSystem;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
@ -70,7 +70,7 @@ public class LoggingApplicationListenerTests {
public void init() throws SecurityException, IOException { public void init() throws SecurityException, IOException {
LogManager.getLogManager().readConfiguration( LogManager.getLogManager().readConfiguration(
JavaLoggingSystem.class.getResourceAsStream("logging.properties")); JavaLoggingSystem.class.getResourceAsStream("logging.properties"));
this.initializer.onApplicationEvent(new SpringApplicationStartEvent( this.initializer.onApplicationEvent(new ApplicationStartedEvent(
new SpringApplication(), NO_ARGS)); new SpringApplication(), NO_ARGS));
new File("target/foo.log").delete(); new File("target/foo.log").delete();
} }
@ -190,7 +190,7 @@ public class LoggingApplicationListenerTests {
public void parseArgsDoesntReplace() throws Exception { public void parseArgsDoesntReplace() throws Exception {
this.initializer.setSpringBootLogging(LogLevel.ERROR); this.initializer.setSpringBootLogging(LogLevel.ERROR);
this.initializer.setParseArgs(false); this.initializer.setParseArgs(false);
this.initializer.onApplicationEvent(new SpringApplicationStartEvent( this.initializer.onApplicationEvent(new ApplicationStartedEvent(
this.springApplication, new String[] { "--debug" })); this.springApplication, new String[] { "--debug" }));
this.initializer.initialize(this.context.getEnvironment(), this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader()); this.context.getClassLoader());

@ -18,7 +18,7 @@ package org.springframework.boot.context.listener;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.SpringApplication; 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.boot.test.EnvironmentTestUtils;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -34,8 +34,8 @@ public class VcapApplicationListenerTests {
private final VcapApplicationListener initializer = new VcapApplicationListener(); private final VcapApplicationListener initializer = new VcapApplicationListener();
private final ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(); private final ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
private final SpringApplicationEnvironmentAvailableEvent event = new SpringApplicationEnvironmentAvailableEvent( private final ApplicationEnvironmentPreparedEvent event = new ApplicationEnvironmentPreparedEvent(
new SpringApplication(), this.context.getEnvironment(), new String[0]); new SpringApplication(), new String[0], this.context.getEnvironment());
@Test @Test
public void testApplicationProperties() { public void testApplicationProperties() {

@ -40,8 +40,8 @@ public class LogbackLoggingSystemTests {
@Rule @Rule
public OutputCapture output = new OutputCapture(); public OutputCapture output = new OutputCapture();
private final LogbackLoggingSystem loggingSystem = new LogbackLoggingSystem(getClass() private final LogbackLoggingSystem loggingSystem = new LogbackLoggingSystem(
.getClassLoader()); getClass().getClassLoader());
private Log logger; private Log logger;

Loading…
Cancel
Save