From a28afe09e530e4bfc941f20d4cbc2c5fc2949b80 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 11 May 2016 13:57:15 +0100 Subject: [PATCH] =?UTF-8?q?Tighten=20up=20AutomaticJobRegistrar=E2=80=99s?= =?UTF-8?q?=20event=20listening?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, AutomaticJobRegistrar listened to all ApplicationEvents despite only being interested in two ApplicationContextEvents, namely ContextRefreshedEvent and ContextClosedEvent. This could lead to an AutomaticJobRegistrar instance being created earlier than necessary. This commits tightens up AutomaticJobRegistrar so that it only listens to ApplicationContextEvents. Closes BATCH-2506 See https://github.com/spring-projects/spring-boot/issues/2395 --- .../configuration/support/AutomaticJobRegistrar.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/AutomaticJobRegistrar.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/AutomaticJobRegistrar.java index 716a4e999..5a8fa8a61 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/AutomaticJobRegistrar.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/AutomaticJobRegistrar.java @@ -25,9 +25,9 @@ import org.springframework.batch.core.configuration.JobRegistry; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; -import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.Lifecycle; +import org.springframework.context.event.ApplicationContextEvent; import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.core.Ordered; @@ -43,7 +43,7 @@ import org.springframework.util.Assert; * * @since 2.1 */ -public class AutomaticJobRegistrar implements Ordered, Lifecycle, ApplicationListener, ApplicationContextAware, +public class AutomaticJobRegistrar implements Ordered, Lifecycle, ApplicationListener, ApplicationContextAware, InitializingBean { private Collection applicationContextFactories = new ArrayList(); @@ -59,8 +59,8 @@ public class AutomaticJobRegistrar implements Ordered, Lifecycle, ApplicationLis private int order = Ordered.LOWEST_PRECEDENCE; /** - * The enclosing application context, which can be used to check if {@link ApplicationEvent events} come from the - * expected source. + * The enclosing application context, which can be used to check if {@link ApplicationContextEvent events} come + * from the expected source. * * @param applicationContext the enclosing application context if there is one * @see ApplicationContextAware#setApplicationContext(ApplicationContext) @@ -132,7 +132,7 @@ public class AutomaticJobRegistrar implements Ordered, Lifecycle, ApplicationLis * @see InitializingBean#afterPropertiesSet() */ @Override - public final void onApplicationEvent(ApplicationEvent event) { + public final void onApplicationEvent(ApplicationContextEvent event) { // TODO: With Spring 3 a SmartLifecycle is started automatically if (event.getSource() == applicationContext) { if (event instanceof ContextRefreshedEvent) {