diff --git a/spring-boot/src/main/java/org/springframework/boot/builder/ParentContextCloserApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/builder/ParentContextCloserApplicationListener.java index dce32b404d7..bb26b38fe9a 100644 --- a/spring-boot/src/main/java/org/springframework/boot/builder/ParentContextCloserApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/builder/ParentContextCloserApplicationListener.java @@ -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, - Ordered { +public class ParentContextCloserApplicationListener implements + ApplicationListener, 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 { + protected static class ContextCloserListener implements + ApplicationListener { - private WeakReference contextRef; - - public ContextCloserListener(ConfigurableApplicationContext context) { - this.contextRef = new WeakReference(context); + private WeakReference childContext; + public ContextCloserListener(ConfigurableApplicationContext childContext) { + this.childContext = new WeakReference( + 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(); } }