在没有Spring Boot的情况下创建上下文后,防止Spring Batch自动作业触发

时间:2017-11-02 22:03:50

标签: spring spring-boot spring-batch spring-annotations spring-config

我正在使用 Spring Batch而不使用Spring Boot 来设置项目。 创建Spring应用程序上下文后,将执行所有作业。

我尝试将spring.batch.job.enbled=false添加到 application.properties 以防止这种情况,但它仍无效。

还有其他方法可以阻止Spring在开始时执行作业吗?

主类:

package com.project.batch;
import ...    

@Configuration
@EnableBatchProcessing
@PropertySource("classpath:application.properties")
public class App {
    public static void main(String [] args) throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        System.out.println("starting main");

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.scan("com.project.batch");
        context.refresh();

        //JobParameters jobParameters = new JobParametersBuilder().toJobParameters();
        //JobLauncher jobLauncher = context.getBean(JobLauncher.class);
        //JobExecution execution = jobLauncher.run(context.getBean("loaderJob",Job.class),jobParameters);
        System.out.println("finished!!");
    }
}

工作班:

package com.project.batch;
import ... 

@Configuration
public class LoaderJobConfig {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job loaderJob(Step step1) throws Exception {
        System.out.println("Starting loaderJob");
        ...
    }
}

application.properties:

spring.batch.job.enbled=false
spring.batch.job.names=

运行日志:

starting main
Nov 06, 2017 9:29:02 AM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@306a30c7: startup date [Mon Nov 06 09:29:02 EST 2017]; root of context hierarchy
Nov 06, 2017 9:29:03 AM org.springframework.context.annotation.ConfigurationClassEnhancer intercept
WARNING: @Bean method ScopeConfiguration.stepScope is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean Javadoc for complete details
Nov 06, 2017 9:29:03 AM org.springframework.context.annotation.ConfigurationClassEnhancer intercept
WARNING: @Bean method ScopeConfiguration.jobScope is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean Javadoc for complete details
Nov 06, 2017 9:29:03 AM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: org.postgresql.Driver
Starting loaderJob
found the value: [MER]
Completed loaderJob
finished!!

Process finished with exit code 0

编辑:从主类中删除了作业执行代码,作业仍然在上下文刷新时被触发

编辑2:包括运行日志

编辑3:修正了拼写错误和更新日志

3 个答案:

答案 0 :(得分:2)

Spring docs中提到的解决方案对我有用。我刚刚在application.properties中添加了一行,它在启动时停止了批处理作业的执行。

sed -i.bak '/LogFiles.txt$/s/^/##/'  Input_file

答案 1 :(得分:0)

spring.batch.job.enabled = false

此配置适用于我

答案 2 :(得分:0)

根据 Spring 文档,将以下行添加到 application.properties 对我来说效果很好:

spring.batch.job.enabled=false