Prevent PostConstruct to be used in production code

Closes gh-36528
pull/37018/head
Stephane Nicoll 1 year ago
parent 2db45e9ba3
commit 73cc54ad34

@ -24,7 +24,6 @@ import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import jakarta.annotation.PostConstruct;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.JobRestartException; import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner; import org.springframework.boot.ApplicationRunner;
@ -67,7 +67,8 @@ import org.springframework.util.StringUtils;
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 2.3.0 * @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. * The default order for the command line runner.
@ -110,13 +111,18 @@ public class JobLauncherApplicationRunner implements ApplicationRunner, Ordered,
this.jobRepository = jobRepository; this.jobRepository = jobRepository;
} }
@PostConstruct @Override
public void validate() { public void afterPropertiesSet() {
if (this.jobs.size() > 1 && !StringUtils.hasText(this.jobName)) { if (this.jobs.size() > 1 && !StringUtils.hasText(this.jobName)) {
throw new IllegalArgumentException("Job name must be specified in case of multiple jobs"); 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) { public void setOrder(int order) {
this.order = order; this.order = order;
} }

@ -23,7 +23,7 @@
<property name="id" value="mainCodeIllegalImportCheck"/> <property name="id" value="mainCodeIllegalImportCheck"/>
<property name="regexp" value="true" /> <property name="regexp" value="true" />
<property name="illegalClasses" <property name="illegalClasses"
value="^javax.annotation.PostConstruct"/> value="javax.annotation.PostConstruct, jakarta.annotation.PostConstruct"/>
</module> </module>
<module <module
name="com.puppycrawl.tools.checkstyle.checks.imports.ImportControlCheck"> name="com.puppycrawl.tools.checkstyle.checks.imports.ImportControlCheck">

Loading…
Cancel
Save