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

sau*_*urb 3 spring spring-annotations spring-batch spring-boot spring-config

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

我尝试添加spring.batch.job.enbled=falseapplication.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!!");
    }
}
Run Code Online (Sandbox Code Playgroud)

职位类别:

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");
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

application.properties:

spring.batch.job.enbled=false
spring.batch.job.names=
Run Code Online (Sandbox Code Playgroud)

运行日志:

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
Run Code Online (Sandbox Code Playgroud)

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

编辑2:包括运行日志

编辑3:固定错字和更新的日志

小智 9

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

spring.batch.job.enabled=false
Run Code Online (Sandbox Code Playgroud)