Browse Source
Fix package tangle between the `org.springframework.boot` and the `event` sub-package. A new `SpringApplicationRunParticipant` abstraction has been introduced, with the `event` package solely responsible for handling event multi-casting.pull/313/head
4 changed files with 247 additions and 127 deletions
@ -0,0 +1,69 @@
@@ -0,0 +1,69 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.context.ConfigurableApplicationContext; |
||||
import org.springframework.core.env.ConfigurableEnvironment; |
||||
import org.springframework.core.io.support.SpringFactoriesLoader; |
||||
|
||||
/** |
||||
* Strategy class to allow dynamic participation when the {@link SpringApplication} |
||||
* {@code run} method is called. Participants are loaded via the |
||||
* {@link SpringFactoriesLoader} and should declare a public constructor that accepts a |
||||
* {@link SpringApplication} instance and a {@code String[]} of arguments. A new |
||||
* {@link SpringApplicationRunParticipant} instance will be created for each run. |
||||
* |
||||
* @author Phillip Webb |
||||
*/ |
||||
public interface SpringApplicationRunParticipant { |
||||
|
||||
/** |
||||
* Called immediately when the run method has first started. Can be used for very |
||||
* early initialization. |
||||
*/ |
||||
void started(); |
||||
|
||||
/** |
||||
* Called once the environment has been prepared, but before the |
||||
* {@link ApplicationContext} has been created. |
||||
* @param environment the environment |
||||
*/ |
||||
void environmentPrepared(ConfigurableEnvironment environment); |
||||
|
||||
/** |
||||
* Called once the {@link ApplicationContext} has been created and prepared, but |
||||
* before sources have been loaded. |
||||
* @param context the application context |
||||
*/ |
||||
void contextPrepared(ConfigurableApplicationContext context); |
||||
|
||||
/** |
||||
* Called once the application context has been loaded but before it has been |
||||
* refreshed. |
||||
* @param context the application context |
||||
*/ |
||||
void contextLoaded(ConfigurableApplicationContext context); |
||||
|
||||
/** |
||||
* Called immediately before the run method finishes. |
||||
* @param context the application context |
||||
* @param exception any run exception or null if run completed successfully. |
||||
*/ |
||||
void finished(ConfigurableApplicationContext context, Throwable exception); |
||||
|
||||
} |
||||
@ -0,0 +1,95 @@
@@ -0,0 +1,95 @@
|
||||
/* |
||||
* 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.beans.factory.BeanFactoryAware; |
||||
import org.springframework.boot.SpringApplication; |
||||
import org.springframework.boot.SpringApplicationRunParticipant; |
||||
import org.springframework.context.ApplicationListener; |
||||
import org.springframework.context.ConfigurableApplicationContext; |
||||
import org.springframework.context.event.ApplicationEventMulticaster; |
||||
import org.springframework.context.event.SimpleApplicationEventMulticaster; |
||||
import org.springframework.context.support.AbstractApplicationContext; |
||||
import org.springframework.core.env.ConfigurableEnvironment; |
||||
|
||||
/** |
||||
* {@link SpringApplicationRunParticipant} to publish {@link SpringApplicationEvent}s. |
||||
* |
||||
* @author Phillip Webb |
||||
*/ |
||||
public class EventPublishingRunParticipant implements SpringApplicationRunParticipant { |
||||
|
||||
private final ApplicationEventMulticaster multicaster; |
||||
|
||||
private SpringApplication application; |
||||
|
||||
private String[] args; |
||||
|
||||
public EventPublishingRunParticipant(SpringApplication application, String[] args) { |
||||
this.application = application; |
||||
this.args = args; |
||||
this.multicaster = new SimpleApplicationEventMulticaster(); |
||||
for (ApplicationListener<?> listener : application.getListeners()) { |
||||
this.multicaster.addApplicationListener(listener); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void started() { |
||||
publishEvent(new ApplicationStartedEvent(this.application, this.args)); |
||||
} |
||||
|
||||
@Override |
||||
public void environmentPrepared(ConfigurableEnvironment environment) { |
||||
publishEvent(new ApplicationEnvironmentPreparedEvent(this.application, this.args, |
||||
environment)); |
||||
} |
||||
|
||||
@Override |
||||
public void contextPrepared(ConfigurableApplicationContext context) { |
||||
registerApplicationEventMulticaster(context); |
||||
} |
||||
|
||||
private void registerApplicationEventMulticaster( |
||||
ConfigurableApplicationContext context) { |
||||
context.getBeanFactory().registerSingleton( |
||||
AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME, |
||||
this.multicaster); |
||||
if (this.multicaster instanceof BeanFactoryAware) { |
||||
((BeanFactoryAware) this.multicaster) |
||||
.setBeanFactory(context.getBeanFactory()); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void contextLoaded(ConfigurableApplicationContext context) { |
||||
publishEvent(new ApplicationPreparedEvent(this.application, this.args, context)); |
||||
} |
||||
|
||||
@Override |
||||
public void finished(ConfigurableApplicationContext context, Throwable exception) { |
||||
if (exception != null) { |
||||
publishEvent(new ApplicationFailedEvent(this.application, this.args, context, |
||||
exception)); |
||||
} |
||||
} |
||||
|
||||
private void publishEvent(SpringApplicationEvent event) { |
||||
this.multicaster.multicastEvent(event); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue