在没有Spring引导的情况下,防止在创建上下文后自动触发Spring批处理作业

在没有Spring引导的情况下,防止在创建上下文后自动触发Spring批处理作业,spring,spring-boot,spring-batch,spring-annotations,spring-config,Spring,Spring Boot,Spring Batch,Spring Annotations,Spring Config,我正在使用Spring批处理设置一个项目,而不使用Spring Boot。 创建Spring应用程序上下文时,所有作业都会执行 我尝试将spring.batch.job.enbled=false添加到application.properties以防止出现这种情况,但仍然不起作用 有没有其他方法可以阻止Spring在开始时执行作业 主要类别: package com.project.batch; import ... @Configuration @EnableBatchProcessi

我正在使用Spring批处理设置一个项目,而不使用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:修复打字错误和更新日志

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

spring.batch.job.enabled=false

spring.batch.job.enabled=false


此配置适用于我

您的主要方法是执行作业。如果你不想要我,请删除那里的代码‎t要在启动时执行…@MichaelMinella我已经删除了作业执行代码,但作业仍然会触发。您可以提供显示启动的日志吗?现在添加日志您的属性中有一个输入错误:它应该是
spring.batch.job.enabled=false
,而不是
enbled