Skip to content

Batch auto-configuration backs off too aggressively without a dependency on spring-jdbc #17451

@Sam-Kruglov

Description

@Sam-Kruglov

If I don't want a database for my jobs in Spring Batch, I want to exclude jdbc from my dependencies like this:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-batch</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </exclusion>
    </exclusions>
</dependency>

And have it work. see https://stackoverflow.com/questions/25077549
Currently, org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration does not get picked up because of conditions not matched, so I have to copy paste the relevant stuff like this:

import org.springframework.batch.core.configuration.ListableJobLocator;
import org.springframework.batch.core.converter.JobParametersConverter;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.JobOperator;
import org.springframework.batch.core.launch.support.SimpleJobOperator;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.autoconfigure.batch.BatchProperties;
import org.springframework.boot.autoconfigure.batch.JobExecutionExitCodeGenerator;
import org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

//copied from org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration
@Configuration
@EnableConfigurationProperties(BatchProperties.class)
public class BatchAutoConfiguration {

    private final BatchProperties properties;

    private final JobParametersConverter jobParametersConverter;

    public BatchAutoConfiguration(
            BatchProperties properties,
            ObjectProvider<JobParametersConverter> jobParametersConverter) {
        this.properties = properties;
        this.jobParametersConverter = jobParametersConverter.getIfAvailable();
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "spring.batch.job", name = "enabled", havingValue = "true", matchIfMissing = true)
    public JobLauncherCommandLineRunner jobLauncherCommandLineRunner(
            JobLauncher jobLauncher, JobExplorer jobExplorer,
            JobRepository jobRepository) {
        JobLauncherCommandLineRunner runner = new JobLauncherCommandLineRunner(jobLauncher, jobExplorer, jobRepository);
        String jobNames = this.properties.getJob().getNames();
        if (StringUtils.hasText(jobNames)) {
            runner.setJobNames(jobNames);
        }
        return runner;
    }

    @Bean
    @ConditionalOnMissingBean(ExitCodeGenerator.class)
    public JobExecutionExitCodeGenerator jobExecutionExitCodeGenerator() {
        return new JobExecutionExitCodeGenerator();
    }

    @Bean
    @ConditionalOnMissingBean(JobOperator.class)
    public SimpleJobOperator jobOperator(
            JobExplorer jobExplorer, JobLauncher jobLauncher,
            ListableJobLocator jobRegistry, JobRepository jobRepository) throws Exception {
        SimpleJobOperator factory = new SimpleJobOperator();
        factory.setJobExplorer(jobExplorer);
        factory.setJobLauncher(jobLauncher);
        factory.setJobRegistry(jobRegistry);
        factory.setJobRepository(jobRepository);
        if (this.jobParametersConverter != null) {
            factory.setJobParametersConverter(this.jobParametersConverter);
        }
        return factory;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions