mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Polish ParentContextCloserApplicationListener
Fix formatting for the recent ParentContextCloserApplicationListener change, also some minor internal renames.
This commit is contained in:
parent
9d2983e994
commit
39cbb1d0ae
@ -25,13 +25,15 @@ import org.springframework.context.event.ContextClosedEvent;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
/**
|
||||
* Listener that closes the application context if its parent is closed. It listens for refresh events and grabs the
|
||||
* current context from there, and then listens for closed events and propagates it down the hierarchy.
|
||||
* Listener that closes the application context if its parent is closed. It listens for
|
||||
* refresh events and grabs the current context from there, and then listens for closed
|
||||
* events and propagates it down the hierarchy.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Eric Bottard
|
||||
*/
|
||||
public class ParentContextCloserApplicationListener implements ApplicationListener<ParentContextAvailableEvent>,
|
||||
Ordered {
|
||||
public class ParentContextCloserApplicationListener implements
|
||||
ApplicationListener<ParentContextAvailableEvent>, Ordered {
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
@ -39,38 +41,43 @@ public class ParentContextCloserApplicationListener implements ApplicationListen
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void onApplicationEvent(ParentContextAvailableEvent event) {
|
||||
public void onApplicationEvent(ParentContextAvailableEvent event) {
|
||||
maybeInstallListenerInParent(event.getApplicationContext());
|
||||
}
|
||||
|
||||
private void maybeInstallListenerInParent(ConfigurableApplicationContext child) {
|
||||
if (child.getParent() instanceof ConfigurableApplicationContext) {
|
||||
ConfigurableApplicationContext parent = (ConfigurableApplicationContext) child.getParent();
|
||||
ConfigurableApplicationContext parent = (ConfigurableApplicationContext) child
|
||||
.getParent();
|
||||
parent.addApplicationListener(createContextCloserListener(child));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subclasses may override to create their own subclass of ContextCloserListener. This still enforces the use of a
|
||||
* weak reference.
|
||||
* Subclasses may override to create their own subclass of ContextCloserListener. This
|
||||
* still enforces the use of a weak reference.
|
||||
*/
|
||||
protected ContextCloserListener createContextCloserListener(ConfigurableApplicationContext child) {
|
||||
protected ContextCloserListener createContextCloserListener(
|
||||
ConfigurableApplicationContext child) {
|
||||
return new ContextCloserListener(child);
|
||||
}
|
||||
|
||||
protected static class ContextCloserListener implements ApplicationListener<ContextClosedEvent> {
|
||||
protected static class ContextCloserListener implements
|
||||
ApplicationListener<ContextClosedEvent> {
|
||||
|
||||
private WeakReference<ConfigurableApplicationContext> contextRef;
|
||||
|
||||
public ContextCloserListener(ConfigurableApplicationContext context) {
|
||||
this.contextRef = new WeakReference<ConfigurableApplicationContext>(context);
|
||||
private WeakReference<ConfigurableApplicationContext> childContext;
|
||||
|
||||
public ContextCloserListener(ConfigurableApplicationContext childContext) {
|
||||
this.childContext = new WeakReference<ConfigurableApplicationContext>(
|
||||
childContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ContextClosedEvent event) {
|
||||
ConfigurableApplicationContext context = contextRef.get();
|
||||
if (context != null && event.getApplicationContext() == context.getParent() && context.isActive()) {
|
||||
ConfigurableApplicationContext context = this.childContext.get();
|
||||
if ((context != null)
|
||||
&& (event.getApplicationContext() == context.getParent())
|
||||
&& context.isActive()) {
|
||||
context.close();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user