From 73cc54ad3445930c62ce7824420c9f3ccad38be6 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 24 Jul 2023 16:08:50 +0200 Subject: [PATCH] Prevent PostConstruct to be used in production code Closes gh-36528 --- .../batch/JobLauncherApplicationRunner.java | 14 ++++++++++---- src/checkstyle/checkstyle.xml | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherApplicationRunner.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherApplicationRunner.java index a6532ee716..c8fd079f23 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherApplicationRunner.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherApplicationRunner.java @@ -24,7 +24,6 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Properties; -import jakarta.annotation.PostConstruct; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -46,6 +45,7 @@ import org.springframework.batch.core.repository.JobExecutionAlreadyRunningExcep import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException; import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.repository.JobRestartException; +import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; @@ -67,7 +67,8 @@ import org.springframework.util.StringUtils; * @author Stephane Nicoll * @since 2.3.0 */ -public class JobLauncherApplicationRunner implements ApplicationRunner, Ordered, ApplicationEventPublisherAware { +public class JobLauncherApplicationRunner + implements ApplicationRunner, InitializingBean, Ordered, ApplicationEventPublisherAware { /** * The default order for the command line runner. @@ -110,13 +111,18 @@ public class JobLauncherApplicationRunner implements ApplicationRunner, Ordered, this.jobRepository = jobRepository; } - @PostConstruct - public void validate() { + @Override + public void afterPropertiesSet() { if (this.jobs.size() > 1 && !StringUtils.hasText(this.jobName)) { throw new IllegalArgumentException("Job name must be specified in case of multiple jobs"); } } + @Deprecated(since = "3.0.10", forRemoval = true) + public void validate() { + afterPropertiesSet(); + } + public void setOrder(int order) { this.order = order; } diff --git a/src/checkstyle/checkstyle.xml b/src/checkstyle/checkstyle.xml index d1d3c1e747..8ea37ace78 100644 --- a/src/checkstyle/checkstyle.xml +++ b/src/checkstyle/checkstyle.xml @@ -23,7 +23,7 @@ + value="javax.annotation.PostConstruct, jakarta.annotation.PostConstruct"/>